Skip to content

Commit

Permalink
Merge pull request #100 from Shikkanime/dev
Browse files Browse the repository at this point in the history
Fix Crunchyroll API
  • Loading branch information
Ziedelth authored Jan 24, 2024
2 parents 1c07924 + 6c2e258 commit 752a68c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 11 deletions.
30 changes: 20 additions & 10 deletions src/main/kotlin/fr/shikkanime/platforms/CrunchyrollPlatform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import fr.shikkanime.utils.ConfigPropertyKey
import fr.shikkanime.utils.HttpRequest
import fr.shikkanime.utils.MapCache
import fr.shikkanime.utils.ObjectParser
import fr.shikkanime.utils.ObjectParser.getAsBoolean
import fr.shikkanime.utils.ObjectParser.getAsInt
import fr.shikkanime.utils.ObjectParser.getAsLong
import fr.shikkanime.utils.ObjectParser.getAsString
Expand Down Expand Up @@ -190,17 +189,28 @@ class CrunchyrollPlatform : AbstractPlatform<CrunchyrollConfiguration, CountryCo
}
}

private fun parseAPIContent(
bypassFileContent: File?,
countryCode: CountryCode,
zonedDateTime: ZonedDateTime
): List<JsonObject> {
return if (bypassFileContent != null && bypassFileContent.exists()) {
if (configCacheService.getValueAsBoolean(ConfigPropertyKey.USE_CRUNCHYROLL_API)) {
ObjectParser.fromJson(bypassFileContent.readText()).getAsJsonArray("items").map { it.asJsonObject }
} else {
jsonObjects(bypassFileContent.readText())
}
} else getApiContent(
countryCode,
zonedDateTime
)
}

override fun fetchEpisodes(zonedDateTime: ZonedDateTime, bypassFileContent: File?): List<Episode> {
val list = mutableListOf<Episode>()

configuration!!.availableCountries.forEach { countryCode ->
val api =
if (bypassFileContent != null && bypassFileContent.exists()) {
jsonObjects(bypassFileContent.readText())
} else getApiContent(
countryCode,
zonedDateTime
)
val api = parseAPIContent(bypassFileContent, countryCode, zonedDateTime)

api.forEach {
try {
Expand Down Expand Up @@ -243,11 +253,11 @@ class CrunchyrollPlatform : AbstractPlatform<CrunchyrollConfiguration, CountryCo
throw EpisodeNotAvailableInCountryException("Episode of $animeName is not available in ${countryCode.name}")
}

val isDubbed = episodeMetadata.getAsBoolean("is_dubbed", false)
val audio = episodeMetadata.getAsString("audio_locale")?.ifBlank { null }
val isDubbed = audio == countryCode.locale
val subtitles = episodeMetadata.getAsJsonArray("subtitle_locales").map { it.asString!! }

if ((!isDubbed && (subtitles.isEmpty() || !subtitles.contains(countryCode.locale))) || (isDubbed && audio != countryCode.locale)) {
if (!isDubbed && (subtitles.isEmpty() || !subtitles.contains(countryCode.locale))) {
throw EpisodeNoSubtitlesOrVoiceException("Episode is not available in ${countryCode.name} with subtitles or voice")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package fr.shikkanime.platforms

import fr.shikkanime.caches.CountryCodeAnimeIdKeyCache
import fr.shikkanime.entities.Config
import fr.shikkanime.entities.enums.CountryCode
import fr.shikkanime.entities.enums.LangType
import fr.shikkanime.services.ConfigService
import fr.shikkanime.utils.ConfigPropertyKey
import fr.shikkanime.utils.Constant
import fr.shikkanime.utils.MapCache
import jakarta.inject.Inject
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
Expand All @@ -14,6 +20,9 @@ class CrunchyrollPlatformTest {
@Inject
lateinit var platform: CrunchyrollPlatform

@Inject
lateinit var configService: ConfigService

@BeforeEach
fun setUp() {
Constant.injector.injectMembers(this)
Expand All @@ -22,8 +31,14 @@ class CrunchyrollPlatformTest {
platform.configuration!!.availableCountries.add(CountryCode.FR)
}

@AfterEach
fun tearDown() {
configService.deleteAll()
MapCache.invalidate(Config::class.java)
}

@Test
fun fetchEpisodes() {
fun fetchEpisodesXML() {
val s = "2023-12-11T18:00:00Z"
val zonedDateTime = ZonedDateTime.parse(s)

Expand Down Expand Up @@ -181,4 +196,30 @@ class CrunchyrollPlatformTest {
assertEquals("SHY", episodes[2].anime?.name)
assertEquals("Dead Mount Death Play", episodes[3].anime?.name)
}

@Test
fun fetchEpisodesJSON() {
configService.save(Config(propertyKey = ConfigPropertyKey.USE_CRUNCHYROLL_API.key, propertyValue = "true"))
MapCache.invalidate(Config::class.java)

val s = "2024-01-24T18:45:00Z"
val zonedDateTime = ZonedDateTime.parse(s)

platform.simulcasts[CountryCode.FR] = setOf("metallic rouge")

val episodes = platform.fetchEpisodes(
zonedDateTime,
File(
ClassLoader.getSystemClassLoader().getResource("crunchyroll/api-${s.replace(':', '-')}.json")?.file
?: throw Exception("File not found")
)
)

assertEquals(true, episodes.isNotEmpty())
assertEquals(2, episodes.size)
assertEquals("Metallic Rouge", episodes[0].anime?.name)
assertEquals(LangType.VOICE, episodes[0].langType)
assertEquals("Metallic Rouge", episodes[1].anime?.name)
assertEquals(LangType.SUBTITLES, episodes[1].langType)
}
}

Large diffs are not rendered by default.

0 comments on commit 752a68c

Please sign in to comment.