From 5840a3e1e2388c3f2f77ef22f6620282e13186b9 Mon Sep 17 00:00:00 2001 From: Pavka Date: Mon, 29 Apr 2019 19:40:26 +0300 Subject: [PATCH] Shikomori -> Shikimori. Fix update chapters (#1996) * Shikomori -> Shikimori. Fix update chapters * Removed logs and format code --- app/src/main/AndroidManifest.xml | 4 +- .../tachiyomi/data/track/TrackManager.kt | 8 +-- .../track/{shikomori => shikimori}/OAuth.kt | 2 +- .../Shikomori.kt => shikimori/Shikimori.kt} | 13 ++-- .../ShikimoriApi.kt} | 56 +++++++++++------- .../ShikimoriInterceptor.kt} | 12 ++-- .../data/track/shikimori/ShikimoriModels.kt | 24 ++++++++ .../data/track/shikomori/ShikomoriModels.kt | 24 -------- .../ui/setting/SettingsTrackingController.kt | 8 +-- .../ui/setting/ShikomoriLoginActivity.kt | 6 +- .../{shikomori.png => shikimori.png} | Bin 11 files changed, 87 insertions(+), 70 deletions(-) rename app/src/main/java/eu/kanade/tachiyomi/data/track/{shikomori => shikimori}/OAuth.kt (83%) rename app/src/main/java/eu/kanade/tachiyomi/data/track/{shikomori/Shikomori.kt => shikimori/Shikimori.kt} (89%) rename app/src/main/java/eu/kanade/tachiyomi/data/track/{shikomori/ShikomoriApi.kt => shikimori/ShikimoriApi.kt} (77%) rename app/src/main/java/eu/kanade/tachiyomi/data/track/{shikomori/ShikomoriInterceptor.kt => shikimori/ShikimoriInterceptor.kt} (79%) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/ShikimoriModels.kt delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/track/shikomori/ShikomoriModels.kt rename app/src/main/res/drawable-xxxhdpi/{shikomori.png => shikimori.png} (100%) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index feddf2096..eff4c7057 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -62,8 +62,8 @@ + android:name=".ui.setting.ShikimoriLoginActivity" + android:label="Shikimori"> diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/TrackManager.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/TrackManager.kt index 14558d1f1..70d669af5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/TrackManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/TrackManager.kt @@ -4,7 +4,7 @@ import android.content.Context import eu.kanade.tachiyomi.data.track.anilist.Anilist import eu.kanade.tachiyomi.data.track.kitsu.Kitsu import eu.kanade.tachiyomi.data.track.myanimelist.Myanimelist -import eu.kanade.tachiyomi.data.track.shikomori.Shikomori +import eu.kanade.tachiyomi.data.track.shikimori.Shikimori class TrackManager(private val context: Context) { @@ -12,7 +12,7 @@ class TrackManager(private val context: Context) { const val MYANIMELIST = 1 const val ANILIST = 2 const val KITSU = 3 - const val SHIKOMORI = 4 + const val SHIKIMORI = 4 } val myAnimeList = Myanimelist(context, MYANIMELIST) @@ -21,9 +21,9 @@ class TrackManager(private val context: Context) { val kitsu = Kitsu(context, KITSU) - val shikomori = Shikomori(context, SHIKOMORI) + val shikimori = Shikimori(context, SHIKIMORI) - val services = listOf(myAnimeList, aniList, kitsu, shikomori) + val services = listOf(myAnimeList, aniList, kitsu, shikimori) fun getService(id: Int) = services.find { it.id == id } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/shikomori/OAuth.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/OAuth.kt similarity index 83% rename from app/src/main/java/eu/kanade/tachiyomi/data/track/shikomori/OAuth.kt rename to app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/OAuth.kt index ad6adc18a..118e584e7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/shikomori/OAuth.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/OAuth.kt @@ -1,4 +1,4 @@ -package eu.kanade.tachiyomi.data.track.shikomori +package eu.kanade.tachiyomi.data.track.shikimori data class OAuth( val access_token: String, diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/shikomori/Shikomori.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/Shikimori.kt similarity index 89% rename from app/src/main/java/eu/kanade/tachiyomi/data/track/shikomori/Shikomori.kt rename to app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/Shikimori.kt index 83fee74cf..8068e6d55 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/shikomori/Shikomori.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/Shikimori.kt @@ -1,7 +1,8 @@ -package eu.kanade.tachiyomi.data.track.shikomori +package eu.kanade.tachiyomi.data.track.shikimori import android.content.Context import android.graphics.Color +import android.util.Log import com.google.gson.Gson import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.models.Track @@ -11,7 +12,7 @@ import rx.Completable import rx.Observable import uy.kohesive.injekt.injectLazy -class Shikomori(private val context: Context, id: Int) : TrackService(id) { +class Shikimori(private val context: Context, id: Int) : TrackService(id) { override fun getScoreList(): List { return IntRange(0, 10).map(Int::toString) @@ -75,15 +76,15 @@ class Shikomori(private val context: Context, id: Int) : TrackService(id) { const val DEFAULT_SCORE = 0 } - override val name = "Shikomori" + override val name = "Shikimori" private val gson: Gson by injectLazy() - private val interceptor by lazy { ShikomoriInterceptor(this, gson) } + private val interceptor by lazy { ShikimoriInterceptor(this, gson) } - private val api by lazy { ShikomoriApi(client, interceptor) } + private val api by lazy { ShikimoriApi(client, interceptor) } - override fun getLogo() = R.drawable.shikomori + override fun getLogo() = R.drawable.shikimori override fun getLogoColor() = Color.rgb(40, 40, 40) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/shikomori/ShikomoriApi.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/ShikimoriApi.kt similarity index 77% rename from app/src/main/java/eu/kanade/tachiyomi/data/track/shikomori/ShikomoriApi.kt rename to app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/ShikimoriApi.kt index 2df1eae63..35dbb532b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/shikomori/ShikomoriApi.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/ShikimoriApi.kt @@ -1,4 +1,4 @@ -package eu.kanade.tachiyomi.data.track.shikomori +package eu.kanade.tachiyomi.data.track.shikimori import android.net.Uri import com.github.salomonbrys.kotson.array @@ -18,7 +18,7 @@ import okhttp3.* import rx.Observable import uy.kohesive.injekt.injectLazy -class ShikomoriApi(private val client: OkHttpClient, interceptor: ShikomoriInterceptor) { +class ShikimoriApi(private val client: OkHttpClient, interceptor: ShikimoriInterceptor) { private val gson: Gson by injectLazy() private val parser = JsonParser() @@ -33,7 +33,7 @@ class ShikomoriApi(private val client: OkHttpClient, interceptor: ShikomoriInter "target_type" to "Manga", "chapters" to track.last_chapter_read, "score" to track.score.toInt(), - "status" to track.toShikomoriStatus() + "status" to track.toShikimoriStatus() ) ) val body = RequestBody.create(jsonime, payload.toString()) @@ -74,7 +74,7 @@ class ShikomoriApi(private val client: OkHttpClient, interceptor: ShikomoriInter } private fun jsonToSearch(obj: JsonObject): TrackSearch { - return TrackSearch.create(TrackManager.SHIKOMORI).apply { + return TrackSearch.create(TrackManager.SHIKIMORI).apply { media_id = obj["id"].asInt title = obj["name"].asString total_chapters = obj["chapters"].asInt @@ -87,14 +87,15 @@ class ShikomoriApi(private val client: OkHttpClient, interceptor: ShikomoriInter } } - private fun jsonToTrack(obj: JsonObject): Track { - return Track.create(TrackManager.SHIKOMORI).apply { + private fun jsonToTrack(obj: JsonObject, mangas: JsonObject): Track { + return Track.create(TrackManager.SHIKIMORI).apply { + title = mangas["name"].asString media_id = obj["id"].asInt - title = "" + total_chapters = mangas["chapters"].asInt last_chapter_read = obj["chapters"].asInt - total_chapters = obj["chapters"].asInt score = (obj["score"].asInt).toFloat() status = toTrackStatus(obj["status"].asString) + tracking_url = baseUrl + mangas["url"].asString } } @@ -108,21 +109,36 @@ class ShikomoriApi(private val client: OkHttpClient, interceptor: ShikomoriInter .url(url.toString()) .get() .build() - return authClient.newCall(request) + + val urlMangas = Uri.parse("$apiUrl/mangas").buildUpon() + .appendPath(track.media_id.toString()) + .build() + val requestMangas = Request.Builder() + .url(urlMangas.toString()) + .get() + .build() + return authClient.newCall(requestMangas) .asObservableSuccess() .map { netResponse -> val responseBody = netResponse.body()?.string().orEmpty() - if (responseBody.isEmpty()) { - throw Exception("Null Response") - } - val response = parser.parse(responseBody).array - if (response.size() > 1) { - throw Exception("Too much mangas in response") - } - val entry = response.map { - jsonToTrack(it.obj) - } - entry.firstOrNull() + parser.parse(responseBody).obj + }.flatMap { mangas -> + authClient.newCall(request) + .asObservableSuccess() + .map { netResponse -> + val responseBody = netResponse.body()?.string().orEmpty() + if (responseBody.isEmpty()) { + throw Exception("Null Response") + } + val response = parser.parse(responseBody).array + if (response.size() > 1) { + throw Exception("Too much mangas in response") + } + val entry = response.map { + jsonToTrack(it.obj, mangas) + } + entry.firstOrNull() + } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/shikomori/ShikomoriInterceptor.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/ShikimoriInterceptor.kt similarity index 79% rename from app/src/main/java/eu/kanade/tachiyomi/data/track/shikomori/ShikomoriInterceptor.kt rename to app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/ShikimoriInterceptor.kt index e46e7cfb4..154020727 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/shikomori/ShikomoriInterceptor.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/ShikimoriInterceptor.kt @@ -1,26 +1,26 @@ -package eu.kanade.tachiyomi.data.track.shikomori +package eu.kanade.tachiyomi.data.track.shikimori import com.google.gson.Gson import okhttp3.Interceptor import okhttp3.Response -class ShikomoriInterceptor(val shikomori: Shikomori, val gson: Gson) : Interceptor { +class ShikimoriInterceptor(val shikimori: Shikimori, val gson: Gson) : Interceptor { /** * OAuth object used for authenticated requests. */ - private var oauth: OAuth? = shikomori.restoreToken() + private var oauth: OAuth? = shikimori.restoreToken() override fun intercept(chain: Interceptor.Chain): Response { val originalRequest = chain.request() - val currAuth = oauth ?: throw Exception("Not authenticated with Shikomori") + val currAuth = oauth ?: throw Exception("Not authenticated with Shikimori") val refreshToken = currAuth.refresh_token!! // Refresh access token if expired. if (currAuth.isExpired()) { - val response = chain.proceed(ShikomoriApi.refreshTokenRequest(refreshToken)) + val response = chain.proceed(ShikimoriApi.refreshTokenRequest(refreshToken)) if (response.isSuccessful) { newAuth(gson.fromJson(response.body()!!.string(), OAuth::class.java)) } else { @@ -38,6 +38,6 @@ class ShikomoriInterceptor(val shikomori: Shikomori, val gson: Gson) : Intercept fun newAuth(oauth: OAuth?) { this.oauth = oauth - shikomori.saveToken(oauth) + shikimori.saveToken(oauth) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/ShikimoriModels.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/ShikimoriModels.kt new file mode 100644 index 000000000..91e556bdd --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/ShikimoriModels.kt @@ -0,0 +1,24 @@ +package eu.kanade.tachiyomi.data.track.shikimori + +import eu.kanade.tachiyomi.data.database.models.Track + +fun Track.toShikimoriStatus() = when (status) { + Shikimori.READING -> "watching" + Shikimori.COMPLETED -> "completed" + Shikimori.ON_HOLD -> "on_hold" + Shikimori.DROPPED -> "dropped" + Shikimori.PLANNING -> "planned" + Shikimori.REPEATING -> "rewatching" + else -> throw NotImplementedError("Unknown status") +} + +fun toTrackStatus(status: String) = when (status) { + "watching" -> Shikimori.READING + "completed" -> Shikimori.COMPLETED + "on_hold" -> Shikimori.ON_HOLD + "dropped" -> Shikimori.DROPPED + "planned" -> Shikimori.PLANNING + "rewatching" -> Shikimori.REPEATING + + else -> throw Exception("Unknown status") +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/shikomori/ShikomoriModels.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/shikomori/ShikomoriModels.kt deleted file mode 100644 index d66f20649..000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/shikomori/ShikomoriModels.kt +++ /dev/null @@ -1,24 +0,0 @@ -package eu.kanade.tachiyomi.data.track.shikomori - -import eu.kanade.tachiyomi.data.database.models.Track - -fun Track.toShikomoriStatus() = when (status) { - Shikomori.READING -> "watching" - Shikomori.COMPLETED -> "completed" - Shikomori.ON_HOLD -> "on_hold" - Shikomori.DROPPED -> "dropped" - Shikomori.PLANNING -> "planned" - Shikomori.REPEATING -> "rewatching" - else -> throw NotImplementedError("Unknown status") -} - -fun toTrackStatus(status: String) = when (status) { - "watching" -> Shikomori.READING - "completed" -> Shikomori.COMPLETED - "on_hold" -> Shikomori.ON_HOLD - "dropped" -> Shikomori.DROPPED - "planned" -> Shikomori.PLANNING - "rewatching" -> Shikomori.REPEATING - - else -> throw Exception("Unknown status") -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsTrackingController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsTrackingController.kt index 250289cc1..2a8f1f73a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsTrackingController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsTrackingController.kt @@ -8,7 +8,7 @@ import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.data.track.TrackService import eu.kanade.tachiyomi.data.track.anilist.AnilistApi -import eu.kanade.tachiyomi.data.track.shikomori.ShikomoriApi +import eu.kanade.tachiyomi.data.track.shikimori.ShikimoriApi import eu.kanade.tachiyomi.util.getResourceColor import eu.kanade.tachiyomi.widget.preference.LoginPreference import eu.kanade.tachiyomi.widget.preference.TrackLoginDialog @@ -54,13 +54,13 @@ class SettingsTrackingController : SettingsController(), dialog.showDialog(router) } } - trackPreference(trackManager.shikomori) { + trackPreference(trackManager.shikimori) { onClick { val tabsIntent = CustomTabsIntent.Builder() .setToolbarColor(context.getResourceColor(R.attr.colorPrimary)) .build() tabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY) - tabsIntent.launchUrl(activity, ShikomoriApi.authUrl()) + tabsIntent.launchUrl(activity, ShikimoriApi.authUrl()) } } } @@ -80,7 +80,7 @@ class SettingsTrackingController : SettingsController(), super.onActivityResumed(activity) // Manually refresh anilist holder updatePreference(trackManager.aniList.id) - updatePreference(trackManager.shikomori.id) + updatePreference(trackManager.shikimori.id) } private fun updatePreference(id: Int) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/ShikomoriLoginActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/ShikomoriLoginActivity.kt index 6c3ba6f83..d369896ed 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/ShikomoriLoginActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/ShikomoriLoginActivity.kt @@ -13,7 +13,7 @@ import rx.android.schedulers.AndroidSchedulers import rx.schedulers.Schedulers import uy.kohesive.injekt.injectLazy -class ShikomoriLoginActivity : AppCompatActivity() { +class ShikimoriLoginActivity : AppCompatActivity() { private val trackManager: TrackManager by injectLazy() @@ -25,7 +25,7 @@ class ShikomoriLoginActivity : AppCompatActivity() { val code = intent.data?.getQueryParameter("code") if (code != null) { - trackManager.shikomori.login(code) + trackManager.shikimori.login(code) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe({ @@ -34,7 +34,7 @@ class ShikomoriLoginActivity : AppCompatActivity() { returnToSettings() }) } else { - trackManager.shikomori.logout() + trackManager.shikimori.logout() returnToSettings() } } diff --git a/app/src/main/res/drawable-xxxhdpi/shikomori.png b/app/src/main/res/drawable-xxxhdpi/shikimori.png similarity index 100% rename from app/src/main/res/drawable-xxxhdpi/shikomori.png rename to app/src/main/res/drawable-xxxhdpi/shikimori.png