Skip to content

Commit

Permalink
Merge pull request #475 from Shikkanime/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Ziedelth authored May 20, 2024
2 parents 9dedad7 + aed5fde commit d3fb432
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 74 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM mcr.microsoft.com/playwright:v1.43.1-jammy
FROM mcr.microsoft.com/playwright:v1.44.0-jammy
ARG version=21.0.3.9-1
ENV LANG C.UTF-8
ENV JAVA_HOME /usr/lib/jvm/java-21-amazon-corretto
Expand Down
6 changes: 3 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
val ktorVersion = "2.3.11"
val ktorSwaggerUiVersion = "2.9.0"
val ktorSwaggerUiVersion = "2.10.0"
val hibernateCoreVersion = "6.5.1.Final"
val ehcacheVersion = "3.10.8"
val glassfishJaxbVersion = "4.0.5"
Expand All @@ -11,9 +11,9 @@ val guiceVersion = "7.0.0"
val liquibaseCoreVersion = "4.27.0"
val quartzVersion = "2.5.0-rc1"
val guavaVersion = "33.2.0-jre"
val playwrightVersion = "1.43.0"
val playwrightVersion = "1.44.0"
val jsoupVersion = "1.17.2"
val gsonVersion = "2.10.1"
val gsonVersion = "2.11.0"
val openCvVersion = "4.9.0-0"
val bcprovVersion = "1.78.1"
val javaImageScalingVersion = "0.8.6"
Expand Down
15 changes: 11 additions & 4 deletions src/main/kotlin/fr/shikkanime/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import fr.shikkanime.jobs.*
import fr.shikkanime.modules.configureHTTP
import fr.shikkanime.modules.configureRouting
import fr.shikkanime.modules.configureSecurity
import fr.shikkanime.services.AnimeService
import fr.shikkanime.services.EpisodeMappingService
import fr.shikkanime.services.ImageService
import fr.shikkanime.services.MemberService
import fr.shikkanime.services.*
import fr.shikkanime.socialnetworks.DiscordSocialNetwork
import fr.shikkanime.utils.Constant
import fr.shikkanime.utils.JobManager
Expand Down Expand Up @@ -64,7 +61,13 @@ fun main() {
}

