Fix multiple recommendation bugs (#18)
* Fix multiple recommendation bugs * Fix typo * Fix typings * Remove unused import Co-authored-by: she11sh0cked <she11sh0cked@users.noreply.github.com>
This commit is contained in:
parent
5e618e0134
commit
6da22ea8b0
@ -4,10 +4,12 @@ import android.util.Log
|
|||||||
import com.github.salomonbrys.kotson.array
|
import com.github.salomonbrys.kotson.array
|
||||||
import com.github.salomonbrys.kotson.get
|
import com.github.salomonbrys.kotson.get
|
||||||
import com.github.salomonbrys.kotson.jsonObject
|
import com.github.salomonbrys.kotson.jsonObject
|
||||||
|
import com.github.salomonbrys.kotson.nullObj
|
||||||
import com.github.salomonbrys.kotson.nullString
|
import com.github.salomonbrys.kotson.nullString
|
||||||
import com.github.salomonbrys.kotson.obj
|
import com.github.salomonbrys.kotson.obj
|
||||||
import com.github.salomonbrys.kotson.string
|
import com.github.salomonbrys.kotson.string
|
||||||
import com.google.gson.JsonArray
|
import com.google.gson.JsonArray
|
||||||
|
import com.google.gson.JsonObject
|
||||||
import com.google.gson.JsonParser
|
import com.google.gson.JsonParser
|
||||||
import eu.kanade.tachiyomi.data.database.models.Manga
|
import eu.kanade.tachiyomi.data.database.models.Manga
|
||||||
import eu.kanade.tachiyomi.network.asObservableSuccess
|
import eu.kanade.tachiyomi.network.asObservableSuccess
|
||||||
@ -30,13 +32,6 @@ open class RecommendsPager(
|
|||||||
) : Pager() {
|
) : Pager() {
|
||||||
private val client = OkHttpClient.Builder().build()
|
private val client = OkHttpClient.Builder().build()
|
||||||
|
|
||||||
private fun countOccurrence(array: JsonArray, search: String): Int {
|
|
||||||
return array.count {
|
|
||||||
val synonym = it.string
|
|
||||||
synonym.contains(search, true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun myAnimeList(): Observable<List<SMangaImpl>>? {
|
private fun myAnimeList(): Observable<List<SMangaImpl>>? {
|
||||||
fun getId(): Observable<String?> {
|
fun getId(): Observable<String?> {
|
||||||
val endpoint =
|
val endpoint =
|
||||||
@ -61,19 +56,14 @@ open class RecommendsPager(
|
|||||||
throw Exception("Null Response")
|
throw Exception("Null Response")
|
||||||
}
|
}
|
||||||
val response = JsonParser.parseString(responseBody).obj
|
val response = JsonParser.parseString(responseBody).obj
|
||||||
val result = response["results"].array
|
val result = response["results"].array.first().nullObj ?: return@map null
|
||||||
.find {
|
|
||||||
val title = it["title"].string
|
|
||||||
title.contains(manga.title, true)
|
|
||||||
}
|
|
||||||
?: return@map null
|
|
||||||
result["mal_id"].string
|
result["mal_id"].string
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return getId().map { id ->
|
return getId().map { id ->
|
||||||
if (id == null) {
|
if (id == null) {
|
||||||
return@map listOf<SMangaImpl>()
|
return@map null
|
||||||
}
|
}
|
||||||
val endpoint =
|
val endpoint =
|
||||||
myAnimeListEndpoint.toHttpUrlOrNull()
|
myAnimeListEndpoint.toHttpUrlOrNull()
|
||||||
@ -149,12 +139,24 @@ open class RecommendsPager(
|
|||||||
"query" to query,
|
"query" to query,
|
||||||
"variables" to variables
|
"variables" to variables
|
||||||
)
|
)
|
||||||
val body = payload.toString().toRequestBody("application/json; charset=utf-8".toMediaTypeOrNull())
|
val body =
|
||||||
|
payload.toString().toRequestBody("application/json; charset=utf-8".toMediaTypeOrNull())
|
||||||
val request = Request.Builder()
|
val request = Request.Builder()
|
||||||
.url(anilistEndpoint)
|
.url(anilistEndpoint)
|
||||||
.post(body)
|
.post(body)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
|
fun countOccurrence(array: JsonArray, search: String): Int {
|
||||||
|
return array.count {
|
||||||
|
val synonym = it.string
|
||||||
|
synonym.contains(search, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun languageContains(it: JsonObject, language: String, search: String): Boolean {
|
||||||
|
return it["title"].obj[language].nullString?.contains(search, true) == true
|
||||||
|
}
|
||||||
|
|
||||||
return client.newCall(request)
|
return client.newCall(request)
|
||||||
.asObservableSuccess().subscribeOn(Schedulers.io())
|
.asObservableSuccess().subscribeOn(Schedulers.io())
|
||||||
.map { netResponse ->
|
.map { netResponse ->
|
||||||
@ -166,23 +168,14 @@ open class RecommendsPager(
|
|||||||
val data = response["data"]!!.obj
|
val data = response["data"]!!.obj
|
||||||
val page = data["Page"].obj
|
val page = data["Page"].obj
|
||||||
val media = page["media"].array
|
val media = page["media"].array
|
||||||
val result = media.find {
|
val result = media.sortedWith(
|
||||||
val title = it["title"].obj
|
compareBy(
|
||||||
if (title["romaji"].nullString?.contains(manga.title, true) == true) {
|
{ languageContains(it.obj, "romaji", manga.title) },
|
||||||
return@find true
|
{ languageContains(it.obj, "english", manga.title) },
|
||||||
}
|
{ languageContains(it.obj, "native", manga.title) },
|
||||||
if (title["english"].nullString?.contains(manga.title, true) == true) {
|
{ countOccurrence(it.obj["synonyms"].array, manga.title) > 0 }
|
||||||
return@find true
|
)
|
||||||
}
|
).last().nullObj ?: return@map null
|
||||||
if (title["native"].nullString?.contains(manga.title, true) == true) {
|
|
||||||
return@find true
|
|
||||||
}
|
|
||||||
if (countOccurrence(it["synonyms"].array, manga.title) <= 0) {
|
|
||||||
return@find true
|
|
||||||
}
|
|
||||||
false
|
|
||||||
}
|
|
||||||
?: return@map listOf<SMangaImpl>()
|
|
||||||
val recommendations = result["recommendations"].obj
|
val recommendations = result["recommendations"].obj
|
||||||
val edges = recommendations["edges"].array
|
val edges = recommendations["edges"].array
|
||||||
edges.map {
|
edges.map {
|
||||||
@ -191,7 +184,7 @@ open class RecommendsPager(
|
|||||||
SMangaImpl().apply {
|
SMangaImpl().apply {
|
||||||
this.title = rec["title"].obj["romaji"].nullString
|
this.title = rec["title"].obj["romaji"].nullString
|
||||||
?: rec["title"].obj["english"].nullString
|
?: rec["title"].obj["english"].nullString
|
||||||
?: rec["title"].obj["native"].string
|
?: rec["title"].obj["native"].string
|
||||||
this.thumbnail_url = rec["coverImage"].obj["large"].string
|
this.thumbnail_url = rec["coverImage"].obj["large"].string
|
||||||
this.initialized = true
|
this.initialized = true
|
||||||
this.url = rec["siteUrl"].string
|
this.url = rec["siteUrl"].string
|
||||||
@ -202,7 +195,8 @@ open class RecommendsPager(
|
|||||||
|
|
||||||
override fun requestNext(): Observable<MangasPage> {
|
override fun requestNext(): Observable<MangasPage> {
|
||||||
if (smart) {
|
if (smart) {
|
||||||
preferredApi = if (manga.mangaType() != MangaType.TYPE_MANGA) API.ANILIST else preferredApi
|
preferredApi =
|
||||||
|
if (manga.mangaType() != MangaType.TYPE_MANGA) API.ANILIST else preferredApi
|
||||||
Log.d("SMART RECOMMEND", preferredApi.toString())
|
Log.d("SMART RECOMMEND", preferredApi.toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user