diff --git a/app/src/main/java/ani/dantotsu/media/anime/AnimeNameAdapter.kt b/app/src/main/java/ani/dantotsu/media/anime/AnimeNameAdapter.kt index b59864b749..8fd0b97b36 100644 --- a/app/src/main/java/ani/dantotsu/media/anime/AnimeNameAdapter.kt +++ b/app/src/main/java/ani/dantotsu/media/anime/AnimeNameAdapter.kt @@ -47,22 +47,29 @@ class AnimeNameAdapter { fun removeEpisodeNumber(text: String): String { val regexPattern = Regex(episodeRegex, RegexOption.IGNORE_CASE) - return text.replace(regexPattern, "").ifEmpty { + val removedNumber = text.replace(regexPattern, "").ifEmpty { text } + return if (removedNumber.equals(text, true)) { // if nothing was removed + val failedEpisodeNumberPattern: Regex = + Regex(failedEpisodeNumberRegex, RegexOption.IGNORE_CASE) + failedEpisodeNumberPattern.replace(removedNumber) { mr -> + mr.value.replaceFirst(mr.groupValues[1], "") + }.ifEmpty { removedNumber } + } else { + removedNumber + } } fun removeEpisodeNumberCompletely(text: String): String { val regexPattern = Regex(episodeRegex, RegexOption.IGNORE_CASE) - val removedNumber = text.replace(regexPattern, "").ifEmpty { - text - } - return if (removedNumber.equals(text, true)) { + val removedNumber = text.replace(regexPattern, "") + return if (removedNumber.equals(text, true)) { // if nothing was removed val failedEpisodeNumberPattern: Regex = Regex(failedEpisodeNumberRegex, RegexOption.IGNORE_CASE) failedEpisodeNumberPattern.replace(removedNumber) { mr -> mr.value.replaceFirst(mr.groupValues[1], "") - }.ifEmpty { removedNumber } + } } else { removedNumber } diff --git a/app/src/main/java/ani/dantotsu/media/anime/AnimeWatchFragment.kt b/app/src/main/java/ani/dantotsu/media/anime/AnimeWatchFragment.kt index bc3ca616ba..487bf20718 100644 --- a/app/src/main/java/ani/dantotsu/media/anime/AnimeWatchFragment.kt +++ b/app/src/main/java/ani/dantotsu/media/anime/AnimeWatchFragment.kt @@ -208,10 +208,16 @@ class AnimeWatchFragment : Fragment() { } if (media.anime?.kitsuEpisodes != null) { if (media.anime!!.kitsuEpisodes!!.containsKey(i)) { - episode.desc = media.anime!!.kitsuEpisodes!![i]?.desc ?: episode.desc - episode.title = media.anime!!.kitsuEpisodes!![i]?.title ?: episode.title - episode.thumb = media.anime!!.kitsuEpisodes!![i]?.thumb - ?: FileUrl[media.cover] + episode.desc = + media.anime!!.kitsuEpisodes!![i]?.desc ?: episode.desc + episode.title = if (AnimeNameAdapter.removeEpisodeNumberCompletely( + episode.title ?: "" + ).isBlank() + ) media.anime!!.kitsuEpisodes!![i]?.title + ?: episode.title else episode.title + ?: media.anime!!.kitsuEpisodes!![i]?.title ?: episode.title + episode.thumb = media.anime!!.kitsuEpisodes!![i]?.thumb + ?: FileUrl[media.cover] } } } @@ -542,7 +548,10 @@ class AnimeWatchFragment : Fragment() { override fun onDestroy() { model.watchSources?.flushText() super.onDestroy() - requireContext().unregisterReceiver(downloadStatusReceiver) + try { + requireContext().unregisterReceiver(downloadStatusReceiver) + } catch (_: IllegalArgumentException) { + } } var state: Parcelable? = null diff --git a/app/src/main/java/ani/dantotsu/media/anime/ExoplayerView.kt b/app/src/main/java/ani/dantotsu/media/anime/ExoplayerView.kt index 6405bf73c3..ee5baedff8 100644 --- a/app/src/main/java/ani/dantotsu/media/anime/ExoplayerView.kt +++ b/app/src/main/java/ani/dantotsu/media/anime/ExoplayerView.kt @@ -957,7 +957,7 @@ class ExoplayerView : AppCompatActivity(), Player.Listener, SessionAvailabilityL episodes.forEach { val episode = it.value val cleanedTitle = AnimeNameAdapter.removeEpisodeNumberCompletely(episode.title ?: "") - episodeTitleArr.add("Episode ${episode.number}${if (episode.filler) " [Filler]" else ""}${if (cleanedTitle.isNotEmpty() && cleanedTitle != "null") ": $cleanedTitle" else ""}") + episodeTitleArr.add("Episode ${episode.number}${if (episode.filler) " [Filler]" else ""}${if (cleanedTitle.isNotBlank() && cleanedTitle != "null") ": $cleanedTitle" else ""}") } //Episode Change diff --git a/app/src/main/java/ani/dantotsu/others/Kitsu.kt b/app/src/main/java/ani/dantotsu/others/Kitsu.kt index e815572909..6f07b75d72 100644 --- a/app/src/main/java/ani/dantotsu/others/Kitsu.kt +++ b/app/src/main/java/ani/dantotsu/others/Kitsu.kt @@ -6,26 +6,38 @@ import ani.dantotsu.logger import ani.dantotsu.media.Media import ani.dantotsu.media.anime.Episode import ani.dantotsu.tryWithSuspend +import com.google.gson.Gson +import com.lagradost.nicehttp.NiceResponse import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable +import java.io.InputStreamReader +import java.util.zip.GZIPInputStream object Kitsu { private suspend fun getKitsuData(query: String): KitsuResponse? { val headers = mapOf( "Content-Type" to "application/json", "Accept" to "application/json", + "Accept-Encoding" to "gzip, deflate", + "Accept-Language" to "en-US,en;q=0.5", + "Host" to "kitsu.io", "Connection" to "keep-alive", - "DNT" to "1", - "Origin" to "https://kitsu.io" + "Origin" to "https://kitsu.io", + "Sec-Fetch-Dest" to "empty", + "Sec-Fetch-Mode" to "cors", + "Sec-Fetch-Site" to "cross-site", ) - val json = tryWithSuspend { - client.post( + val response = tryWithSuspend { + val res = client.post( "https://kitsu.io/api/graphql", headers, data = mapOf("query" to query) ) + res } - return json?.parsed() + val json = decodeToString(response) + val gson = Gson() + return gson.fromJson(json, KitsuResponse::class.java) } suspend fun getKitsuEpisodesDetails(media: Media): Map? { @@ -54,14 +66,14 @@ query { } } } -}""" +}""".trimIndent() val result = getKitsuData(query) ?: return null logger("Kitsu : result=$result", print) media.idKitsu = result.data?.lookupMapping?.id - return (result.data?.lookupMapping?.episodes?.nodes ?: return null).mapNotNull { ep -> - val num = ep?.num?.toString() ?: return@mapNotNull null + val a = (result.data?.lookupMapping?.episodes?.nodes ?: return null).mapNotNull { ep -> + val num = ep?.number?.toString() ?: return@mapNotNull null num to Episode( number = num, title = ep.titles?.canonical, @@ -69,6 +81,25 @@ query { thumb = FileUrl[ep.thumbnail?.original?.url], ) }.toMap() + logger("Kitsu : a=$a", print) + return a + } + + fun decodeToString(res: NiceResponse?): String? { + return when (res?.headers?.get("Content-Encoding")) { + "gzip" -> { + res.body.byteStream()?.use { inputStream -> + GZIPInputStream(inputStream).use { gzipInputStream -> + InputStreamReader(gzipInputStream).use { reader -> + reader.readText() + } + } + } + } + else -> { + res?.body?.string() + } + } } @Serializable @@ -93,7 +124,7 @@ query { @Serializable data class Node( - @SerialName("number") val num: Long? = null, + @SerialName("number") val number: Int? = null, @SerialName("titles") val titles: Titles? = null, @SerialName("description") val description: Description? = null, @SerialName("thumbnail") val thumbnail: Thumbnail? = null