Skip to content

Commit

Permalink
Move ADN simulcast detection on regex config
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziedelth committed Mar 26, 2024
1 parent 6bda5b4 commit c0d27d9
Show file tree
Hide file tree
Showing 6 changed files with 5,136 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ enum class ConfigPropertyKey(val key: String) {
ANALYTICS_API("analytics_api"),
ANALYTICS_SCRIPT("analytics_script"),
CRUNCHYROLL_FETCH_API_SIZE("crunchyroll_fetch_api_size"),
ANIMATION_DITIGAL_NETWORK_SIMULCAST_DETECTION_REGEX("animation_digital_network_simulcast_detection_regex"),
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package fr.shikkanime.platforms

import com.google.gson.JsonArray
import com.google.gson.JsonObject
import com.google.inject.Inject
import fr.shikkanime.entities.Anime
import fr.shikkanime.entities.Episode
import fr.shikkanime.entities.enums.CountryCode
import fr.shikkanime.entities.enums.EpisodeType
import fr.shikkanime.entities.enums.LangType
import fr.shikkanime.entities.enums.Platform
import fr.shikkanime.entities.enums.*
import fr.shikkanime.exceptions.AnimeException
import fr.shikkanime.exceptions.AnimeNotSimulcastedException
import fr.shikkanime.platforms.configuration.AnimationDigitalNetworkConfiguration
import fr.shikkanime.services.caches.ConfigCacheService
import fr.shikkanime.utils.ObjectParser
import fr.shikkanime.utils.ObjectParser.getAsBoolean
import fr.shikkanime.utils.ObjectParser.getAsInt
import fr.shikkanime.utils.ObjectParser.getAsLong
Expand All @@ -23,6 +23,9 @@ import java.util.logging.Level

class AnimationDigitalNetworkPlatform :
AbstractPlatform<AnimationDigitalNetworkConfiguration, CountryCode, List<JsonObject>>() {
@Inject
private lateinit var configCacheService: ConfigCacheService

override fun getPlatform(): Platform = Platform.ANIM

override fun getConfigurationClass() = AnimationDigitalNetworkConfiguration::class.java
Expand All @@ -31,11 +34,24 @@ class AnimationDigitalNetworkPlatform :
return AnimationDigitalNetworkWrapper.getLatestVideos(zonedDateTime.toLocalDate())
}

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

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

configuration!!.availableCountries.forEach { countryCode ->
val api = getApiContent(countryCode, zonedDateTime)
val api = parseAPIContent(bypassFileContent, countryCode, zonedDateTime)

api.forEach {
try {
Expand Down Expand Up @@ -86,10 +102,10 @@ class AnimationDigitalNetworkPlatform :

val descriptionLowercase = animeDescription.lowercase()

isSimulcasted = isSimulcasted || descriptionLowercase.startsWith("(premier épisode ") ||
descriptionLowercase.startsWith("(diffusion des ") ||
descriptionLowercase.startsWith("(diffusion du premier épisode") ||
descriptionLowercase.startsWith("(diffusion de l'épisode 1 le")
isSimulcasted = isSimulcasted ||
configCacheService.getValueAsString(ConfigPropertyKey.ANIMATION_DITIGAL_NETWORK_SIMULCAST_DETECTION_REGEX)?.let {
Regex(it).containsMatchIn(descriptionLowercase)
} == true

if (!isSimulcasted) throw AnimeNotSimulcastedException("Anime is not simulcasted")

Expand Down
24 changes: 24 additions & 0 deletions src/main/resources/db/changelog/2024/03/06-changelog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.26.xsd"
objectQuotingStrategy="QUOTE_ONLY_RESERVED_WORDS">
<property global="false" name="id" value="1711448390991"/>
<property global="false" name="author" value="Ziedelth"/>

<changeSet id="${id}-1" author="${author}" dbms="postgresql">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="0">SELECT COUNT(*)
FROM config
WHERE property_key = 'animation_digital_network_simulcast_detection_regex'</sqlCheck>
</preConditions>

<insert tableName="config">
<column name="uuid" valueComputed="gen_random_uuid()"/>
<column name="property_key" value="animation_digital_network_simulcast_detection_regex"/>
<column name="property_value" value="\((premier épisode |diffusion des épisodes |diffusion du premier épisode|diffusion de l'épisode 1 le)"/>
</insert>
</changeSet>
</databaseChangeLog>
1 change: 1 addition & 0 deletions src/main/resources/db/changelog/db.changelog-master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@
<include file="/db/changelog/2024/03/03-changelog.xml"/>
<include file="/db/changelog/2024/03/04-changelog.xml"/>
<include file="/db/changelog/2024/03/05-changelog.xml"/>
<include file="/db/changelog/2024/03/06-changelog.xml"/>
</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
package fr.shikkanime.platforms

import fr.shikkanime.entities.Config
import fr.shikkanime.entities.enums.ConfigPropertyKey
import fr.shikkanime.entities.enums.CountryCode
import fr.shikkanime.entities.enums.LangType
import fr.shikkanime.platforms.configuration.PlatformSimulcast
import fr.shikkanime.services.ConfigService
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.Assertions.assertNotNull
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import java.io.File
import java.time.ZonedDateTime
import java.util.*

class AnimationDigitalNetworkPlatformTest {
@Inject
lateinit var platform: AnimationDigitalNetworkPlatform

@Inject
lateinit var configService: ConfigService

@BeforeEach
fun setUp() {
Constant.injector.injectMembers(this)
Expand All @@ -31,6 +39,8 @@ class AnimationDigitalNetworkPlatformTest {
platform.configuration!!.availableCountries.remove(CountryCode.FR)
platform.configuration!!.simulcasts.removeIf { it.name == "Pon no Michi" }
platform.reset()
configService.deleteAll()
MapCache.invalidate(Config::class.java)
}

@Test
Expand Down Expand Up @@ -117,4 +127,28 @@ class AnimationDigitalNetworkPlatformTest {
assertEquals("Urusei Yatsura", episodes[2].anime?.name)
assertNotNull(episodes[2].description)
}

@Test
fun `fetchEpisodes for 2024-04-10`() {
configService.save(
Config(
propertyKey = ConfigPropertyKey.ANIMATION_DITIGAL_NETWORK_SIMULCAST_DETECTION_REGEX.key,
propertyValue = "\\((premier épisode |diffusion des épisodes |diffusion du premier épisode|diffusion de l'épisode 1 le)"
)
)
MapCache.invalidate(Config::class.java)

val s = "2024-04-10T08:00:00Z"
val zonedDateTime = ZonedDateTime.parse(s)

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

assertEquals(true, episodes.isEmpty())
}
}
Loading

0 comments on commit c0d27d9

Please sign in to comment.