mirror of
https://github.com/amir1376/ab-download-manager.git
synced 2025-02-20 11:43:24 +08:00
add each line import to the import link function
This commit is contained in:
parent
f9d19b5019
commit
55c4cd0279
@ -3,9 +3,27 @@ package com.abdownloadmanager.utils.extractors.linkextractor
|
||||
import com.abdownloadmanager.utils.extractors.Extractor
|
||||
import com.abdownloadmanager.utils.isValidUrl
|
||||
|
||||
object StringUrlExtractor: Extractor<String, List<String>> {
|
||||
private val urlRegex = Regex("""\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]""")
|
||||
override fun extract(input: String):List<String>{
|
||||
object StringUrlExtractor : Extractor<String, List<String>> {
|
||||
private val urlRegex by lazy { Regex("""\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]""") }
|
||||
override fun extract(input: String): List<String> {
|
||||
// maybe each line is a link
|
||||
val linksInEachLines = byEachLine(input)
|
||||
if (linksInEachLines.isNotEmpty()) {
|
||||
return linksInEachLines
|
||||
}
|
||||
// try to find links by regex
|
||||
return byRegex(input)
|
||||
}
|
||||
|
||||
private fun byEachLine(input: String): List<String> {
|
||||
return input
|
||||
.lineSequence()
|
||||
.map { it.trim() }
|
||||
.filter { isValidUrl(it) }
|
||||
.toList()
|
||||
}
|
||||
|
||||
private fun byRegex(input: String): List<String> {
|
||||
return urlRegex.findAll(input)
|
||||
.map { it.value }
|
||||
.filter { isValidUrl(it) }
|
||||
|
Loading…
x
Reference in New Issue
Block a user