Cubari: implement client side search (#15803)

This commit is contained in:
mobi2002 2023-03-24 07:37:24 +05:00 committed by GitHub
parent 1b4ccdf0d8
commit 45553c5331
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 4 deletions

View File

@ -6,7 +6,7 @@ ext {
extName = 'Cubari'
pkgNameSuffix = "all.cubari"
extClass = '.CubariFactory'
extVersionCode = 18
extVersionCode = 19
}
apply from: "$rootDir/common.gradle"

View File

@ -224,17 +224,34 @@ open class Cubari(override val lang: String) : HttpSource() {
client.newBuilder()
.addInterceptor(RemoteStorageUtils.TagInterceptor())
.build()
.newCall(searchMangaRequest(page, trimmedQuery, filters))
.newCall(proxySearchRequest(trimmedQuery))
.asObservableSuccess()
.map { response ->
searchMangaParse(response, trimmedQuery)
proxySearchParse(response, trimmedQuery)
}
}
else -> {
client.newBuilder()
.addInterceptor(RemoteStorageUtils.HomeInterceptor())
.build()
.newCall(searchMangaRequest(page, query, filters))
.asObservableSuccess()
.map { response ->
searchMangaParse(response, query)
}
.map { mangasPage ->
require(mangasPage.mangas.isNotEmpty()) { SEARCH_FALLBACK_MSG }
mangasPage
}
}
else -> throw Exception(SEARCH_FALLBACK_MSG)
}
}
override fun searchMangaRequest(page: Int, query: String, filters: FilterList): Request {
return GET("$baseUrl/", headers)
}
private fun proxySearchRequest(query: String): Request {
try {
val queryFragments = query.split("/")
val source = queryFragments[0]
@ -251,6 +268,17 @@ open class Cubari(override val lang: String) : HttpSource() {
}
private fun searchMangaParse(response: Response, query: String): MangasPage {
val result = json.parseToJsonElement(response.body.string()).jsonArray
val filterList = result.asSequence()
.map { it as JsonObject }
.filter { it["title"].toString().contains(query.trim(), true) }
.toList()
return parseMangaList(JsonArray(filterList), SortType.ALL)
}
private fun proxySearchParse(response: Response, query: String): MangasPage {
val result = json.parseToJsonElement(response.body.string()).jsonObject
return parseSearchList(result, query)
}
@ -328,6 +356,8 @@ open class Cubari(override val lang: String) : HttpSource() {
parseManga(jsonObj)
} else if (sortType == SortType.UNPINNED && !pinned) {
parseManga(jsonObj)
} else if (sortType == SortType.ALL) {
parseManga(jsonObj)
} else {
null
}
@ -383,6 +413,7 @@ open class Cubari(override val lang: String) : HttpSource() {
enum class SortType {
PINNED,
UNPINNED,
ALL,
}
}
}