animeonsen: implement oauth, add aniyomi user-agent

This commit is contained in:
jmir1
2022-08-13 21:43:23 +02:00
parent 7f53ba940e
commit 289eb4e823
3 changed files with 35 additions and 17 deletions

View File

@ -6,7 +6,7 @@ ext {
extName = 'AnimeOnsen' extName = 'AnimeOnsen'
pkgNameSuffix = 'all.animeonsen' pkgNameSuffix = 'all.animeonsen'
extClass = '.AnimeOnsen' extClass = '.AnimeOnsen'
extVersionCode = 1 extVersionCode = 2
libVersion = '13' libVersion = '13'
} }

View File

@ -1,11 +1,15 @@
package eu.kanade.tachiyomi.animeextension.all.animeonsen package eu.kanade.tachiyomi.animeextension.all.animeonsen
import android.util.Base64 import eu.kanade.tachiyomi.network.POST
import android.util.Log import kotlinx.serialization.decodeFromString
import eu.kanade.tachiyomi.network.GET import kotlinx.serialization.json.Json
import okhttp3.HttpUrl.Companion.toHttpUrl import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.jsonPrimitive
import okhttp3.Headers
import okhttp3.Interceptor import okhttp3.Interceptor
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response import okhttp3.Response
class AOAPIInterceptor(client: OkHttpClient) : Interceptor { class AOAPIInterceptor(client: OkHttpClient) : Interceptor {
@ -13,19 +17,27 @@ class AOAPIInterceptor(client: OkHttpClient) : Interceptor {
private val token: String private val token: String
init { init {
val cookie = client.cookieJar val body = """
.loadForRequest("https://animeonsen.xyz".toHttpUrl()) {
.find { it.name == "ao.session" }?.value "client_id": "f296be26-28b5-4358-b5a1-6259575e23b7",
?: client.newCall(GET("https://animeonsen.xyz")).execute().header("set-cookie") "client_secret": "349038c4157d0480784753841217270c3c5b35f4281eaee029de21cb04084235",
"grant_type": "client_credentials"
}
""".trimIndent().toRequestBody("application/json".toMediaType())
token = String( val headers = Headers.headersOf("user-agent", AO_USER_AGENT)
Base64.decode(
java.net.URLDecoder.decode(cookie, "utf-8"), val tokenResponse = client.newCall(
Base64.DEFAULT POST(
"https://auth.animeonsen.xyz/oauth/token",
headers,
body,
) )
) ).execute().body!!.string()
Log.i("bruh", token) val tokenObject = Json.decodeFromString<JsonObject>(tokenResponse)
token = tokenObject["access_token"]!!.jsonPrimitive.content
} }
override fun intercept(chain: Interceptor.Chain): Response { override fun intercept(chain: Interceptor.Chain): Response {

View File

@ -49,7 +49,8 @@ class AnimeOnsen : AnimeHttpSource() {
} }
override fun headersBuilder(): Headers.Builder = Headers.Builder() override fun headersBuilder(): Headers.Builder = Headers.Builder()
.add("Referer", baseUrl) .add("referer", baseUrl)
.add("user-agent", AO_USER_AGENT)
// ============================== Popular =============================== // ============================== Popular ===============================
// The site doesn't have a popular anime tab, so we use the home page instead (latest anime). // The site doesn't have a popular anime tab, so we use the home page instead (latest anime).
@ -91,7 +92,10 @@ class AnimeOnsen : AnimeHttpSource() {
val videoData = json.decodeFromString<VideoData>(response.body!!.string()) val videoData = json.decodeFromString<VideoData>(response.body!!.string())
val videoUrl = videoData.uri.stream val videoUrl = videoData.uri.stream
val subtitleLangs = videoData.metadata.subtitles val subtitleLangs = videoData.metadata.subtitles
val headers = Headers.headersOf("Referer", baseUrl) val headers = Headers.headersOf(
"referer", baseUrl,
"user-agent", AO_USER_AGENT,
)
val video = try { val video = try {
val subtitles = videoData.uri.subtitles.keys.map { val subtitles = videoData.uri.subtitles.keys.map {
val lang = subtitleLangs[it]!!.jsonPrimitive.content val lang = subtitleLangs[it]!!.jsonPrimitive.content
@ -166,3 +170,5 @@ class AnimeOnsen : AnimeHttpSource() {
thumbnail_url = "https://api.animeonsen.xyz/v4/image/420x600/$content_id" thumbnail_url = "https://api.animeonsen.xyz/v4/image/420x600/$content_id"
} }
} }
const val AO_USER_AGENT = "Aniyomi/app (mobile)"