This commit is contained in:
Jays2Kings 2021-03-26 13:37:11 -04:00
commit bbe0a86573
2 changed files with 47 additions and 22 deletions

View File

@ -92,6 +92,12 @@ class Shikimori(private val context: Context, id: Int) : TrackService(id) {
} }
} }
override fun canRemoveFromService(): Boolean = true
override suspend fun removeFromService(track: Track): Boolean {
return api.remove(track, getUsername())
}
override suspend fun search(query: String) = api.search(query) override suspend fun search(query: String) = api.search(query)
override suspend fun refresh(track: Track): Track { override suspend fun refresh(track: Track): Track {
@ -107,17 +113,17 @@ class Shikimori(private val context: Context, id: Int) : TrackService(id) {
override suspend fun login(username: String, password: String) = login(password) override suspend fun login(username: String, password: String) = login(password)
suspend fun login(code: String): Boolean { suspend fun login(code: String): Boolean {
try { return try {
val oauth = api.accessToken(code) val oauth = api.accessToken(code)
interceptor.newAuth(oauth) interceptor.newAuth(oauth)
val user = api.getCurrentUser() val user = api.getCurrentUser()
saveCredentials(user.toString(), oauth.access_token) saveCredentials(user.toString(), oauth.access_token)
return true true
} catch (e: java.lang.Exception) { } catch (e: java.lang.Exception) {
Timber.e(e) Timber.e(e)
logout() logout()
return false false
} }
} }

View File

@ -2,10 +2,12 @@ package eu.kanade.tachiyomi.data.track.shikimori
import androidx.core.net.toUri import androidx.core.net.toUri
import com.github.salomonbrys.kotson.array import com.github.salomonbrys.kotson.array
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.nullString
import com.github.salomonbrys.kotson.obj import com.github.salomonbrys.kotson.obj
import com.google.gson.Gson import com.google.gson.Gson
import com.google.gson.JsonArray
import com.google.gson.JsonObject import com.google.gson.JsonObject
import com.google.gson.JsonParser import com.google.gson.JsonParser
import eu.kanade.tachiyomi.data.database.models.Track import eu.kanade.tachiyomi.data.database.models.Track
@ -20,6 +22,7 @@ import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.Request import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody import okhttp3.RequestBody.Companion.toRequestBody
import timber.log.Timber
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
class ShikimoriApi(private val client: OkHttpClient, interceptor: ShikimoriInterceptor) { class ShikimoriApi(private val client: OkHttpClient, interceptor: ShikimoriInterceptor) {
@ -68,6 +71,22 @@ class ShikimoriApi(private val client: OkHttpClient, interceptor: ShikimoriInter
} }
} }
suspend fun remove(track: Track, user_id: String): Boolean {
return withContext(Dispatchers.IO) {
try {
val rates = getUserRates(track, user_id)
val id = rates.last()["id"]
val url = "$apiUrl/v2/user_rates/$id"
val request = Request.Builder().url(url).delete().build()
authClient.newCall(request).execute()
return@withContext true
} catch (e: Exception) {
Timber.w(e)
}
return@withContext false
}
}
private fun jsonToSearch(obj: JsonObject): TrackSearch { private fun jsonToSearch(obj: JsonObject): TrackSearch {
return TrackSearch.create(TrackManager.SHIKIMORI).apply { return TrackSearch.create(TrackManager.SHIKIMORI).apply {
media_id = obj["id"].asInt media_id = obj["id"].asInt
@ -94,14 +113,24 @@ class ShikimoriApi(private val client: OkHttpClient, interceptor: ShikimoriInter
} }
} }
suspend fun findLibManga(track: Track, user_id: String): Track? { private fun getUserRates(track: Track, user_id: String): JsonArray {
return withContext(Dispatchers.IO) {
val url = "$apiUrl/v2/user_rates".toUri().buildUpon() val url = "$apiUrl/v2/user_rates".toUri().buildUpon()
.appendQueryParameter("user_id", user_id) .appendQueryParameter("user_id", user_id)
.appendQueryParameter("target_id", track.media_id.toString()) .appendQueryParameter("target_id", track.media_id.toString())
.appendQueryParameter("target_type", "Manga").build() .appendQueryParameter("target_type", "Manga").build()
val request = Request.Builder().url(url.toString()).get().build() val request = Request.Builder().url(url.toString()).get().build()
val requestResponse = authClient.newCall(request).execute()
val requestResponseBody = requestResponse.body?.string().orEmpty()
if (requestResponseBody.isEmpty()) {
throw Exception("Null Response")
}
return JsonParser.parseString(requestResponseBody).array
}
suspend fun findLibManga(track: Track, user_id: String): Track? {
return withContext(Dispatchers.IO) {
val urlMangas = "$apiUrl/mangas".toUri().buildUpon().appendPath(track.media_id.toString()) val urlMangas = "$apiUrl/mangas".toUri().buildUpon().appendPath(track.media_id.toString())
.build() .build()
val requestMangas = Request.Builder().url(urlMangas.toString()).get().build() val requestMangas = Request.Builder().url(urlMangas.toString()).get().build()
@ -110,20 +139,10 @@ class ShikimoriApi(private val client: OkHttpClient, interceptor: ShikimoriInter
val requestMangasBody = requestMangasResponse.body?.string().orEmpty() val requestMangasBody = requestMangasResponse.body?.string().orEmpty()
val mangas = JsonParser.parseString(requestMangasBody).obj val mangas = JsonParser.parseString(requestMangasBody).obj
val requestResponse = authClient.newCall(request).execute() val entry = getUserRates(track, user_id)
val requestResponseBody = requestResponse.body?.string().orEmpty() return@withContext entry.map {
if (requestResponseBody.isEmpty()) {
throw Exception("Null Response")
}
val response = JsonParser.parseString(requestResponseBody).array
if (response.size() > 1) {
throw Exception("Too much mangas in response")
}
val entry = response.map {
jsonToTrack(it.obj, mangas) jsonToTrack(it.obj, mangas)
} }.firstOrNull()
entry.firstOrNull()
} }
} }