diff --git a/src/en/allanime/src/eu/kanade/tachiyomi/animeextension/en/allanime/AllAnime.kt b/src/en/allanime/src/eu/kanade/tachiyomi/animeextension/en/allanime/AllAnime.kt index 48bacab07..f6ffe399a 100644 --- a/src/en/allanime/src/eu/kanade/tachiyomi/animeextension/en/allanime/AllAnime.kt +++ b/src/en/allanime/src/eu/kanade/tachiyomi/animeextension/en/allanime/AllAnime.kt @@ -26,6 +26,10 @@ import kotlinx.coroutines.async import kotlinx.coroutines.awaitAll import kotlinx.coroutines.runBlocking import kotlinx.serialization.json.Json +import kotlinx.serialization.json.buildJsonArray +import kotlinx.serialization.json.buildJsonObject +import kotlinx.serialization.json.put +import kotlinx.serialization.json.putJsonObject import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.Response @@ -39,7 +43,7 @@ class AllAnime : ConfigurableAnimeSource, AnimeHttpSource() { override val name = "AllAnime" - override val baseUrl by lazy { preferences.getString(PREF_DOMAIN_KEY, PREF_DOMAIN_DEFAULT)!! } + override val baseUrl by lazy { preferences.baseUrl } override val lang = "en" @@ -56,7 +60,12 @@ class AllAnime : ConfigurableAnimeSource, AnimeHttpSource() { // ============================== Popular =============================== override fun popularAnimeRequest(page: Int): Request { - val variables = """{"type":"anime","size":$PAGE_SIZE,"dateRange":7,"page":$page}""" + val variables = buildJsonObject { + put("type", "anime") + put("size", PAGE_SIZE) + put("dateRange", 7) + put("page", page) + } return GET("$baseUrl/allanimeapi?variables=$variables&query=$POPULAR_QUERY", headers = headers) } @@ -64,13 +73,11 @@ class AllAnime : ConfigurableAnimeSource, AnimeHttpSource() { val parsed = json.decodeFromString(response.body.string()) val animeList = mutableListOf() - val titleStyle = preferences.getString(PREF_TITLE_STYLE_KEY, PREF_TITLE_STYLE_DEFAULT)!! - parsed.data.queryPopular.recommendations.forEach { if (it.anyCard != null) { animeList.add( SAnime.create().apply { - title = when (titleStyle) { + title = when (preferences.titleStyle) { "romaji" -> it.anyCard.name "eng" -> it.anyCard.englishName ?: it.anyCard.name else -> it.anyCard.nativeName ?: it.anyCard.name @@ -88,15 +95,20 @@ class AllAnime : ConfigurableAnimeSource, AnimeHttpSource() { // =============================== Latest =============================== override fun latestUpdatesRequest(page: Int): Request { - // Could be lazily loaded along with url, but would require user to restart - val subPref = preferences.getString(PREF_SUB_KEY, PREF_SUB_DEFAULT)!! - val variables = """{"search":{"allowAdult":false,"allowUnknown":false},"limit":$PAGE_SIZE,"page":$page,"translationType":"$subPref","countryOrigin":"ALL"}""" + val variables = buildJsonObject { + putJsonObject("search") { + put("allowAdult", false) + put("allowUnknown", false) + } + put("limit", PAGE_SIZE) + put("page", page) + put("translationType", preferences.subPref) + put("countryOrigin", "ALL") + } return GET("$baseUrl/allanimeapi?variables=$variables&query=$SEARCH_QUERY", headers = headers) } - override fun latestUpdatesParse(response: Response): AnimesPage { - return parseAnime(response) - } + override fun latestUpdatesParse(response: Response): AnimesPage = parseAnime(response) // =============================== Search =============================== @@ -112,84 +124,105 @@ class AllAnime : ConfigurableAnimeSource, AnimeHttpSource() { override fun searchAnimeRequest(page: Int, query: String, filters: AnimeFilterList): Request = throw Exception("not used") private fun searchAnimeRequest(page: Int, query: String, filters: AllAnimeFilters.FilterSearchParams): Request { - val subPref = preferences.getString(PREF_SUB_KEY, PREF_SUB_DEFAULT)!! return if (query.isNotEmpty()) { - val variables = """{"search":{"query":"$query","allowAdult":false,"allowUnknown":false},"limit":$PAGE_SIZE,"page":$page,"translationType":"$subPref","countryOrigin":"ALL"}""" + val variables = buildJsonObject { + putJsonObject("search") { + put("query", query) + put("allowAdult", false) + put("allowUnknown", false) + } + put("limit", PAGE_SIZE) + put("page", page) + put("translationType", preferences.subPref) + put("countryOrigin", "ALL") + } GET("$baseUrl/allanimeapi?variables=$variables&query=$SEARCH_QUERY", headers = headers) } else { - val seasonString = if (filters.season == "all") "" else ""","season":"${filters.season}"""" - val yearString = if (filters.releaseYear == "all") "" else ""","year":${filters.releaseYear}""" - val genresString = if (filters.genres == "all") "" else ""","genres":${filters.genres},"excludeGenres":[]""" - val typesString = if (filters.types == "all") "" else ""","types":${filters.types}""" - val sortByString = if (filters.sortBy == "update") "" else ""","sortBy":"${filters.sortBy}"""" - - var variables = """{"search":{"allowAdult":false,"allowUnknown":false$seasonString$yearString$genresString$typesString$sortByString""" - variables += """},"limit":$PAGE_SIZE,"page":$page,"translationType":"$subPref","countryOrigin":"${filters.origin}"}""" - + val variables = buildJsonObject { + putJsonObject("search") { + put("allowAdult", false) + put("allowUnknown", false) + if (filters.season != "all") put("season", filters.season) + if (filters.releaseYear != "all") put("year", filters.releaseYear.toInt()) + if (filters.genres != "all") { + put("genres", json.decodeFromString(filters.genres)) + put("excludeGenres", buildJsonArray { }) + } + if (filters.types != "all") put("types", json.decodeFromString(filters.types)) + if (filters.sortBy != "update") put("sortBy", filters.sortBy) + } + put("limit", PAGE_SIZE) + put("page", page) + put("translationType", preferences.subPref) + put("countryOrigin", filters.origin) + } GET("$baseUrl/allanimeapi?variables=$variables&query=$SEARCH_QUERY", headers = headers) } } - override fun searchAnimeParse(response: Response): AnimesPage { - return parseAnime(response) - } + override fun searchAnimeParse(response: Response): AnimesPage = parseAnime(response) + + // ============================== Filters =============================== override fun getFilterList(): AnimeFilterList = AllAnimeFilters.FILTER_LIST // =========================== Anime Details ============================ - override fun animeDetailsParse(response: Response): SAnime = throw Exception("Not used") - override fun fetchAnimeDetails(anime: SAnime): Observable { return client.newCall(animeDetailsRequestInternal(anime)) .asObservableSuccess() .map { response -> - animeDetailsParse(response, anime).apply { initialized = true } + animeDetailsParse(response).apply { initialized = true } } } private fun animeDetailsRequestInternal(anime: SAnime): Request { - val variables = """{"_id":"${anime.url.split("<&sep>").first()}"}""" + val variables = buildJsonObject { + put("_id", anime.url.split("<&sep>").first()) + } return GET("$baseUrl/allanimeapi?variables=$variables&query=$DETAILS_QUERY", headers = headers) } override fun animeDetailsRequest(anime: SAnime): Request { val (id, time, slug) = anime.url.split("<&sep>") - val slugTime = if (time.isNotEmpty()) { - "-st-$time" - } else { - time - } - val siteUrl = preferences.getString(PREF_SITE_DOMAIN_KEY, PREF_SITE_DOMAIN_DEFAULT)!! + val slugTime = if (time.isNotEmpty()) "-st-$time" else time + val siteUrl = preferences.siteUrl + return GET("$siteUrl/anime/$id/$slug$slugTime") } - private fun animeDetailsParse(response: Response, animeOld: SAnime): SAnime { + override fun animeDetailsParse(response: Response): SAnime { val show = json.decodeFromString(response.body.string()).data.show + return SAnime.create().apply { - title = animeOld.title genre = show.genres?.joinToString(separator = ", ") ?: "" status = parseStatus(show.status) author = show.studios?.firstOrNull() - description = Jsoup.parse( - show.description?.replace("
", "br2n") ?: "", - ).text().replace("br2n", "\n") + - "\n\n" + - "Type: ${show.type ?: "Unknown"}" + - "\nAired: ${show.season?.quarter ?: "-"} ${show.season?.year ?: "-"}" + - "\nScore: ${show.score ?: "-"}★" + description = buildString { + append( + Jsoup.parse( + show.description?.replace("
", "br2n") ?: "", + ).text().replace("br2n", "\n"), + ) + append("\n\n") + append("Type: ${show.type ?: "Unknown"}") + append("\nAired: ${show.season?.quarter ?: "-"} ${show.season?.year ?: "-"}") + append("\nScore: ${show.score ?: "-"}★") + } } } // ============================== Episodes ============================== override fun episodeListRequest(anime: SAnime): Request { - val variables = """{"_id":"${anime.url.split("<&sep>").first()}"}""" + val variables = buildJsonObject { + put("_id", anime.url.split("<&sep>").first()) + } return GET("$baseUrl/allanimeapi?variables=$variables&query=$EPISODES_QUERY", headers = headers) } override fun episodeListParse(response: Response): List { - val subPref = preferences.getString(PREF_SUB_KEY, PREF_SUB_DEFAULT)!! + val subPref = preferences.subPref val medias = json.decodeFromString(response.body.string()) val episodesDetail = if (subPref == "sub") { @@ -200,7 +233,12 @@ class AllAnime : ConfigurableAnimeSource, AnimeHttpSource() { return episodesDetail.map { ep -> val numName = ep.toIntOrNull() ?: (ep.toFloatOrNull() ?: "1") - val variables = """{"showId":"${medias.data.show._id}","translationType":"$subPref","episodeString":"$ep"}""" + val variables = buildJsonObject { + put("showId", medias.data.show._id) + put("translationType", subPref) + put("episodeString", ep) + } + SEpisode.create().apply { episode_number = ep.toFloatOrNull() ?: 0F name = "Episode $numName ($subPref)" @@ -211,27 +249,13 @@ class AllAnime : ConfigurableAnimeSource, AnimeHttpSource() { // ============================ Video Links ============================= - override fun videoListRequest(episode: SEpisode): Request { - val headers = headers.newBuilder() - .set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:101.0) Gecko/20100101 Firefox/101.0") - .build() - return GET(baseUrl + episode.url, headers) - } - override fun videoListParse(response: Response): List