More recs cleanup

This commit is contained in:
Jobobby04 2020-11-29 20:38:22 -05:00
parent 5b54fc8885
commit 1118fe7cf7

View File

@ -1,6 +1,8 @@
package exh.recs package exh.recs
import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.network.GET
import eu.kanade.tachiyomi.network.POST
import eu.kanade.tachiyomi.network.await import eu.kanade.tachiyomi.network.await
import eu.kanade.tachiyomi.source.model.MangasPage import eu.kanade.tachiyomi.source.model.MangasPage
import eu.kanade.tachiyomi.source.model.SMangaImpl import eu.kanade.tachiyomi.source.model.SMangaImpl
@ -27,7 +29,6 @@ import kotlinx.serialization.json.put
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody import okhttp3.RequestBody.Companion.toRequestBody
import rx.Observable import rx.Observable
import timber.log.Timber import timber.log.Timber
@ -44,59 +45,42 @@ abstract class API(_endpoint: String) {
class MyAnimeList : API("https://api.jikan.moe/v3/") { class MyAnimeList : API("https://api.jikan.moe/v3/") {
private suspend fun getRecsById(id: String): List<SMangaImpl> { private suspend fun getRecsById(id: String): List<SMangaImpl> {
val httpUrl = endpoint.toHttpUrlOrNull() ?: throw Exception("Could not convert endpoint url") val httpUrl = endpoint.toHttpUrlOrNull() ?: throw Exception("Could not convert endpoint url")
val urlBuilder = httpUrl.newBuilder() val apiUrl = httpUrl.newBuilder()
urlBuilder.addPathSegment("manga") .addPathSegment("manga")
urlBuilder.addPathSegment(id) .addPathSegment(id)
urlBuilder.addPathSegment("recommendations") .addPathSegment("recommendations")
val url = urlBuilder.build().toUrl()
val request = Request.Builder()
.url(url)
.get()
.build() .build()
.toString()
val response = client.newCall(request).await() val response = client.newCall(GET(apiUrl)).await()
val body = withContext(Dispatchers.IO) { response.body?.string().orEmpty() } val body = withContext(Dispatchers.IO) { response.body?.string() } ?: throw Exception("Null Response")
if (body.isEmpty()) {
throw Exception("Null Response")
}
val data = Json.decodeFromString<JsonObject>(body) val data = Json.decodeFromString<JsonObject>(body)
val recommendations = data["recommendations"] as? JsonArray val recommendations = data["recommendations"] as? JsonArray
?: throw Exception("Unexpected response") return recommendations?.filterIsInstance<JsonObject>()?.map { rec ->
return recommendations.map { rec -> Timber.tag("RECOMMENDATIONS").d("MYANIMELIST > RECOMMENDATION: %s", rec["title"]?.jsonPrimitive?.content.orEmpty())
rec as? JsonObject ?: throw Exception("Invalid json")
Timber.tag("RECOMMENDATIONS").d("MYANIMELIST > RECOMMENDATION: %s", rec["title"]!!.jsonPrimitive.content)
SMangaImpl().apply { SMangaImpl().apply {
title = rec["title"]!!.jsonPrimitive.content title = rec["title"]!!.jsonPrimitive.content
thumbnail_url = rec["image_url"]!!.jsonPrimitive.content thumbnail_url = rec["image_url"]!!.jsonPrimitive.content
initialized = true initialized = true
this.url = rec["url"]!!.jsonPrimitive.content url = rec["url"]!!.jsonPrimitive.content
}
} }
}.orEmpty()
} }
override suspend fun getRecsBySearch(search: String): List<SMangaImpl> { override suspend fun getRecsBySearch(search: String): List<SMangaImpl> {
val httpUrl = val httpUrl = endpoint.toHttpUrlOrNull() ?: throw Exception("Could not convert endpoint url")
endpoint.toHttpUrlOrNull() ?: throw Exception("Could not convert endpoint url") val url = httpUrl.newBuilder()
val urlBuilder = httpUrl.newBuilder() .addPathSegment("search")
urlBuilder.addPathSegment("search") .addPathSegment("manga")
urlBuilder.addPathSegment("manga") .addQueryParameter("q", search)
urlBuilder.addQueryParameter("q", search)
val url = urlBuilder.build().toUrl()
val request = Request.Builder()
.url(url)
.get()
.build() .build()
.toString()
val response = client.newCall(request).await() val response = client.newCall(GET(url)).await()
val body = withContext(Dispatchers.IO) { response.body?.string().orEmpty() } val body = withContext(Dispatchers.IO) { response.body?.string() } ?: throw Exception("Null Response")
if (body.isEmpty()) {
throw Exception("Null Response")
}
val data = Json.decodeFromString<JsonObject>(body) val data = Json.decodeFromString<JsonObject>(body)
val results = data["results"] as? JsonArray ?: throw Exception("Unexpected response") val results = data["results"] as? JsonArray
if (results.size <= 0) { if (results.isNullOrEmpty()) {
throw Exception("'$search' not found") throw Exception("'$search' not found")
} }
val result = results.first().jsonObject val result = results.first().jsonObject
@ -165,23 +149,14 @@ class Anilist : API("https://graphql.anilist.co/") {
put("query", query) put("query", query)
put("variables", variables) put("variables", variables)
} }
val payloadBody = val payloadBody = payload.toString().toRequestBody("application/json; charset=utf-8".toMediaTypeOrNull())
payload.toString().toRequestBody("application/json; charset=utf-8".toMediaTypeOrNull())
val request = Request.Builder()
.url(endpoint)
.post(payloadBody)
.build()
val response = client.newCall(request).await() val response = client.newCall(POST(endpoint, body = payloadBody)).await()
val body = withContext(Dispatchers.IO) { response.body?.string().orEmpty() } val body = withContext(Dispatchers.IO) { response.body?.string() } ?: throw Exception("Null Response")
if (body.isEmpty()) { val data = Json.decodeFromString<JsonObject>(body)["data"] as? JsonObject ?: throw Exception("Unexpected response")
throw Exception("Null Response")
} val media = data["Page"]?.jsonObject?.get("media")?.jsonArray
val data = Json.decodeFromString<JsonObject>(body)["data"] as? JsonObject if (media.isNullOrEmpty()) {
?: throw Exception("Unexpected response")
val page = data["Page"]!!.jsonObject
val media = page["media"]!!.jsonArray
if (media.size <= 0) {
throw Exception("'$search' not found") throw Exception("'$search' not found")
} }
val result = media.sortedWith( val result = media.sortedWith(
@ -192,17 +167,18 @@ class Anilist : API("https://graphql.anilist.co/") {
{ countOccurrence(it.jsonObject["synonyms"]!!.jsonArray, search) > 0 } { countOccurrence(it.jsonObject["synonyms"]!!.jsonArray, search) > 0 }
) )
).last().jsonObject ).last().jsonObject
val recommendations = result["recommendations"]!!.jsonObject["edges"]!!.jsonArray
return recommendations.map { return result["recommendations"]?.jsonObject?.get("edges")?.jsonArray?.map {
val rec = it.jsonObject["node"]!!.jsonObject["mediaRecommendation"]!!.jsonObject val rec = it.jsonObject["node"]!!.jsonObject["mediaRecommendation"]!!.jsonObject
Timber.tag("RECOMMENDATIONS").d("ANILIST > RECOMMENDATION: %s", getTitle(rec)) val recTitle = getTitle(rec)
Timber.tag("RECOMMENDATIONS").d("ANILIST > RECOMMENDATION: %s", recTitle)
SMangaImpl().apply { SMangaImpl().apply {
title = getTitle(rec) title = recTitle
thumbnail_url = rec["coverImage"]!!.jsonObject["large"]!!.jsonPrimitive.content thumbnail_url = rec["coverImage"]!!.jsonObject["large"]!!.jsonPrimitive.content
initialized = true initialized = true
url = rec["siteUrl"]!!.jsonPrimitive.content url = rec["siteUrl"]!!.jsonPrimitive.content
} }
} }.orEmpty()
} }
} }