Improve recommendations (#14)
* Improve anilist recommendations by matching synonyms; Some bug fixes related to anilist * Fix formatting * Sort myanimelist results by result.title matching search.title * Throw an exception if the result is not the manga we searched Co-authored-by: she11sh0cked <she11sh0cked@users.noreply.github.com>
This commit is contained in:
parent
7e65e0de0e
commit
7ce15caded
@ -7,6 +7,7 @@ import com.github.salomonbrys.kotson.jsonObject
|
|||||||
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.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
|
||||||
@ -29,6 +30,13 @@ open class RecommendsPager(
|
|||||||
) : Pager() {
|
) : Pager() {
|
||||||
private val client = OkHttpClient.Builder().build()
|
private val client = OkHttpClient.Builder().build()
|
||||||
|
|
||||||
|
private fun countOccurance(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 =
|
||||||
@ -54,8 +62,16 @@ open class RecommendsPager(
|
|||||||
}
|
}
|
||||||
val response = JsonParser.parseString(responseBody).obj
|
val response = JsonParser.parseString(responseBody).obj
|
||||||
val results = response["results"].array
|
val results = response["results"].array
|
||||||
val firstResult = results[0].obj
|
.sortedBy {
|
||||||
val id = firstResult["mal_id"].string
|
val title = it["title"].string
|
||||||
|
title.contains(manga.title, true)
|
||||||
|
}
|
||||||
|
val result = results.last()
|
||||||
|
val title = result["title"].string
|
||||||
|
if (!title.contains(manga.title, true)) {
|
||||||
|
throw Exception("Not found")
|
||||||
|
}
|
||||||
|
val id = result["mal_id"].string
|
||||||
if (id.isEmpty()) {
|
if (id.isEmpty()) {
|
||||||
throw Exception("Not found")
|
throw Exception("Not found")
|
||||||
}
|
}
|
||||||
@ -104,22 +120,27 @@ open class RecommendsPager(
|
|||||||
val query =
|
val query =
|
||||||
"""
|
"""
|
||||||
{
|
{
|
||||||
Media(search: "$manga.title", type: MANGA) {
|
Page {
|
||||||
title{
|
media(search: "${manga.title}", type: MANGA) {
|
||||||
romaji
|
title {
|
||||||
english
|
romaji
|
||||||
native
|
english
|
||||||
}
|
native
|
||||||
recommendations {
|
}
|
||||||
edges {
|
synonyms
|
||||||
node {
|
recommendations {
|
||||||
mediaRecommendation {
|
edges {
|
||||||
siteUrl
|
node {
|
||||||
title {
|
mediaRecommendation {
|
||||||
romaji
|
siteUrl
|
||||||
}
|
title {
|
||||||
coverImage {
|
romaji
|
||||||
large
|
english
|
||||||
|
native
|
||||||
|
}
|
||||||
|
coverImage {
|
||||||
|
large
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -148,8 +169,24 @@ open class RecommendsPager(
|
|||||||
}
|
}
|
||||||
val response = JsonParser.parseString(responseBody).obj
|
val response = JsonParser.parseString(responseBody).obj
|
||||||
val data = response["data"]!!.obj
|
val data = response["data"]!!.obj
|
||||||
val media = data["Media"].obj
|
val page = data["Page"].obj
|
||||||
val recommendations = media["recommendations"].obj
|
val media = page["media"].array
|
||||||
|
val results = media.sortedBy {
|
||||||
|
val synonyms = it["synonyms"].array
|
||||||
|
countOccurance(synonyms, manga.title)
|
||||||
|
}
|
||||||
|
val result = results.last()
|
||||||
|
val title = result["title"].obj
|
||||||
|
val synonyms = result["synonyms"].array
|
||||||
|
if (
|
||||||
|
title["romaji"].nullString?.contains("", true) != true &&
|
||||||
|
title["english"].nullString?.contains("", true) != true &&
|
||||||
|
title["native"].nullString?.contains("", true) != true &&
|
||||||
|
countOccurance(synonyms, manga.title) <= 0
|
||||||
|
) {
|
||||||
|
throw Exception("Not found")
|
||||||
|
}
|
||||||
|
val recommendations = result["recommendations"].obj
|
||||||
val edges = recommendations["edges"].array
|
val edges = recommendations["edges"].array
|
||||||
edges.map {
|
edges.map {
|
||||||
val rec = it["node"]["mediaRecommendation"].obj
|
val rec = it["node"]["mediaRecommendation"].obj
|
||||||
|
Loading…
x
Reference in New Issue
Block a user