Skip to content

Commit

Permalink
Fix FetchOldEpisodesJob.kt with simulcast range
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziedelth committed May 27, 2024
1 parent 8920f7b commit e5c169d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
19 changes: 16 additions & 3 deletions src/main/kotlin/fr/shikkanime/jobs/FetchOldEpisodesJob.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Episode>()
val start = System.currentTimeMillis()
logger.info("Fetching old episodes... (From ${dates.first()} to ${dates.last()})")
Expand Down Expand Up @@ -144,6 +143,20 @@ class FetchOldEpisodesJob : AbstractJob {
logger.info("Take ${(System.currentTimeMillis() - start) / 1000}s to check ${dates.size} dates")
}

fun getSimulcasts(dates: List<LocalDate>): Set<String> {
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<LocalDate>
Expand Down
31 changes: 29 additions & 2 deletions src/test/kotlin/fr/shikkanime/jobs/FetchOldEpisodesJobTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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())
}
}

0 comments on commit e5c169d

Please sign in to comment.