Skip to content

Commit

Permalink
kitsu description fix
Browse files Browse the repository at this point in the history
  • Loading branch information
rebelonion committed Jan 21, 2024
1 parent 3c992f8 commit b018d0f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 21 deletions.
19 changes: 13 additions & 6 deletions app/src/main/java/ani/dantotsu/media/anime/AnimeNameAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
19 changes: 14 additions & 5 deletions app/src/main/java/ani/dantotsu/media/anime/AnimeWatchFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
}
}
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 40 additions & 9 deletions app/src/main/java/ani/dantotsu/others/Kitsu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Episode>? {
Expand Down Expand Up @@ -54,21 +66,40 @@ 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,
desc = ep.description?.en,
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
Expand All @@ -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
Expand Down

0 comments on commit b018d0f

Please sign in to comment.