Smart recommendations (#10)
* Pass manga through to RecommendsPager * Implement smart recommendations * Add null checks * Add more anilistSmart tags * Add fallback titles Co-authored-by: she11sh0cked <she11sh0cked@users.noreply.github.com>
This commit is contained in:
parent
b0251b8177
commit
a9de6a123e
@ -133,14 +133,15 @@ open class BrowseSourceController(bundle: Bundle) :
|
|||||||
override fun getTitle(): String? {
|
override fun getTitle(): String? {
|
||||||
return when (mode) {
|
return when (mode) {
|
||||||
Mode.CATALOGUE -> presenter.source.name
|
Mode.CATALOGUE -> presenter.source.name
|
||||||
Mode.RECOMMENDS -> recommendsConfig!!.origTitle
|
Mode.RECOMMENDS -> recommendsConfig!!.manga.title
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun createPresenter(): BrowseSourcePresenter {
|
override fun createPresenter(): BrowseSourcePresenter {
|
||||||
return BrowseSourcePresenter(
|
return BrowseSourcePresenter(
|
||||||
args.getLong(SOURCE_ID_KEY),
|
args.getLong(SOURCE_ID_KEY),
|
||||||
if (mode == Mode.RECOMMENDS) recommendsConfig!!.origTitle else args.getString(SEARCH_QUERY_KEY),
|
args.getString(SEARCH_QUERY_KEY),
|
||||||
|
searchManga = if (mode == Mode.RECOMMENDS) recommendsConfig?.manga else null,
|
||||||
recommends = (mode == Mode.RECOMMENDS)
|
recommends = (mode == Mode.RECOMMENDS)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -776,8 +777,9 @@ open class BrowseSourceController(bundle: Bundle) :
|
|||||||
}
|
}
|
||||||
activity?.toast(activity?.getString(R.string.manga_added_library))
|
activity?.toast(activity?.getString(R.string.manga_added_library))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Parcelize
|
@Parcelize
|
||||||
data class RecommendsConfig(val origTitle: String, val origSource: Long) : Parcelable
|
data class RecommendsConfig(val manga: Manga) : Parcelable
|
||||||
|
|
||||||
enum class Mode {
|
enum class Mode {
|
||||||
CATALOGUE,
|
CATALOGUE,
|
||||||
|
@ -53,6 +53,7 @@ import xyz.nulldev.ts.api.http.serializer.FilterSerializer
|
|||||||
open class BrowseSourcePresenter(
|
open class BrowseSourcePresenter(
|
||||||
private val sourceId: Long,
|
private val sourceId: Long,
|
||||||
private val searchQuery: String? = null,
|
private val searchQuery: String? = null,
|
||||||
|
private val searchManga: Manga? = null,
|
||||||
private val sourceManager: SourceManager = Injekt.get(),
|
private val sourceManager: SourceManager = Injekt.get(),
|
||||||
private val db: DatabaseHelper = Injekt.get(),
|
private val db: DatabaseHelper = Injekt.get(),
|
||||||
private val prefs: PreferencesHelper = Injekt.get(),
|
private val prefs: PreferencesHelper = Injekt.get(),
|
||||||
@ -154,8 +155,8 @@ open class BrowseSourcePresenter(
|
|||||||
subscribeToMangaInitializer()
|
subscribeToMangaInitializer()
|
||||||
|
|
||||||
// Create a new pager.
|
// Create a new pager.
|
||||||
pager = if (recommends) RecommendsPager(
|
pager = if (recommends && searchManga != null) RecommendsPager(
|
||||||
searchQuery!!
|
searchManga
|
||||||
) else createPager(query, filters)
|
) else createPager(query, filters)
|
||||||
|
|
||||||
val sourceId = source.id
|
val sourceId = source.id
|
||||||
|
@ -4,12 +4,15 @@ 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.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.JsonParser
|
import com.google.gson.JsonParser
|
||||||
|
import eu.kanade.tachiyomi.data.database.models.Manga
|
||||||
import eu.kanade.tachiyomi.network.asObservableSuccess
|
import eu.kanade.tachiyomi.network.asObservableSuccess
|
||||||
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
|
||||||
|
import java.util.Locale
|
||||||
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
|
||||||
@ -18,7 +21,11 @@ import okhttp3.RequestBody.Companion.toRequestBody
|
|||||||
import rx.Observable
|
import rx.Observable
|
||||||
import rx.schedulers.Schedulers
|
import rx.schedulers.Schedulers
|
||||||
|
|
||||||
open class RecommendsPager(val title: String, val preferredApi: API = API.MYANIMELIST) : Pager() {
|
open class RecommendsPager(
|
||||||
|
val manga: Manga,
|
||||||
|
val smart: Boolean = true,
|
||||||
|
var preferredApi: API = API.MYANIMELIST
|
||||||
|
) : Pager() {
|
||||||
private val client = OkHttpClient.Builder().build()
|
private val client = OkHttpClient.Builder().build()
|
||||||
|
|
||||||
private fun myAnimeList(): Observable<List<SMangaImpl>>? {
|
private fun myAnimeList(): Observable<List<SMangaImpl>>? {
|
||||||
@ -29,7 +36,7 @@ open class RecommendsPager(val title: String, val preferredApi: API = API.MYANIM
|
|||||||
val urlBuilder = endpoint.newBuilder()
|
val urlBuilder = endpoint.newBuilder()
|
||||||
urlBuilder.addPathSegment("search")
|
urlBuilder.addPathSegment("search")
|
||||||
urlBuilder.addPathSegment("manga")
|
urlBuilder.addPathSegment("manga")
|
||||||
urlBuilder.addQueryParameter("q", title)
|
urlBuilder.addQueryParameter("q", manga.title)
|
||||||
val url = urlBuilder.build().toUrl()
|
val url = urlBuilder.build().toUrl()
|
||||||
|
|
||||||
val request = Request.Builder()
|
val request = Request.Builder()
|
||||||
@ -96,9 +103,11 @@ open class RecommendsPager(val title: String, val preferredApi: API = API.MYANIM
|
|||||||
val query =
|
val query =
|
||||||
"""
|
"""
|
||||||
{
|
{
|
||||||
Media(search: "$title", type: MANGA) {
|
Media(search: "$manga.title", type: MANGA) {
|
||||||
title{
|
title{
|
||||||
romaji
|
romaji
|
||||||
|
english
|
||||||
|
native
|
||||||
}
|
}
|
||||||
recommendations {
|
recommendations {
|
||||||
edges {
|
edges {
|
||||||
@ -145,7 +154,9 @@ open class RecommendsPager(val title: String, val preferredApi: API = API.MYANIM
|
|||||||
val rec = it["node"]["mediaRecommendation"].obj
|
val rec = it["node"]["mediaRecommendation"].obj
|
||||||
Log.d("ANILIST RECOMMEND", "${rec["title"].obj["romaji"].string}")
|
Log.d("ANILIST RECOMMEND", "${rec["title"].obj["romaji"].string}")
|
||||||
SMangaImpl().apply {
|
SMangaImpl().apply {
|
||||||
this.title = rec["title"].obj["romaji"].string
|
this.title = rec["title"].obj["romaji"].nullString
|
||||||
|
?: rec["title"].obj["english"].nullString
|
||||||
|
?: 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
|
||||||
@ -155,6 +166,23 @@ open class RecommendsPager(val title: String, val preferredApi: API = API.MYANIM
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun requestNext(): Observable<MangasPage> {
|
override fun requestNext(): Observable<MangasPage> {
|
||||||
|
if (smart) {
|
||||||
|
val myAnimeListPoints = 0
|
||||||
|
val anilistPoints =
|
||||||
|
anilistSmart.count { manga.genre!!.toLowerCase(Locale.ROOT).contains(it) }
|
||||||
|
val apiPoints = listOf(
|
||||||
|
API.MYANIMELIST to myAnimeListPoints,
|
||||||
|
API.ANILIST to anilistPoints
|
||||||
|
).sortedWith(
|
||||||
|
compareBy(
|
||||||
|
{ (_, value) -> value },
|
||||||
|
{ (key, _) -> key == preferredApi }
|
||||||
|
)
|
||||||
|
)
|
||||||
|
preferredApi = apiPoints.last().first
|
||||||
|
Log.d("SMART RECOMMEND", preferredApi.toString())
|
||||||
|
}
|
||||||
|
|
||||||
val apiList = API.values().toMutableList()
|
val apiList = API.values().toMutableList()
|
||||||
apiList.removeAt(apiList.indexOf(preferredApi))
|
apiList.removeAt(apiList.indexOf(preferredApi))
|
||||||
apiList.add(0, preferredApi)
|
apiList.add(0, preferredApi)
|
||||||
@ -183,6 +211,8 @@ open class RecommendsPager(val title: String, val preferredApi: API = API.MYANIM
|
|||||||
companion object {
|
companion object {
|
||||||
private const val myAnimeListEndpoint = "https://api.jikan.moe/v3/"
|
private const val myAnimeListEndpoint = "https://api.jikan.moe/v3/"
|
||||||
private const val anilistEndpoint = "https://graphql.anilist.co/"
|
private const val anilistEndpoint = "https://graphql.anilist.co/"
|
||||||
|
private val anilistSmart =
|
||||||
|
listOf("manhua", "manhwa", "webtoon", "long strip", "korean", "chinese")
|
||||||
|
|
||||||
enum class API { MYANIMELIST, ANILIST }
|
enum class API { MYANIMELIST, ANILIST }
|
||||||
}
|
}
|
||||||
|
@ -437,7 +437,7 @@ class MangaAllInOneController :
|
|||||||
|
|
||||||
// AZ -->
|
// AZ -->
|
||||||
private fun openRecommends() {
|
private fun openRecommends() {
|
||||||
val recommendsConfig = BrowseSourceController.RecommendsConfig(presenter.manga.title, presenter.manga.source)
|
val recommendsConfig = BrowseSourceController.RecommendsConfig(presenter.manga)
|
||||||
|
|
||||||
router?.pushController(
|
router?.pushController(
|
||||||
BrowseSourceController(
|
BrowseSourceController(
|
||||||
|
@ -276,7 +276,7 @@ class MangaInfoController(private val fromSource: Boolean = false) :
|
|||||||
|
|
||||||
// AZ -->
|
// AZ -->
|
||||||
private fun openRecommends() {
|
private fun openRecommends() {
|
||||||
val recommendsConfig = BrowseSourceController.RecommendsConfig(presenter.manga.title, presenter.manga.source)
|
val recommendsConfig = BrowseSourceController.RecommendsConfig(presenter.manga)
|
||||||
|
|
||||||
parentController?.router?.pushController(
|
parentController?.router?.pushController(
|
||||||
BrowseSourceController(
|
BrowseSourceController(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user