mirror of
				https://github.com/mihonapp/mihon.git
				synced 2025-10-31 14:27:57 +01:00 
			
		
		
		
	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:
		| @@ -4,10 +4,12 @@ import android.util.Log | ||||
| import com.github.salomonbrys.kotson.array | ||||
| import com.github.salomonbrys.kotson.get | ||||
| import com.github.salomonbrys.kotson.jsonObject | ||||
| import com.github.salomonbrys.kotson.nullObj | ||||
| import com.github.salomonbrys.kotson.nullString | ||||
| import com.github.salomonbrys.kotson.obj | ||||
| import com.github.salomonbrys.kotson.string | ||||
| import com.google.gson.JsonArray | ||||
| import com.google.gson.JsonObject | ||||
| import com.google.gson.JsonParser | ||||
| import eu.kanade.tachiyomi.data.database.models.Manga | ||||
| import eu.kanade.tachiyomi.network.asObservableSuccess | ||||
| @@ -30,13 +32,6 @@ open class RecommendsPager( | ||||
| ) : Pager() { | ||||
|     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>>? { | ||||
|         fun getId(): Observable<String?> { | ||||
|             val endpoint = | ||||
| @@ -61,19 +56,14 @@ open class RecommendsPager( | ||||
|                         throw Exception("Null Response") | ||||
|                     } | ||||
|                     val response = JsonParser.parseString(responseBody).obj | ||||
|                     val result = response["results"].array | ||||
|                         .find { | ||||
|                             val title = it["title"].string | ||||
|                             title.contains(manga.title, true) | ||||
|                         } | ||||
|                         ?: return@map null | ||||
|                     val result = response["results"].array.first().nullObj ?: return@map null | ||||
|                     result["mal_id"].string | ||||
|                 } | ||||
|         } | ||||
|  | ||||
|         return getId().map { id -> | ||||
|             if (id == null) { | ||||
|                 return@map listOf<SMangaImpl>() | ||||
|                 return@map null | ||||
|             } | ||||
|             val endpoint = | ||||
|                 myAnimeListEndpoint.toHttpUrlOrNull() | ||||
| @@ -149,12 +139,24 @@ open class RecommendsPager( | ||||
|             "query" to query, | ||||
|             "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() | ||||
|             .url(anilistEndpoint) | ||||
|             .post(body) | ||||
|             .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) | ||||
|             .asObservableSuccess().subscribeOn(Schedulers.io()) | ||||
|             .map { netResponse -> | ||||
| @@ -166,23 +168,14 @@ open class RecommendsPager( | ||||
|                 val data = response["data"]!!.obj | ||||
|                 val page = data["Page"].obj | ||||
|                 val media = page["media"].array | ||||
|                 val result = media.find { | ||||
|                     val title = it["title"].obj | ||||
|                     if (title["romaji"].nullString?.contains(manga.title, true) == true) { | ||||
|                         return@find true | ||||
|                     } | ||||
|                     if (title["english"].nullString?.contains(manga.title, true) == true) { | ||||
|                         return@find true | ||||
|                     } | ||||
|                     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 result = media.sortedWith( | ||||
|                     compareBy( | ||||
|                         { languageContains(it.obj, "romaji", manga.title) }, | ||||
|                         { languageContains(it.obj, "english", manga.title) }, | ||||
|                         { languageContains(it.obj, "native", manga.title) }, | ||||
|                         { countOccurrence(it.obj["synonyms"].array, manga.title) > 0 } | ||||
|                     ) | ||||
|                 ).last().nullObj ?: return@map null | ||||
|                 val recommendations = result["recommendations"].obj | ||||
|                 val edges = recommendations["edges"].array | ||||
|                 edges.map { | ||||
| @@ -191,7 +184,7 @@ open class RecommendsPager( | ||||
|                     SMangaImpl().apply { | ||||
|                         this.title = rec["title"].obj["romaji"].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.initialized = true | ||||
|                         this.url = rec["siteUrl"].string | ||||
| @@ -202,7 +195,8 @@ open class RecommendsPager( | ||||
|  | ||||
|     override fun requestNext(): Observable<MangasPage> { | ||||
|         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()) | ||||
|         } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user