From e5c169d1ba212cee20bec640a9d249325eb7b24c Mon Sep 17 00:00:00 2001 From: Ziedelth Date: Mon, 27 May 2024 15:55:47 +0200 Subject: [PATCH] Fix FetchOldEpisodesJob.kt with simulcast range --- .../fr/shikkanime/jobs/FetchOldEpisodesJob.kt | 19 ++++++++++-- .../jobs/FetchOldEpisodesJobTest.kt | 31 +++++++++++++++++-- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/fr/shikkanime/jobs/FetchOldEpisodesJob.kt b/src/main/kotlin/fr/shikkanime/jobs/FetchOldEpisodesJob.kt index f7b7971b..8bb462e9 100644 --- a/src/main/kotlin/fr/shikkanime/jobs/FetchOldEpisodesJob.kt +++ b/src/main/kotlin/fr/shikkanime/jobs/FetchOldEpisodesJob.kt @@ -73,9 +73,8 @@ class FetchOldEpisodesJob : AbstractJob { val to = LocalDate.parse(config.propertyValue!!) val from = to.minusDays(range.toLong()) val dates = from.datesUntil(to.plusDays(1), Period.ofDays(1)).toList().sorted() - val simulcasts = dates.map { - "${Constant.seasons[(it.monthValue - 1) / 3]}-${it.year}".lowercase().replace("autumn", "fall") - }.toSet() + val simulcasts = getSimulcasts(dates) + val episodes = mutableListOf() val start = System.currentTimeMillis() logger.info("Fetching old episodes... (From ${dates.first()} to ${dates.last()})") @@ -144,6 +143,20 @@ class FetchOldEpisodesJob : AbstractJob { logger.info("Take ${(System.currentTimeMillis() - start) / 1000}s to check ${dates.size} dates") } + fun getSimulcasts(dates: List): Set { + val simulcastRange = configCacheService.getValueAsInt(ConfigPropertyKey.SIMULCAST_RANGE, 1) + val simulcastDates = dates.toMutableSet() + + for (i in 1..simulcastRange) { + simulcastDates.addAll(dates.map { it.plusDays(i.toLong()) }) + simulcastDates.addAll(dates.map { it.minusDays(i.toLong()) }) + } + + return simulcastDates.sorted().map { + "${Constant.seasons[(it.monthValue - 1) / 3]}-${it.year}".lowercase().replace("autumn", "fall") + }.toSet() + } + private fun fetchAnimationDigitalNetwork( countryCode: CountryCode, dates: List diff --git a/src/test/kotlin/fr/shikkanime/jobs/FetchOldEpisodesJobTest.kt b/src/test/kotlin/fr/shikkanime/jobs/FetchOldEpisodesJobTest.kt index 17541139..3d7bbe76 100644 --- a/src/test/kotlin/fr/shikkanime/jobs/FetchOldEpisodesJobTest.kt +++ b/src/test/kotlin/fr/shikkanime/jobs/FetchOldEpisodesJobTest.kt @@ -2,22 +2,36 @@ package fr.shikkanime.jobs import com.google.inject.Inject import fr.shikkanime.caches.CountryCodeIdKeyCache +import fr.shikkanime.entities.Config +import fr.shikkanime.entities.enums.ConfigPropertyKey import fr.shikkanime.entities.enums.CountryCode +import fr.shikkanime.services.ConfigService import fr.shikkanime.utils.Constant -import org.junit.jupiter.api.Assertions.assertNotNull -import org.junit.jupiter.api.Assertions.assertTrue +import fr.shikkanime.utils.MapCache +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test +import java.time.LocalDate class FetchOldEpisodesJobTest { @Inject private lateinit var fetchOldEpisodesJob: FetchOldEpisodesJob + @Inject + private lateinit var configService: ConfigService + @BeforeEach fun setUp() { Constant.injector.injectMembers(this) } + @AfterEach + fun tearDown() { + configService.deleteAll() + MapCache.invalidate(Config::class.java) + } + @Test fun `check if Atelier Riza is correctly fetch`() { val episodes = fetchOldEpisodesJob.crunchyrollEpisodesCache[CountryCodeIdKeyCache(CountryCode.FR, "GEXH3W2Z5")] @@ -36,4 +50,17 @@ class FetchOldEpisodesJobTest { assertTrue(episodes.any { it.seasonNumber == 1 && it.number == 170 }) assertTrue(episodes.size >= 170) } + + @Test + fun getSimulcasts() { + val dates = LocalDate.of(2023, 4, 10).datesUntil(LocalDate.of(2023, 4, 25)).toList() + configService.save(Config(propertyKey = ConfigPropertyKey.SIMULCAST_RANGE.key, propertyValue = "20")) + MapCache.invalidate(Config::class.java) + val simulcasts = fetchOldEpisodesJob.getSimulcasts(dates) + + assertTrue(simulcasts.isNotEmpty()) + assertEquals(2, simulcasts.size) + assertEquals("winter-2023", simulcasts.first()) + assertEquals("spring-2023", simulcasts.last()) + } } \ No newline at end of file