Koushoku: Improve query building (#12612)

This commit is contained in:
MatchaSoba 2022-07-17 22:19:38 +08:00 committed by GitHub
parent 3ffb75cee3
commit 1068bdc8dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 6 deletions

View File

@ -5,7 +5,7 @@ ext {
extName = 'Koushoku'
pkgNameSuffix = 'en.koushoku'
extClass = '.Koushoku'
extVersionCode = 5
extVersionCode = 6
isNsfw = true
}

View File

@ -80,22 +80,31 @@ class Koushoku : ParsedHttpSource() {
val filterList = if (filters.isEmpty()) getFilterList() else filters
filterList.findInstance<SortFilter>()?.addQueryParameter(url)
url.addQueryParameter("q", buildAdvQuery(query, filterList))
println(url)
return GET(url.toString(), headers)
}
private fun buildAdvQuery(query: String, filterList: FilterList): String {
val title = "title*:$query"
val title = if (query.isNotBlank()) "title*:$query " else ""
val filters: List<String> = filterList.filterIsInstance<Filter.Text>().map { filter ->
if (filter.state.isBlank()) return@map ""
val included = mutableListOf<String>()
val excluded = mutableListOf<String>()
val name = if (filter.name.lowercase().contentEquals("tags")) "tag" else filter.name.lowercase()
filter.state.split(",").map(String::trim).filterNot(String::isBlank).forEach { entry ->
if (entry.startsWith("-")) excluded.add(entry) else included.add(entry)
if (entry.startsWith("-")) {
excluded.add(entry.slice(1 until entry.length))
} else {
included.add(entry)
}
}
buildString {
if (included.isNotEmpty()) append("$name*:${included.joinToString(",")} ")
if (excluded.isNotEmpty()) append("-$name*:${excluded.joinToString(",")}")
}
"$name*:${included.joinToString(",")} -$name*:${excluded.joinToString(",")}"
}
return "$title ${filters.joinToString(" ")}"
return "$title${
filters.filterNot(String::isBlank).joinToString(" ", transform = String::trim)
}"
}
override fun searchMangaSelector() = latestUpdatesSelector()