Skip to content

Commit

Permalink
Merge pull request #489 from Shikkanime/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Ziedelth authored May 24, 2024
2 parents 77faa5f + 64ad54d commit cb3d455
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 49 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.44.0-jammy
FROM mcr.microsoft.com/playwright:v1.44.1-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
44 changes: 21 additions & 23 deletions src/main/kotlin/fr/shikkanime/jobs/FetchOldEpisodesJob.kt
Original file line number Diff line number Diff line change
Expand Up @@ -184,31 +184,29 @@ class FetchOldEpisodesJob : AbstractJob {
val crunchyrollEpisodesCache =
MapCache<CountryCodeIdKeyCache, List<CrunchyrollWrapper.Episode>>(duration = Duration.ofDays(7)) {
try {
val episodes = mutableListOf<CrunchyrollWrapper.Episode>()

val accessToken = runBlocking { CrunchyrollWrapper.getAnonymousAccessToken() }
val seasons =
runBlocking { CrunchyrollWrapper.getSeasonsBySeriesId(it.countryCode.locale, accessToken, it.id) }
.filter { season -> season.subtitleLocales.contains(it.countryCode.locale) }

return@MapCache seasons.flatMap { season ->
runBlocking {
CrunchyrollWrapper.getEpisodesBySeasonId(
it.countryCode.locale,
accessToken,
season.id
)
}
.flatMap { it.versions ?: listOf(CrunchyrollWrapper.Version(it.id)) }
.distinctBy { it.guid }
.parallelStream().map { version ->
runBlocking {
CrunchyrollWrapper.getEpisode(
it.countryCode.locale,
accessToken,
version.guid
)
}
}.toList()
val seasons = runBlocking { CrunchyrollWrapper.getSeasonsBySeriesId(it.countryCode.locale, accessToken, it.id) }
.filter { season -> season.subtitleLocales.contains(it.countryCode.locale) }

// Add original episodes
seasons.parallelStream().forEach { season ->
runBlocking { episodes.addAll(CrunchyrollWrapper.getEpisodesBySeasonId(it.countryCode.locale, accessToken, season.id)) }
}

// Need to list all available variants
val variants = mutableSetOf<CrunchyrollWrapper.Version>()
episodes.forEach { episode -> variants.addAll(episode.versions ?: listOf(CrunchyrollWrapper.Version(episode.id))) }
// Remove duplicates and already fetched episodes
val missingVariants = variants.distinctBy { variant -> variant.guid }
.filter { variant -> episodes.none { it.id == variant.guid } }

missingVariants.parallelStream().forEach { variant ->
runBlocking { episodes.add(CrunchyrollWrapper.getEpisode(it.countryCode.locale, accessToken, variant.guid)) }
}

return@MapCache episodes
} catch (e: Exception) {
logger.log(Level.SEVERE, "Error while fetching Crunchyroll episodes", e)
return@MapCache emptyList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class CrunchyrollPlatform : AbstractPlatform<CrunchyrollConfiguration, CountryCo
return@MapCache simulcastSeries
}

val animeInfoCache = MapCache<CountryCodeIdKeyCache, CrunchyrollAnimeContent>(Duration.ofDays(1)) {
private val animeInfoCache = MapCache<CountryCodeIdKeyCache, CrunchyrollAnimeContent>(Duration.ofDays(1)) {
val token = identifiers[it.countryCode]!!
val `object` = runBlocking {
CrunchyrollWrapper.getSeries(
Expand Down Expand Up @@ -136,7 +136,11 @@ class CrunchyrollPlatform : AbstractPlatform<CrunchyrollConfiguration, CountryCo
simulcasts.resetWithNewDuration(Duration.ofMinutes(configuration!!.simulcastCheckDelayInMinutes))
}

fun convertEpisode(countryCode: CountryCode, jsonObject: JsonObject, needSimulcast: Boolean = true): Episode {
private fun convertEpisode(
countryCode: CountryCode,
jsonObject: JsonObject,
needSimulcast: Boolean = true
): Episode {
val episodeMetadata = jsonObject.getAsJsonObject("episode_metadata")

val animeName = requireNotNull(episodeMetadata.getAsString("series_title")) { "Anime name is null" }
Expand Down
19 changes: 17 additions & 2 deletions src/main/kotlin/fr/shikkanime/repositories/MemberRepository.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package fr.shikkanime.repositories

import fr.shikkanime.entities.Member
import fr.shikkanime.entities.Member_
import fr.shikkanime.entities.*
import fr.shikkanime.entities.enums.Role
import java.util.*

class MemberRepository : AbstractRepository<Member>() {
override fun getEntityClass() = Member::class.java
Expand All @@ -19,6 +19,21 @@ class MemberRepository : AbstractRepository<Member>() {
}
}

fun findAllByAnimeUUID(animeUuid: UUID): List<Member> {
return inTransaction {
val cb = it.criteriaBuilder
val query = cb.createQuery(getEntityClass())
val root = query.from(MemberFollowAnime::class.java)

query.select(root[MemberFollowAnime_.member])
query.distinct(true)
query.where(cb.equal(root[MemberFollowAnime_.anime][Anime_.uuid], animeUuid))

createReadOnlyQuery(it, query)
.resultList
}
}

fun findByUsernameAndPassword(username: String, password: ByteArray): Member? {
return inTransaction {
val cb = it.criteriaBuilder
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/fr/shikkanime/services/ImageService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ object ImageService {
val take = measureTimeMillis {
val deserializedCache = ByteArrayInputStream(file.readBytes()).use { bais ->
ObjectInputStream(bais).use {
it.readObject() as List<Image>
it.readObject() as List<Image> // NOSONAR
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/fr/shikkanime/services/MemberService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class MemberService : AbstractService<Member, MemberRepository>() {

private fun findAllByRoles(roles: List<Role>) = memberRepository.findAllByRoles(roles)

fun findAllByAnimeUUID(animeUuid: UUID) = memberRepository.findAllByAnimeUUID(animeUuid)

fun findByUsernameAndPassword(username: String, password: String) =
memberRepository.findByUsernameAndPassword(username, EncryptionManager.generate(password))

Expand Down
48 changes: 29 additions & 19 deletions src/main/kotlin/fr/shikkanime/utils/FirebaseNotification.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import com.google.firebase.FirebaseApp
import com.google.firebase.FirebaseOptions
import com.google.firebase.messaging.AndroidConfig
import com.google.firebase.messaging.FirebaseMessaging
import com.google.firebase.messaging.Message
import com.google.firebase.messaging.MulticastMessage
import com.google.firebase.messaging.Notification
import fr.shikkanime.dtos.variants.EpisodeVariantDto
import fr.shikkanime.services.MemberService
import java.io.File
import java.io.FileInputStream

Expand All @@ -30,24 +31,33 @@ object FirebaseNotification {

fun send(episodeDto: EpisodeVariantDto) {
init()
if (!isInitialized) return
val memberService = Constant.injector.getInstance(MemberService::class.java)

FirebaseMessaging.getInstance()
.send(
Message.builder()
.setNotification(
Notification.builder()
.setTitle(episodeDto.mapping.anime.shortName)
.setBody(StringUtils.toEpisodeString(episodeDto))
.setImage("${Constant.apiUrl}/v1/attachments?uuid=${episodeDto.mapping.uuid}&type=image")
.build()
)
.setAndroidConfig(
AndroidConfig.builder()
.setPriority(AndroidConfig.Priority.HIGH)
.build()
)
.setTopic("global")
.build()
)
val notification = Notification.builder()
.setTitle(episodeDto.mapping.anime.shortName)
.setBody(StringUtils.toEpisodeString(episodeDto))
.setImage("${Constant.apiUrl}/v1/attachments?uuid=${episodeDto.mapping.uuid}&type=image")
.build()

val androidConfig = AndroidConfig.builder()
.setPriority(AndroidConfig.Priority.HIGH)
.build()

val tokens = mutableSetOf("global")
memberService.findAllByAnimeUUID(episodeDto.mapping.anime.uuid!!).forEach { tokens.add(it.uuid!!.toString()) }
// Chunked tokens to avoid the 500 tokens limit (due to the limit of the Firebase API)
val chunkedTokens = tokens.chunked(500)

chunkedTokens.forEach {
FirebaseMessaging.getInstance()
.sendEachForMulticast(
MulticastMessage.builder()
.addAllTokens(it)
.setNotification(notification)
.setAndroidConfig(androidConfig)
.build()
)
}
}
}
2 changes: 1 addition & 1 deletion src/main/resources/assets/css/purged/bootstrap.min.css

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions src/test/kotlin/fr/shikkanime/jobs/FetchOldEpisodesJobTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,14 @@ class FetchOldEpisodesJobTest {
assertNotNull(episodes)
assertTrue(episodes!!.any { it.seasonNumber == 1 && it.number == 1 })
}

@Test
fun `fetch Black Clover`() {
val episodes = fetchOldEpisodesJob.crunchyrollEpisodesCache[CountryCodeIdKeyCache(CountryCode.FR, "GRE50KV36")]

assertNotNull(episodes)
assertTrue(episodes!!.any { it.seasonNumber == 1 && it.number == 1 })
assertTrue(episodes.any { it.seasonNumber == 1 && it.number == 170 })
assertTrue(episodes.size >= 170)
}
}

0 comments on commit cb3d455

Please sign in to comment.