Display recommendations from AniList
(cherry picked from commit f032667be5d2c9cfa5000ab1e166c7aaedbac701)
This commit is contained in:
parent
b0f39a7d0a
commit
4c5e99142f
@ -61,6 +61,7 @@ interface SManga : Serializable {
|
||||
const val ONGOING = 1
|
||||
const val COMPLETED = 2
|
||||
const val LICENSED = 3
|
||||
const val RECOMMENDS = 69 // nice
|
||||
|
||||
fun create(): SManga {
|
||||
return SMangaImpl()
|
||||
|
@ -138,7 +138,8 @@ open class BrowseSourceController(bundle: Bundle) :
|
||||
override fun createPresenter(): BrowseSourcePresenter {
|
||||
return BrowseSourcePresenter(
|
||||
args.getLong(SOURCE_ID_KEY),
|
||||
args.getString(SEARCH_QUERY_KEY)
|
||||
if (mode == Mode.RECOMMENDS) recommendsConfig!!.origTitle else args.getString(SEARCH_QUERY_KEY),
|
||||
recommends = (mode == Mode.RECOMMENDS)
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -56,7 +56,8 @@ open class BrowseSourcePresenter(
|
||||
private val sourceManager: SourceManager = Injekt.get(),
|
||||
private val db: DatabaseHelper = Injekt.get(),
|
||||
private val prefs: PreferencesHelper = Injekt.get(),
|
||||
private val coverCache: CoverCache = Injekt.get()
|
||||
private val coverCache: CoverCache = Injekt.get(),
|
||||
private val recommends: Boolean = false
|
||||
) : BasePresenter<BrowseSourceController>() {
|
||||
|
||||
/**
|
||||
@ -67,9 +68,8 @@ open class BrowseSourcePresenter(
|
||||
/**
|
||||
* Query from the view.
|
||||
*/
|
||||
var query = searchQuery ?: ""
|
||||
var query = if (recommends) "" else searchQuery ?: ""
|
||||
private set
|
||||
|
||||
/**
|
||||
* Modifiable list of filters.
|
||||
*/
|
||||
@ -154,7 +154,7 @@ open class BrowseSourcePresenter(
|
||||
subscribeToMangaInitializer()
|
||||
|
||||
// Create a new pager.
|
||||
pager = createPager(query, filters)
|
||||
pager = if (recommends) RecommendsPager(searchQuery!!) else createPager(query, filters)
|
||||
|
||||
val sourceId = source.id
|
||||
|
||||
|
@ -0,0 +1,95 @@
|
||||
package eu.kanade.tachiyomi.ui.source.browse
|
||||
|
||||
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.obj
|
||||
import com.github.salomonbrys.kotson.string
|
||||
import com.google.gson.JsonParser
|
||||
import eu.kanade.tachiyomi.network.asObservableSuccess
|
||||
import eu.kanade.tachiyomi.source.model.MangasPage
|
||||
import eu.kanade.tachiyomi.source.model.SMangaImpl
|
||||
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
import rx.Observable
|
||||
import rx.schedulers.Schedulers
|
||||
|
||||
open class RecommendsPager(val title: String) : Pager() {
|
||||
private val client = OkHttpClient.Builder().build()
|
||||
|
||||
override fun requestNext(): Observable<MangasPage> {
|
||||
val query =
|
||||
"""
|
||||
{
|
||||
Media(search: "$title", type: MANGA) {
|
||||
title{
|
||||
romaji
|
||||
}
|
||||
recommendations {
|
||||
edges {
|
||||
node {
|
||||
mediaRecommendation {
|
||||
siteUrl
|
||||
title {
|
||||
romaji
|
||||
}
|
||||
coverImage {
|
||||
large
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""".trimIndent()
|
||||
val variables = jsonObject()
|
||||
val payload = jsonObject(
|
||||
"query" to query,
|
||||
"variables" to variables
|
||||
)
|
||||
val body = payload.toString().toRequestBody("application/json; charset=utf-8".toMediaTypeOrNull())
|
||||
val request = Request.Builder()
|
||||
.url(apiUrl)
|
||||
.post(body)
|
||||
.build()
|
||||
|
||||
return client.newCall(request)
|
||||
.asObservableSuccess().subscribeOn(Schedulers.io())
|
||||
.map { netResponse ->
|
||||
val responseBody = netResponse.body?.string().orEmpty()
|
||||
if (responseBody.isEmpty()) {
|
||||
throw Exception("Null Response")
|
||||
}
|
||||
val response = JsonParser.parseString(responseBody).obj
|
||||
val data = response["data"]!!.obj
|
||||
val media = data["Media"].obj
|
||||
val recommendations = media["recommendations"].obj
|
||||
val edges = recommendations["edges"].array
|
||||
edges.map {
|
||||
val rec = it["node"]["mediaRecommendation"].obj
|
||||
Log.d("RECOMMEND", "${rec["title"].obj["romaji"].string}")
|
||||
SMangaImpl().apply {
|
||||
this.title = rec["title"].obj["romaji"].string
|
||||
this.thumbnail_url = rec["coverImage"].obj["large"].string
|
||||
this.initialized = true
|
||||
this.url = rec["siteUrl"].string
|
||||
}
|
||||
}
|
||||
}.map {
|
||||
MangasPage(it, false)
|
||||
}.doOnNext {
|
||||
if (it.mangas.isNotEmpty()) {
|
||||
onPageReceived(it)
|
||||
} else {
|
||||
throw NoResultsException()
|
||||
}
|
||||
}
|
||||
}
|
||||
companion object {
|
||||
const val apiUrl = "https://graphql.anilist.co/"
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user