Skip to content

Commit

Permalink
fix kitsu tracking and other trackinf related fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jmir1 committed Jun 9, 2021
1 parent eff1bb5 commit 9e2db50
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -542,11 +542,16 @@ class AnilistApi(val client: OkHttpClient, interceptor: AnilistInterceptor) {
private const val apiUrl = "https://graphql.anilist.co/"
private const val baseUrl = "https://anilist.co/api/v2/"
private const val baseMangaUrl = "https://anilist.co/manga/"
private const val baseAnimeUrl = "https://anilist.co/anime/"

fun mangaUrl(mediaId: Int): String {
return baseMangaUrl + mediaId
}

fun animeUrl(mediaId: Int): String {
return baseAnimeUrl + mediaId
}

fun authUrl(): Uri = "${baseUrl}oauth/authorize".toUri().buildUpon()
.appendQueryParameter("client_id", clientId)
.appendQueryParameter("response_type", "token")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ data class ALAnime(
total_episodes = this@ALAnime.total_episodes
cover_url = image_url_lge
summary = description ?: ""
tracking_url = AnilistApi.mangaUrl(media_id)
tracking_url = AnilistApi.animeUrl(media_id)
publishing_status = this@ALAnime.publishing_status
publishing_type = type
if (start_date_fuzzy != 0L) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ class Kitsu(private val context: Context, id: Int) : TrackService(id) {

companion object {
const val READING = 1
const val WATCHING = 11
const val COMPLETED = 2
const val ON_HOLD = 3
const val DROPPED = 4
const val PLAN_TO_READ = 5
const val PLAN_TO_WATCH = 15
}

@StringRes
Expand All @@ -43,13 +45,15 @@ class Kitsu(private val context: Context, id: Int) : TrackService(id) {
}

override fun getStatusListAnime(): List<Int> {
return listOf(READING, PLAN_TO_READ, COMPLETED, ON_HOLD, DROPPED)
return listOf(WATCHING, PLAN_TO_WATCH, COMPLETED, ON_HOLD, DROPPED)
}

override fun getStatus(status: Int): String = with(context) {
when (status) {
READING -> getString(R.string.currently_reading)
WATCHING -> getString(R.string.currently_watching)
PLAN_TO_READ -> getString(R.string.want_to_read)
PLAN_TO_WATCH -> getString(R.string.want_to_watch)
COMPLETED -> getString(R.string.completed)
ON_HOLD -> getString(R.string.on_hold)
DROPPED -> getString(R.string.dropped)
Expand Down Expand Up @@ -114,7 +118,7 @@ class Kitsu(private val context: Context, id: Int) : TrackService(id) {
track.media_id = remoteTrack.media_id
update(track)
} else {
track.status = READING
track.status = WATCHING
track.score = 0F
add(track)
}
Expand Down
23 changes: 15 additions & 8 deletions app/src/main/java/eu/kanade/tachiyomi/data/track/kitsu/KitsuApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class KitsuApi(private val client: OkHttpClient, interceptor: KitsuInterceptor)
putJsonObject("media") {
putJsonObject("data") {
put("id", track.media_id)
put("type", "manga")
put("type", "anime")
}
}
}
Expand Down Expand Up @@ -245,7 +245,7 @@ class KitsuApi(private val client: OkHttpClient, interceptor: KitsuInterceptor)
private suspend fun algoliaSearchAnime(key: String, query: String): List<AnimeTrackSearch> {
return withIOContext {
val jsonObject = buildJsonObject {
put("params", "query=$query$algoliaFilter")
put("params", "query=$query$algoliaFilterAnime")
}

client.newCall(
Expand Down Expand Up @@ -295,17 +295,17 @@ class KitsuApi(private val client: OkHttpClient, interceptor: KitsuInterceptor)
suspend fun findLibAnime(track: AnimeTrack, userId: String): AnimeTrack? {
return withIOContext {
val url = "${baseUrl}library-entries".toUri().buildUpon()
.encodedQuery("filter[manga_id]=${track.media_id}&filter[user_id]=$userId")
.appendQueryParameter("include", "manga")
.encodedQuery("filter[anime_id]=${track.media_id}&filter[user_id]=$userId")
.appendQueryParameter("include", "anime")
.build()
authClient.newCall(GET(url.toString()))
.await()
.parseAs<JsonObject>()
.let {
val data = it["data"]!!.jsonArray
if (data.size > 0) {
val manga = it["included"]!!.jsonArray[0].jsonObject
KitsuLibAnime(data[0].jsonObject, manga).toTrack()
val anime = it["included"]!!.jsonArray[0].jsonObject
KitsuLibAnime(data[0].jsonObject, anime).toTrack()
} else {
null
}
Expand Down Expand Up @@ -338,7 +338,7 @@ class KitsuApi(private val client: OkHttpClient, interceptor: KitsuInterceptor)
return withIOContext {
val url = "${baseUrl}library-entries".toUri().buildUpon()
.encodedQuery("filter[id]=${track.media_id}")
.appendQueryParameter("include", "manga")
.appendQueryParameter("include", "anime")
.build()
authClient.newCall(GET(url.toString()))
.await()
Expand All @@ -349,7 +349,7 @@ class KitsuApi(private val client: OkHttpClient, interceptor: KitsuInterceptor)
val anime = it["included"]!!.jsonArray[0].jsonObject
KitsuLibAnime(data[0].jsonObject, anime).toTrack()
} else {
throw Exception("Could not find manga")
throw Exception("Could not find anime")
}
}
}
Expand Down Expand Up @@ -393,18 +393,25 @@ class KitsuApi(private val client: OkHttpClient, interceptor: KitsuInterceptor)
private const val baseUrl = "https://kitsu.io/api/edge/"
private const val loginUrl = "https://kitsu.io/api/oauth/token"
private const val baseMangaUrl = "https://kitsu.io/manga/"
private const val baseAnimeUrl = "https://kitsu.io/anime/"
private const val algoliaKeyUrl = "https://kitsu.io/api/edge/algolia-keys/media/"

private const val algoliaUrl =
"https://AWQO5J657S-dsn.algolia.net/1/indexes/production_media/query/"
private const val algoliaAppId = "AWQO5J657S"
private const val algoliaFilter =
"&facetFilters=%5B%22kind%3Amanga%22%5D&attributesToRetrieve=%5B%22synopsis%22%2C%22canonicalTitle%22%2C%22chapterCount%22%2C%22posterImage%22%2C%22startDate%22%2C%22subtype%22%2C%22endDate%22%2C%20%22id%22%5D"
private const val algoliaFilterAnime =
"&facetFilters=%5B%22kind%3Aanime%22%5D&attributesToRetrieve=%5B%22synopsis%22%2C%22canonicalTitle%22%2C%22episodeCount%22%2C%22posterImage%22%2C%22startDate%22%2C%22subtype%22%2C%22endDate%22%2C%20%22id%22%5D"

fun mangaUrl(remoteId: Int): String {
return baseMangaUrl + remoteId
}

fun animeUrl(remoteId: Int): String {
return baseAnimeUrl + remoteId
}

fun refreshTokenRequest(token: String) = POST(
loginUrl,
body = FormBody.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class KitsuSearchManga(obj: JsonObject) {
class KitsuSearchAnime(obj: JsonObject) {
val id = obj["id"]!!.jsonPrimitive.int
private val canonicalTitle = obj["canonicalTitle"]!!.jsonPrimitive.content
private val chapterCount = obj["chapterCount"]?.jsonPrimitive?.intOrNull
private val episodeCount = obj["episodeCount"]?.jsonPrimitive?.intOrNull
val subType = obj["subtype"]?.jsonPrimitive?.contentOrNull
val original = try {
obj["posterImage"]?.jsonObject?.get("original")?.jsonPrimitive?.content
Expand All @@ -74,10 +74,10 @@ class KitsuSearchAnime(obj: JsonObject) {
fun toTrack() = AnimeTrackSearch.create(TrackManager.KITSU).apply {
media_id = this@KitsuSearchAnime.id
title = canonicalTitle
total_episodes = chapterCount ?: 0
total_episodes = episodeCount ?: 0
cover_url = original ?: ""
summary = synopsis
tracking_url = KitsuApi.mangaUrl(media_id)
tracking_url = KitsuApi.animeUrl(media_id)
publishing_status = if (endDate == null) {
"Publishing"
} else {
Expand Down Expand Up @@ -130,7 +130,7 @@ class KitsuLibAnime(obj: JsonObject, anime: JsonObject) {
val id = anime["id"]!!.jsonPrimitive.int
private val canonicalTitle = anime["attributes"]!!.jsonObject["canonicalTitle"]!!.jsonPrimitive.content
private val episodeCount = anime["attributes"]!!.jsonObject["episodeCount"]?.jsonPrimitive?.intOrNull
val type = anime["attributes"]!!.jsonObject["animeType"]?.jsonPrimitive?.contentOrNull.orEmpty()
val type = anime["attributes"]!!.jsonObject["subtype"]?.jsonPrimitive?.contentOrNull.orEmpty()
val original = anime["attributes"]!!.jsonObject["posterImage"]!!.jsonObject["original"]!!.jsonPrimitive.content
private val synopsis = anime["attributes"]!!.jsonObject["synopsis"]!!.jsonPrimitive.content
private val startDate = anime["attributes"]!!.jsonObject["startDate"]?.jsonPrimitive?.contentOrNull.orEmpty()
Expand All @@ -145,7 +145,7 @@ class KitsuLibAnime(obj: JsonObject, anime: JsonObject) {
total_episodes = episodeCount ?: 0
cover_url = original
summary = synopsis
tracking_url = KitsuApi.mangaUrl(media_id)
tracking_url = KitsuApi.animeUrl(media_id)
publishing_status = this@KitsuLibAnime.status
publishing_type = type
start_date = startDate
Expand All @@ -155,11 +155,11 @@ class KitsuLibAnime(obj: JsonObject, anime: JsonObject) {
}

private fun toTrackStatus() = when (status) {
"current" -> Kitsu.READING
"current" -> Kitsu.WATCHING
"completed" -> Kitsu.COMPLETED
"on_hold" -> Kitsu.ON_HOLD
"dropped" -> Kitsu.DROPPED
"planned" -> Kitsu.PLAN_TO_READ
"planned" -> Kitsu.PLAN_TO_WATCH
else -> throw Exception("Unknown status")
}
}
Expand All @@ -178,11 +178,11 @@ fun Track.toKitsuScore(): String? {
}

fun AnimeTrack.toKitsuStatus() = when (status) {
Kitsu.READING -> "current"
Kitsu.WATCHING -> "current"
Kitsu.COMPLETED -> "completed"
Kitsu.ON_HOLD -> "on_hold"
Kitsu.DROPPED -> "dropped"
Kitsu.PLAN_TO_READ -> "planned"
Kitsu.PLAN_TO_WATCH -> "planned"
else -> throw Exception("Unknown status")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI
summary = obj["synopsis"]?.jsonPrimitive?.content ?: ""
total_episodes = obj["num_episodes"]!!.jsonPrimitive.int
cover_url = obj["main_picture"]?.jsonObject?.get("large")?.jsonPrimitive?.content ?: ""
tracking_url = "https://myanimelist.net/manga/$media_id"
tracking_url = "https://myanimelist.net/anime/$media_id"
publishing_status = obj["status"]!!.jsonPrimitive.content.replace("_", " ")
publishing_type = obj["media_type"]!!.jsonPrimitive.content.replace("_", " ")
start_date = try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.preference.PreferenceScreen
import com.afollestad.materialdialogs.MaterialDialog
import eu.kanade.tachiyomi.BuildConfig
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.animelib.AnimelibUpdateService
import eu.kanade.tachiyomi.data.cache.ChapterCache
import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.library.LibraryUpdateService
Expand Down Expand Up @@ -154,14 +155,20 @@ class SettingsAdvancedController : SettingsController() {
key = "pref_refresh_library_covers"
titleRes = R.string.pref_refresh_library_covers

onClick { LibraryUpdateService.start(context, target = Target.COVERS) }
onClick {
LibraryUpdateService.start(context.createAttributionContext("manga"), target = Target.COVERS)
AnimelibUpdateService.start(context.createAttributionContext("anime"), target = AnimelibUpdateService.Target.COVERS)
}
}
preference {
key = "pref_refresh_library_tracking"
titleRes = R.string.pref_refresh_library_tracking
summaryRes = R.string.pref_refresh_library_tracking_summary

onClick { LibraryUpdateService.start(context, target = Target.TRACKING) }
onClick {
LibraryUpdateService.start(context.createAttributionContext("manga"), target = Target.TRACKING)
AnimelibUpdateService.start(context.createAttributionContext("anime"), target = AnimelibUpdateService.Target.TRACKING)
}
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,9 @@
<string name="pref_clear_database_summary">Delete history for manga that are not saved in your library</string>
<string name="clear_database_confirmation">Are you sure? Read chapters and progress of non-library manga will be lost</string>
<string name="clear_database_completed">Entries deleted</string>
<string name="pref_refresh_library_covers">Refresh library manga covers</string>
<string name="pref_refresh_library_covers">Refresh library covers</string>
<string name="pref_refresh_library_tracking">Refresh tracking</string>
<string name="pref_refresh_library_tracking_summary">Updates status, score and last chapter read from the tracking services</string>
<string name="pref_refresh_library_tracking_summary">Updates status, score and progress from the tracking services</string>
<string name="pref_dump_crash_logs">Dump crash logs</string>
<string name="pref_dump_crash_logs_summary">Saves error logs to a file for sharing with the developers</string>
<string name="crash_log_saved">Crash logs saved</string>
Expand Down Expand Up @@ -650,6 +650,7 @@
<string name="plan_to_read">Plan to read</string>
<string name="plan_to_watch">Plan to watch</string>
<string name="want_to_read">Want to read</string>
<string name="want_to_watch">Want to watch</string>
<string name="repeating">Rereading</string>
<string name="repeating_anime">Rewatching</string>
<string name="score">Score</string>
Expand Down

0 comments on commit 9e2db50

Please sign in to comment.