private fun updateAndDeleteData(episodeMappingService: EpisodeMappingService, animeService: AnimeService) {
val episodeVariantService = Constant.injector.getInstance(EpisodeVariantService::class.java)

animeService.findAll().forEach {
val mappingVariants = episodeVariantService.findAllByAnime(it)
it.releaseDateTime = mappingVariants.minOf { it.releaseDateTime }
it.lastReleaseDateTime = mappingVariants.maxOf { it.releaseDateTime }

val toSlug = StringUtils.toSlug(StringUtils.getShortName(it.name!!))

if (toSlug != it.slug) {
Expand All @@ -77,6 +80,10 @@ private fun updateAndDeleteData(episodeMappingService: EpisodeMappingService, an
}

episodeMappingService.findAll().forEach {
val mappingVariants = episodeVariantService.findAllByMapping(it)
it.releaseDateTime = mappingVariants.minOf { it.releaseDateTime }
it.lastReleaseDateTime = mappingVariants.maxOf { it.releaseDateTime }

it.status = StringUtils.getStatus(it)
episodeMappingService.update(it)
}
Expand Down
16 changes: 9 additions & 7 deletions src/main/kotlin/fr/shikkanime/jobs/FetchOldEpisodesJob.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,22 @@ class FetchOldEpisodesJob : AbstractJob {
if (realSaved > 0) {
logger.info("Updating mappings...")

variants.groupBy { it.mapping!!.uuid }.forEach { (mappingUuid, variants) ->
variants.groupBy { it.mapping!!.uuid }.forEach { (mappingUuid, _) ->
val mapping = episodeMappingService.find(mappingUuid) ?: return@forEach
mapping.releaseDateTime = variants.minOf { it.releaseDateTime }
mapping.lastReleaseDateTime = variants.maxOf { it.releaseDateTime }
val mappingVariants = episodeVariantService.findAllByMapping(mapping)
mapping.releaseDateTime = mappingVariants.minOf { it.releaseDateTime }
mapping.lastReleaseDateTime = mappingVariants.maxOf { it.releaseDateTime }
episodeMappingService.update(mapping)
}

logger.info("Updating animes...")

variants.groupBy { it.mapping!!.anime!!.uuid }.forEach { (animeUuid, variants) ->
variants.groupBy { it.mapping!!.anime!!.uuid }.forEach { (animeUuid, _) ->
val anime = animeService.find(animeUuid) ?: return@forEach
val mappingVariants = episodeVariantService.findAllByAnime(anime)
logger.info("Updating ${StringUtils.getShortName(anime.name!!)}...")
anime.releaseDateTime = variants.minOf { it.releaseDateTime }
anime.lastReleaseDateTime = variants.maxOf { it.releaseDateTime }
anime.releaseDateTime = mappingVariants.minOf { it.releaseDateTime }
anime.lastReleaseDateTime = mappingVariants.maxOf { it.releaseDateTime }
animeService.update(anime)
}

Expand Down Expand Up @@ -179,7 +181,7 @@ class FetchOldEpisodesJob : AbstractJob {
return episodes
}

private val crunchyrollEpisodesCache =
val crunchyrollEpisodesCache =
MapCache<CountryCodeIdKeyCache, List<CrunchyrollWrapper.Episode>>(duration = Duration.ofDays(7)) {
try {
val accessToken = runBlocking { CrunchyrollWrapper.getAnonymousAccessToken() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fr.shikkanime.socialnetworks
import fr.shikkanime.dtos.variants.EpisodeVariantDto
import fr.shikkanime.entities.enums.ConfigPropertyKey
import fr.shikkanime.services.ImageService
import fr.shikkanime.socialnetworks.listeners.SlashCommandInteractionListener
import fr.shikkanime.utils.Constant
import fr.shikkanime.utils.LoggerFactory
import fr.shikkanime.utils.ObjectParser
Expand All @@ -12,9 +13,6 @@ import net.dv8tion.jda.api.JDA
import net.dv8tion.jda.api.JDABuilder
import net.dv8tion.jda.api.Permission
import net.dv8tion.jda.api.entities.Activity
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
import net.dv8tion.jda.api.hooks.ListenerAdapter
import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions
import net.dv8tion.jda.api.interactions.commands.OptionType
import net.dv8tion.jda.api.interactions.commands.build.Commands
Expand All @@ -35,9 +33,6 @@ class DiscordSocialNetwork : AbstractSocialNetwork() {
private var isInitialized = false
private var jda: JDA? = null

private fun getTextChannels(): MutableList<TextChannel>? =
jda?.getTextChannelsByName("bot\uD83E\uDD16", true)

override fun utmSource() = "discord"

override fun login() {
Expand Down Expand Up @@ -68,59 +63,7 @@ class DiscordSocialNetwork : AbstractSocialNetwork() {
)

commands.submit()

this.jda!!.addEventListener(object : ListenerAdapter() {
override fun onSlashCommandInteraction(event: SlashCommandInteractionEvent) {
if (!event.name.equals("add-release-channel", true)) return

val channel = event.getOption("channel")?.asChannel ?: run {
event.reply("Channel is required").setEphemeral(true).queue()
return
}

// If we can send messages to the channel
if (!channel.type.isMessage) {
event.reply("I can't send messages to this channel").setEphemeral(true).queue()
return
}

val animeName = event.getOption("anime")?.asString
val releaseType = if (animeName.isNullOrBlank()) "ALL" else "CUSTOM"

// Save the channel to the database
event.reply("Channel added to receive release notifications").queue()

val file = getFile()
val channels = getChannels(file)

// If channel id already exists, update the release type and animes
val existingChannel = channels.firstOrNull { it.id == channel.idLong }

if (existingChannel != null) {
val oldReleaseType = existingChannel.releaseType
existingChannel.releaseType = releaseType

if (oldReleaseType != releaseType && oldReleaseType == "CUSTOM") {
existingChannel.animes.clear()
}

if (releaseType == "CUSTOM" && !animeName.isNullOrBlank() && !existingChannel.animes.contains(
animeName
)
) {
existingChannel.animes.add(animeName)
}
} else {
channels.add(
Channel(
channel.idLong,
releaseType,
animeName?.let { mutableListOf(it) } ?: mutableListOf()))
}

file.writeText(ObjectParser.toJson(channels))
}
})
this.jda!!.addEventListener(SlashCommandInteractionListener())
}

private fun getFile() = File(Constant.configFolder, "discord_channels.json")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package fr.shikkanime.socialnetworks.listeners

import fr.shikkanime.socialnetworks.DiscordSocialNetwork.Channel
import fr.shikkanime.utils.Constant
import fr.shikkanime.utils.ObjectParser
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
import net.dv8tion.jda.api.hooks.ListenerAdapter
import java.io.File

class SlashCommandInteractionListener : ListenerAdapter() {
private fun getFile() = File(Constant.configFolder, "discord_channels.json")

private fun getChannels(file: File): MutableList<Channel> {
return if (file.exists()) {
ObjectParser.fromJson(file.readText(), Array<Channel>::class.java).toMutableList()
} else {
mutableListOf()
}
}

override fun onSlashCommandInteraction(event: SlashCommandInteractionEvent) {
if (!event.name.equals("add-release-channel", true)) return

val channel = event.getOption("channel")?.asChannel ?: run {
event.reply("Channel is required").setEphemeral(true).queue()
return
}

// If we can send messages to the channel
if (!channel.type.isMessage) {
event.reply("I can't send messages to this channel").setEphemeral(true).queue()
return
}

val animeName = event.getOption("anime")?.asString
val releaseType = if (animeName.isNullOrBlank()) "ALL" else "CUSTOM"

// Save the channel to the database
event.reply("Channel added to receive release notifications").queue()

val file = getFile()
val channels = getChannels(file)

// If channel id already exists, update the release type and animes
val existingChannel = channels.firstOrNull { it.id == channel.idLong }

if (existingChannel != null) {
val oldReleaseType = existingChannel.releaseType
existingChannel.releaseType = releaseType

if (oldReleaseType != releaseType && oldReleaseType == "CUSTOM") {
existingChannel.animes.clear()
}

if (releaseType == "CUSTOM" && !animeName.isNullOrBlank() && !existingChannel.animes.contains(
animeName
)
) {
existingChannel.animes.add(animeName)
}
} else {
channels.add(
Channel(
channel.idLong,
releaseType,
animeName?.let { mutableListOf(it) } ?: mutableListOf()))
}

file.writeText(ObjectParser.toJson(channels))
}
}
29 changes: 29 additions & 0 deletions src/test/kotlin/fr/shikkanime/jobs/FetchOldEpisodesJobTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package fr.shikkanime.jobs

import com.google.inject.Inject
import fr.shikkanime.caches.CountryCodeIdKeyCache
import fr.shikkanime.entities.enums.CountryCode
import fr.shikkanime.utils.Constant
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

class FetchOldEpisodesJobTest {
@Inject
private lateinit var fetchOldEpisodesJob: FetchOldEpisodesJob

@BeforeEach
fun setUp() {
Constant.injector.injectMembers(this)
}

@Test
fun `check if Atelier Riza is correctly fetch`() {
val episodes = fetchOldEpisodesJob.crunchyrollEpisodesCache[CountryCodeIdKeyCache(CountryCode.FR, "GEXH3W2Z5")]

// If episodes contains the episode 1, it means that the job has worked
assertNotNull(episodes)
assertTrue(episodes!!.any { it.seasonNumber == 1 && it.number == 1 })
}
}

0 comments on commit d3fb432

Please sign in to comment.