From 99fd2731f5d9d374700e89fa67d4d5bf611bbafa Mon Sep 17 00:00:00 2001 From: AntsyLich <59261191+AntsyLich@users.noreply.github.com> Date: Thu, 18 Jan 2024 01:28:41 +0600 Subject: [PATCH] Fix score issue with MangaUpdates Also add custom user agent Potentially fix #17 --- .../data/track/mangaupdates/MangaUpdates.kt | 32 +++++++++++++------ .../mangaupdates/MangaUpdatesInterceptor.kt | 2 ++ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/mangaupdates/MangaUpdates.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/mangaupdates/MangaUpdates.kt index f5c33cf8d..24ad1a56b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/mangaupdates/MangaUpdates.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/mangaupdates/MangaUpdates.kt @@ -6,6 +6,8 @@ import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.models.Track import eu.kanade.tachiyomi.data.track.BaseTracker import eu.kanade.tachiyomi.data.track.DeletableTracker +import eu.kanade.tachiyomi.data.track.mangaupdates.dto.ListItem +import eu.kanade.tachiyomi.data.track.mangaupdates.dto.Rating import eu.kanade.tachiyomi.data.track.mangaupdates.dto.copyTo import eu.kanade.tachiyomi.data.track.mangaupdates.dto.toTrackSearch import eu.kanade.tachiyomi.data.track.model.TrackSearch @@ -23,10 +25,16 @@ class MangaUpdates(id: Long) : BaseTracker(id, "MangaUpdates"), DeletableTracker const val UNFINISHED_LIST = 3 const val ON_HOLD_LIST = 4 - private val SCORE_LIST = ( - (0..9) - .flatMap { i -> (0..9).map { j -> "$i.$j" } } + listOf("10.0") - ) + private val SCORE_LIST = (0..10) + .flatMap { decimal -> + when (decimal) { + 0 -> listOf("-") + 10 -> listOf("10.0") + else -> (0..9).map { fraction -> + "$decimal.$fraction" + } + } + } .toImmutableList() } @@ -59,7 +67,7 @@ class MangaUpdates(id: Long) : BaseTracker(id, "MangaUpdates"), DeletableTracker override fun getScoreList(): ImmutableList = SCORE_LIST - override fun indexToScore(index: Int): Float = SCORE_LIST[index].toFloat() + override fun indexToScore(index: Int): Float = if (index == 0) 0f else SCORE_LIST[index].toFloat() override fun displayScore(track: DomainTrack): String = track.score.toString() @@ -78,9 +86,9 @@ class MangaUpdates(id: Long) : BaseTracker(id, "MangaUpdates"), DeletableTracker override suspend fun bind(track: Track, hasReadChapters: Boolean): Track { return try { val (series, rating) = api.getSeriesListItem(track) - series.copyTo(track) - rating?.copyTo(track) ?: track + track.copyFrom(series, rating) } catch (e: Exception) { + track.score = 0f api.addSeriesToList(track, hasReadChapters) track } @@ -95,8 +103,12 @@ class MangaUpdates(id: Long) : BaseTracker(id, "MangaUpdates"), DeletableTracker override suspend fun refresh(track: Track): Track { val (series, rating) = api.getSeriesListItem(track) - series.copyTo(track) - return rating?.copyTo(track) ?: track + return track.copyFrom(series, rating) + } + + private fun Track.copyFrom(item: ListItem, rating: Rating?): Track = apply { + item.copyTo(this) + score = rating?.rating ?: 0f } override suspend fun login(username: String, password: String) { @@ -106,6 +118,6 @@ class MangaUpdates(id: Long) : BaseTracker(id, "MangaUpdates"), DeletableTracker } fun restoreSession(): String? { - return trackPreferences.trackPassword(this).get() + return trackPreferences.trackPassword(this).get().ifBlank { null } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/mangaupdates/MangaUpdatesInterceptor.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/mangaupdates/MangaUpdatesInterceptor.kt index 2b283c3b8..094471b18 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/mangaupdates/MangaUpdatesInterceptor.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/mangaupdates/MangaUpdatesInterceptor.kt @@ -1,5 +1,6 @@ package eu.kanade.tachiyomi.data.track.mangaupdates +import eu.kanade.tachiyomi.BuildConfig import okhttp3.Interceptor import okhttp3.Response import java.io.IOException @@ -18,6 +19,7 @@ class MangaUpdatesInterceptor( // Add the authorization header to the original request. val authRequest = originalRequest.newBuilder() .addHeader("Authorization", "Bearer $token") + .header("User-Agent", "Mihon v${BuildConfig.VERSION_NAME} (${BuildConfig.APPLICATION_ID})") .build() return chain.proceed(authRequest)