Use own client for trackers + custom user agents

Closes #114
Closes #143

Co-authored-by: Med <45147847+kitsumed@users.noreply.github.com>
This commit is contained in:
AntsyLich 2024-01-22 16:17:08 +06:00
parent 9fd1419142
commit e3f33e24f5
No known key found for this signature in database
9 changed files with 42 additions and 25 deletions

View file

@ -1,5 +1,6 @@
package eu.kanade.tachiyomi.data.track.anilist
import eu.kanade.tachiyomi.BuildConfig
import okhttp3.Interceptor
import okhttp3.Response
import java.io.IOException
@ -40,6 +41,7 @@ class AnilistInterceptor(val anilist: Anilist, private var token: String?) : Int
// Add the authorization header to the original request.
val authRequest = originalRequest.newBuilder()
.addHeader("Authorization", "Bearer ${oauth!!.access_token}")
.header("User-Agent", "Mihon v${BuildConfig.VERSION_NAME} (${BuildConfig.APPLICATION_ID})")
.build()
return chain.proceed(authRequest)

View file

@ -182,8 +182,8 @@ class BangumiApi(
)
companion object {
private const val clientId = "bgm10555cda0762e80ca"
private const val clientSecret = "8fff394a8627b4c388cbf349ec865775"
private const val clientId = "bgm291665acbd06a4c28"
private const val clientSecret = "43e5ce36b207de16e5d3cfd3e79118db"
private const val apiUrl = "https://api.bgm.tv"
private const val oauthUrl = "https://bgm.tv/oauth/access_token"

View file

@ -1,8 +1,10 @@
package eu.kanade.tachiyomi.data.track.bangumi
import eu.kanade.tachiyomi.BuildConfig
import kotlinx.serialization.json.Json
import okhttp3.FormBody
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
import uy.kohesive.injekt.injectLazy
@ -29,22 +31,20 @@ class BangumiInterceptor(private val bangumi: Bangumi) : Interceptor {
}
}
val authRequest = if (originalRequest.method == "GET") {
originalRequest.newBuilder()
.header("User-Agent", "Tachiyomi")
.url(
originalRequest.url.newBuilder()
.addQueryParameter("access_token", currAuth.access_token).build(),
)
.build()
} else {
originalRequest.newBuilder()
.post(addToken(currAuth.access_token, originalRequest.body as FormBody))
.header("User-Agent", "Tachiyomi")
.build()
}
return chain.proceed(authRequest)
return originalRequest.newBuilder()
.header("User-Agent", "antsylich/Mihon/v${BuildConfig.VERSION_NAME} (Android) (http://github.com/mihonapp/mihon)")
.apply {
if (originalRequest.method == "GET") {
val newUrl = originalRequest.url.newBuilder()
.addQueryParameter("access_token", currAuth.access_token)
.build()
url(newUrl)
} else {
post(addToken(currAuth.access_token, originalRequest.body as FormBody))
}
}
.build()
.let(chain::proceed)
}
fun newAuth(oauth: OAuth?) {

View file

@ -18,7 +18,7 @@ class KavitaInterceptor(private val kavita: Kavita) : Interceptor {
// Add the authorization header to the original request.
val authRequest = originalRequest.newBuilder()
.addHeader("Authorization", "Bearer $jwtToken")
.header("User-Agent", "Tachiyomi Kavita v${BuildConfig.VERSION_NAME}")
.header("User-Agent", "Mihon v${BuildConfig.VERSION_NAME} (${BuildConfig.APPLICATION_ID})")
.build()
return chain.proceed(authRequest)

View file

@ -1,5 +1,6 @@
package eu.kanade.tachiyomi.data.track.kitsu
import eu.kanade.tachiyomi.BuildConfig
import kotlinx.serialization.json.Json
import okhttp3.Interceptor
import okhttp3.Response
@ -34,6 +35,7 @@ class KitsuInterceptor(private val kitsu: Kitsu) : Interceptor {
// Add the authorization header to the original request.
val authRequest = originalRequest.newBuilder()
.addHeader("Authorization", "Bearer ${oauth!!.access_token}")
.header("User-Agent", "Mihon v${BuildConfig.VERSION_NAME} (${BuildConfig.APPLICATION_ID})")
.header("Accept", "application/vnd.api+json")
.header("Content-Type", "application/vnd.api+json")
.build()

View file

@ -1,5 +1,6 @@
package eu.kanade.tachiyomi.data.track.komga
import eu.kanade.tachiyomi.BuildConfig
import eu.kanade.tachiyomi.data.database.models.Track
import eu.kanade.tachiyomi.data.track.model.TrackSearch
import eu.kanade.tachiyomi.network.GET
@ -8,6 +9,7 @@ import eu.kanade.tachiyomi.network.parseAs
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import logcat.LogPriority
import okhttp3.Headers
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
@ -23,6 +25,12 @@ class KomgaApi(
private val client: OkHttpClient,
) {
private val headers: Headers by lazy {
Headers.Builder()
.add("User-Agent", "Mihon v${BuildConfig.VERSION_NAME} (${BuildConfig.APPLICATION_ID})")
.build()
}
private val json: Json by injectLazy()
suspend fun getTrackSearch(url: String): TrackSearch =
@ -30,12 +38,12 @@ class KomgaApi(
try {
val track = with(json) {
if (url.contains(READLIST_API)) {
client.newCall(GET(url))
client.newCall(GET(url, headers))
.awaitSuccess()
.parseAs<ReadListDto>()
.toTrack()
} else {
client.newCall(GET(url))
client.newCall(GET(url, headers))
.awaitSuccess()
.parseAs<SeriesDto>()
.toTrack()
@ -43,7 +51,7 @@ class KomgaApi(
}
val progress = client
.newCall(GET("${url.replace("/api/v1/series/", "/api/v2/series/")}/read-progress/tachiyomi"))
.newCall(GET("${url.replace("/api/v1/series/", "/api/v2/series/")}/read-progress/tachiyomi", headers))
.awaitSuccess().let {
with(json) {
if (url.contains("/api/v1/series/")) {
@ -80,6 +88,7 @@ class KomgaApi(
client.newCall(
Request.Builder()
.url("${track.tracking_url.replace("/api/v1/series/", "/api/v2/series/")}/read-progress/tachiyomi")
.headers(headers)
.put(payload.toRequestBody("application/json".toMediaType()))
.build(),
)

View file

@ -1,5 +1,6 @@
package eu.kanade.tachiyomi.data.track.myanimelist
import eu.kanade.tachiyomi.BuildConfig
import eu.kanade.tachiyomi.network.parseAs
import kotlinx.serialization.json.Json
import okhttp3.Interceptor
@ -34,6 +35,7 @@ class MyAnimeListInterceptor(private val myanimelist: MyAnimeList, private var t
// Add the authorization header to the original request
val authRequest = originalRequest.newBuilder()
.addHeader("Authorization", "Bearer ${oauth!!.access_token}")
.header("User-Agent", "Mihon v${BuildConfig.VERSION_NAME} (${BuildConfig.APPLICATION_ID})")
.build()
val response = chain.proceed(authRequest)
@ -50,6 +52,7 @@ class MyAnimeListInterceptor(private val myanimelist: MyAnimeList, private var t
val newRequest = originalRequest.newBuilder()
.addHeader("Authorization", "Bearer ${newToken.access_token}")
.header("User-Agent", "Mihon v${BuildConfig.VERSION_NAME} (${BuildConfig.APPLICATION_ID})")
.build()
return chain.proceed(newRequest)

View file

@ -192,8 +192,8 @@ class ShikimoriApi(
)
companion object {
private const val clientId = "1aaf4cf232372708e98b5abc813d795b539c5a916dbbfe9ac61bf02a360832cc"
private const val clientSecret = "229942c742dd4cde803125d17d64501d91c0b12e14cb1e5120184d77d67024c0"
private const val clientId = "PB9dq8DzI405s7wdtwTdirYqHiyVMh--djnP7lBUqSA"
private const val clientSecret = "NajpZcOBKB9sJtgNcejf8OB9jBN1OYYoo-k4h2WWZus"
private const val baseUrl = "https://shikimori.one"
private const val apiUrl = "$baseUrl/api"

View file

@ -1,5 +1,6 @@
package eu.kanade.tachiyomi.data.track.shikimori
import eu.kanade.tachiyomi.BuildConfig
import kotlinx.serialization.json.Json
import okhttp3.Interceptor
import okhttp3.Response
@ -33,7 +34,7 @@ class ShikimoriInterceptor(private val shikimori: Shikimori) : Interceptor {
// Add the authorization header to the original request.
val authRequest = originalRequest.newBuilder()
.addHeader("Authorization", "Bearer ${oauth!!.access_token}")
.header("User-Agent", "Tachiyomi")
.header("User-Agent", "Mihon v${BuildConfig.VERSION_NAME} (${BuildConfig.APPLICATION_ID})")
.build()
return chain.proceed(authRequest)