@ -12,6 +12,7 @@ import eu.kanade.tachiyomi.network.parseAs
import eu.kanade.tachiyomi.util.PkceUtil
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.boolean
import kotlinx.serialization.json.contentOrNull
@ -27,11 +28,14 @@ import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import tachiyomi.core.util.lang.withIOContext
import uy.kohesive.injekt.injectLazy
import java.text.SimpleDateFormat
import java.util.Locale
class MyAnimeListApi ( private val client : OkHttpClient , interceptor : MyAnimeListInterceptor ) {
private val json : Json by injectLazy ( )
private val authClient = client . newBuilder ( ) . addInterceptor ( interceptor ) . build ( )
suspend fun getAccessToken ( authCode : String ) : OAuth {
@ -42,9 +46,11 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI
. add ( " code_verifier " , codeVerifier )
. add ( " grant_type " , " authorization_code " )
. build ( )
client . newCall ( POST ( " $baseOAuthUrl /token " , body = formBody ) )
. awaitSuccess ( )
. parseAs ( )
with ( json ) {
client . newCall ( POST ( " $baseOAuthUrl /token " , body = formBody ) )
. awaitSuccess ( )
. parseAs ( )
}
}
}
@ -54,10 +60,12 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI
. url ( " $baseApiUrl /users/@me " )
. get ( )
. build ( )
authClient . newCall ( request )
. awaitSuccess ( )
. parseAs < JsonObject > ( )
. let { it [ " name " ] !! . jsonPrimitive . content }
with ( json ) {
authClient . newCall ( request )
. awaitSuccess ( )
. parseAs < JsonObject > ( )
. let { it [ " name " ] !! . jsonPrimitive . content }
}
}
}
@ -68,19 +76,21 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI
. appendQueryParameter ( " q " , query . take ( 64 ) )
. appendQueryParameter ( " nsfw " , " true " )
. build ( )
authClient . newCall ( GET ( url . toString ( ) ) )
. awaitSuccess ( )
. parseAs < JsonObject > ( )
. let {
it [ " data " ] !! . jsonArray
. map { data -> data . jsonObject [ " node " ] !! . jsonObject }
. map { node ->
val id = node [ " id " ] !! . jsonPrimitive . int
async { getMangaDetails ( id ) }
}
. awaitAll ( )
. filter { trackSearch -> ! trackSearch . publishing _type . contains ( " novel " ) }
}
with ( json ) {
authClient . newCall ( GET ( url . toString ( ) ) )
. awaitSuccess ( )
. parseAs < JsonObject > ( )
. let {
it [ " data " ] !! . jsonArray
. map { data -> data . jsonObject [ " node " ] !! . jsonObject }
. map { node ->
val id = node [ " id " ] !! . jsonPrimitive . int
async { getMangaDetails ( id ) }
}
. awaitAll ( )
. filter { trackSearch -> ! trackSearch . publishing _type . contains ( " novel " ) }
}
}
}
}
@ -90,28 +100,34 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI
. appendPath ( id . toString ( ) )
. appendQueryParameter ( " fields " , " id,title,synopsis,num_chapters,main_picture,status,media_type,start_date " )
. build ( )
authClient . newCall ( GET ( url . toString ( ) ) )
. awaitSuccess ( )
. parseAs < JsonObject > ( )
. let {
val obj = it . jsonObject
TrackSearch . create ( TrackManager . MYANIMELIST ) . apply {
media _id = obj [ " id " ] !! . jsonPrimitive . long
title = obj [ " title " ] !! . jsonPrimitive . content
summary = obj [ " synopsis " ] ?. jsonPrimitive ?. content ?: " "
total _chapters = obj [ " num_chapters " ] !! . jsonPrimitive . int
cover _url = obj [ " main_picture " ] ?. jsonObject ?. get ( " large " ) ?. jsonPrimitive ?. content ?: " "
tracking _url = " https://myanimelist.net/manga/ $media _id "
publishing _status = obj [ " status " ] !! . jsonPrimitive . content . replace ( " _ " , " " )
publishing _type = obj [ " media_type " ] !! . jsonPrimitive . content . replace ( " _ " , " " )
start _date = try {
val outputDf = SimpleDateFormat ( " yyyy-MM-dd " , Locale . US )
outputDf . format ( obj [ " start_date " ] !! )
} catch ( e : Exception ) {
" "
with ( json ) {
authClient . newCall ( GET ( url . toString ( ) ) )
. awaitSuccess ( )
. parseAs < JsonObject > ( )
. let {
val obj = it . jsonObject
TrackSearch . create ( TrackManager . MYANIMELIST ) . apply {
media _id = obj [ " id " ] !! . jsonPrimitive . long
title = obj [ " title " ] !! . jsonPrimitive . content
summary = obj [ " synopsis " ] ?. jsonPrimitive ?. content ?: " "
total _chapters = obj [ " num_chapters " ] !! . jsonPrimitive . int
cover _url =
obj [ " main_picture " ] ?. jsonObject ?. get ( " large " ) ?. jsonPrimitive ?. content
?: " "
tracking _url = " https://myanimelist.net/manga/ $media _id "
publishing _status =
obj [ " status " ] !! . jsonPrimitive . content . replace ( " _ " , " " )
publishing _type =
obj [ " media_type " ] !! . jsonPrimitive . content . replace ( " _ " , " " )
start _date = try {
val outputDf = SimpleDateFormat ( " yyyy-MM-dd " , Locale . US )
outputDf . format ( obj [ " start_date " ] !! )
} catch ( e : Exception ) {
" "
}
}
}
}
}
}
}
@ -133,10 +149,12 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI
. url ( mangaUrl ( track . media _id ) . toString ( ) )
. put ( formBodyBuilder . build ( ) )
. build ( )
authClient . newCall ( request )
. awaitSuccess ( )
. parseAs < JsonObject > ( )
. let { parseMangaItem ( it , track ) }
with ( json ) {
authClient . newCall ( request )
. awaitSuccess ( )
. parseAs < JsonObject > ( )
. let { parseMangaItem ( it , track ) }
}
}
}
@ -146,15 +164,17 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI
. appendPath ( track . media _id . toString ( ) )
. appendQueryParameter ( " fields " , " num_chapters,my_list_status{start_date,finish_date} " )
. build ( )
authClient . newCall ( GET ( uri . toString ( ) ) )
. awaitSuccess ( )
. parseAs < JsonObject > ( )
. let { obj ->
track . total _chapters = obj [ " num_chapters " ] !! . jsonPrimitive . int
obj . jsonObject [ " my_list_status " ] ?. jsonObject ?. let {
parseMangaItem ( it , track )
with ( json ) {
authClient . newCall ( GET ( uri . toString ( ) ) )
. awaitSuccess ( )
. parseAs < JsonObject > ( )
. let { obj ->
track . total _chapters = obj [ " num_chapters " ] !! . jsonPrimitive . int
obj . jsonObject [ " my_list_status " ] ?. jsonObject ?. let {
parseMangaItem ( it , track )
}
}
}
}
}
}
@ -198,9 +218,11 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI
. url ( urlBuilder . build ( ) . toString ( ) )
. get ( )
. build ( )
authClient . newCall ( request )
. awaitSuccess ( )
. parseAs ( )
with ( json ) {
authClient . newCall ( request )
. awaitSuccess ( )
. parseAs ( )
}
}
}