Skip to content

Commit

Permalink
Fix bugs and add anime lang types
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziedelth committed Apr 7, 2024
1 parent f872cd4 commit bea44a7
Show file tree
Hide file tree
Showing 27 changed files with 254 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fr.shikkanime.controllers.admin
import fr.shikkanime.entities.enums.Link
import fr.shikkanime.entities.enums.Platform
import fr.shikkanime.utils.Constant
import fr.shikkanime.utils.LoggerFactory
import fr.shikkanime.utils.routes.AdminSessionAuthenticated
import fr.shikkanime.utils.routes.Controller
import fr.shikkanime.utils.routes.Path
Expand All @@ -13,9 +14,12 @@ import fr.shikkanime.utils.routes.param.BodyParam
import fr.shikkanime.utils.routes.param.PathParam
import io.ktor.http.*
import java.util.*
import java.util.logging.Level

@Controller("/admin/platforms")
class AdminPlatformController {
private val logger = LoggerFactory.getLogger(javaClass)

@Path
@Get
@AdminSessionAuthenticated
Expand Down Expand Up @@ -87,14 +91,19 @@ class AdminPlatformController {
): Response {
val abstractPlatform = Constant.abstractPlatforms.find { it.getPlatform().name == platform.name }
?: return Response.redirect(Link.PLATFORMS.href)
val platformConfiguration = abstractPlatform.configuration!!

val uuid = parameters["uuid"]?.let { UUID.fromString(it) }
val simulcast = abstractPlatform.configuration!!.simulcasts.find { it.uuid == uuid }
?: abstractPlatform.configuration!!.newPlatformSimulcast()
val simulcast =
platformConfiguration.simulcasts.find { it.uuid == uuid } ?: platformConfiguration.newPlatformSimulcast()
simulcast.of(parameters)

if (simulcast.uuid == null) {
simulcast.uuid = UUID.randomUUID()
abstractPlatform.configuration?.addPlatformSimulcast(simulcast)

if (!platformConfiguration.addPlatformSimulcast(simulcast)) {
logger.log(Level.SEVERE, "Failed to add simulcast")
}
}

abstractPlatform.saveConfiguration()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ class SiteController {
return Response.template(
Link.CALENDAR,
mutableMapOf(
"weeklyAnimes" to animeCacheService.getWeeklyAnimes(now.minusDays(now.dayOfWeek.value.toLong() - 1), CountryCode.FR),
"weeklyAnimes" to animeCacheService.getWeeklyAnimes(
now.minusDays(now.dayOfWeek.value.toLong() - 1),
CountryCode.FR
),
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class AnimeToAnimeDtoConverter : AbstractConverter<Anime, AnimeDto>() {
languageCacheService.detectLanguage(from.description) != from.countryCode!!.name.lowercase()
) Status.INVALID else Status.VALID


return AnimeDto.from(
convert(from, AnimeNoStatusDto::class.java),
status
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package fr.shikkanime.converters.anime

import com.google.inject.Inject
import fr.shikkanime.converters.AbstractConverter
import fr.shikkanime.dtos.SimulcastDto
import fr.shikkanime.dtos.animes.AnimeNoStatusDto
import fr.shikkanime.entities.Anime
import fr.shikkanime.services.AnimeService
import fr.shikkanime.services.SimulcastService.Companion.sortBySeasonAndYear
import fr.shikkanime.utils.StringUtils
import fr.shikkanime.utils.withUTC
import org.hibernate.Hibernate
import java.time.format.DateTimeFormatter

class AnimeToAnimeNoStatusDtoConverter : AbstractConverter<Anime, AnimeNoStatusDto>() {
@Inject
private lateinit var animeService: AnimeService

override fun convert(from: Anime): AnimeNoStatusDto {
return AnimeNoStatusDto(
uuid = from.uuid,
Expand All @@ -28,7 +33,8 @@ class AnimeToAnimeNoStatusDtoConverter : AbstractConverter<Anime, AnimeNoStatusD
) else null,
slug = from.slug,
lastReleaseDateTime = from.lastReleaseDateTime.withUTC()
.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)
.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME),
langTypes = animeService.getAllLangTypes(from)
)
}
}
3 changes: 3 additions & 0 deletions src/main/kotlin/fr/shikkanime/dtos/animes/AnimeDto.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fr.shikkanime.dtos.animes
import fr.shikkanime.dtos.SimulcastDto
import fr.shikkanime.dtos.enums.Status
import fr.shikkanime.entities.enums.CountryCode
import fr.shikkanime.entities.enums.LangType
import java.util.*

data class AnimeDto(
Expand All @@ -18,6 +19,7 @@ data class AnimeDto(
override val slug: String? = null,
override val lastReleaseDateTime: String? = null,
val status: Status? = null,
override val langTypes: List<LangType>? = null,
) : AnimeNoStatusDto(
uuid,
countryCode,
Expand Down Expand Up @@ -46,6 +48,7 @@ data class AnimeDto(
animeNoStatusDto.slug,
animeNoStatusDto.lastReleaseDateTime,
status,
animeNoStatusDto.langTypes
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fr.shikkanime.dtos.animes

import fr.shikkanime.dtos.SimulcastDto
import fr.shikkanime.entities.enums.CountryCode
import fr.shikkanime.entities.enums.LangType
import java.util.*

open class AnimeNoStatusDto(
Expand All @@ -26,7 +27,9 @@ open class AnimeNoStatusDto(
@Transient
open val slug: String? = null,
@Transient
open val lastReleaseDateTime: String? = null
open val lastReleaseDateTime: String? = null,
@Transient
open val langTypes: List<LangType>? = null,
) {
fun toAnimeDto() = AnimeDto.from(this, null)
}
18 changes: 14 additions & 4 deletions src/main/kotlin/fr/shikkanime/jobs/FetchCalendarJob.kt
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,12 @@ class FetchCalendarJob : AbstractJob {
val platformImage = getPlatformImage(platform)
platformImage?.let { graphics.drawImage(it, 25, y - 25, null) }
graphics.drawString(platformName, 65, y)
graphics.fillRect(25, y + 15, (platformImage?.width ?: 0) + 10 + graphics.fontMetrics.stringWidth(platformName), 2)
graphics.fillRect(
25,
y + 15,
(platformImage?.width ?: 0) + 10 + graphics.fontMetrics.stringWidth(platformName),
2
)

episodes.sortedBy { it.anime.lowercase() }.groupBy { it.anime }.forEach { (anime, episodes) ->
val twoLines = if (episodes.size == 1) {
Expand Down Expand Up @@ -213,14 +218,17 @@ class FetchCalendarJob : AbstractJob {

val animeBody = Jsoup.parse(animePage.bodyAsText())
val list = animeBody.select(".info_fiche > div") ?: return@mapNotNull null
val licencePlatforms = list.find { it.text().contains("Licence VOD") }?.select("a")?.map { it.text() }?.toMutableList()
val licencePlatforms =
list.find { it.text().contains("Licence VOD") }?.select("a")?.map { it.text() }?.toMutableList()
licencePlatforms?.removeIf { it.contains("TF1") }

if (licencePlatforms.isNullOrEmpty()) {
return@mapNotNull null
}

val alternativeTitles = list.find { it.text().contains("Titre alternatif") }?.text()?.replace("Titre alternatif :", "")?.split("/") ?: emptyList()
val alternativeTitles =
list.find { it.text().contains("Titre alternatif") }?.text()?.replace("Titre alternatif :", "")?.split("/")
?: emptyList()
val detectedLanguage = languageCacheService.detectLanguage(title)

if (detectedLanguage != null && detectedLanguage == "fr" && alternativeTitles.isNotEmpty()) {
Expand Down Expand Up @@ -253,7 +261,9 @@ class FetchCalendarJob : AbstractJob {

// If the last word of the first line and the first word of the second line is the same, we move the separator
// Or if the first word of the first line and the first word of the second line is the same, we move the separator
if (first.split(" ").last() == second.split(" ").first() || first.split(" ").first() == second.split(" ").first()) {
if (first.split(" ").last() == second.split(" ").first() || first.split(" ").first() == second.split(" ")
.first()
) {
separator++
val (firstHalfTry, secondHalfTry) = words.withIndex().partition { (index, _) -> index < separator }
first = firstHalfTry.joinToString(" ") { it.value }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ class AnimationDigitalNetworkPlatform :
val descriptionLowercase = animeDescription.lowercase()

isSimulcasted = isSimulcasted ||
configCacheService.getValueAsString(ConfigPropertyKey.ANIMATION_DITIGAL_NETWORK_SIMULCAST_DETECTION_REGEX)?.let {
Regex(it).containsMatchIn(descriptionLowercase)
} == true
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ data class DisneyPlusConfiguration(
),
)
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is DisneyPlusSimulcast) return false
if (!super.equals(other)) return false

if (releaseDay != other.releaseDay) return false
if (releaseTime != other.releaseTime) return false

return true
}

override fun hashCode(): Int {
var result = super.hashCode()
result = 31 * result + releaseDay
result = 31 * result + releaseTime.hashCode()
return result
}
}

override fun newPlatformSimulcast() = DisneyPlusSimulcast()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,28 @@ class NetflixConfiguration : PlatformConfiguration<NetflixConfiguration.NetflixS
),
)
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is NetflixSimulcastDay) return false
if (!super.equals(other)) return false

if (releaseDay != other.releaseDay) return false
if (image != other.image) return false
if (releaseTime != other.releaseTime) return false
if (season != other.season) return false

return true
}

override fun hashCode(): Int {
var result = super.hashCode()
result = 31 * result + releaseDay
result = 31 * result + image.hashCode()
result = 31 * result + releaseTime.hashCode()
result = 31 * result + season
return result
}
}

override fun newPlatformSimulcast() = NetflixSimulcastDay()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ open class PlatformSimulcast(
),
)
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is PlatformSimulcast) return false

if (uuid != other.uuid) return false
if (name != other.name) return false

return true
}

override fun hashCode(): Int {
var result = uuid?.hashCode() ?: 0
result = 31 * result + name.hashCode()
return result
}
}

abstract class PlatformConfiguration<S : PlatformSimulcast>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,27 @@ class PrimeVideoConfiguration : PlatformConfiguration<PrimeVideoConfiguration.Pr
override var releaseDay: Int = 1,
override var image: String = "",
override var releaseTime: String = "",
) : ReleaseDayPlatformSimulcast(releaseDay, image, releaseTime)
) : ReleaseDayPlatformSimulcast(releaseDay, image, releaseTime) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is PrimeVideoSimulcast) return false
if (!super.equals(other)) return false

if (releaseDay != other.releaseDay) return false
if (image != other.image) return false
if (releaseTime != other.releaseTime) return false

return true
}

override fun hashCode(): Int {
var result = super.hashCode()
result = 31 * result + releaseDay
result = 31 * result + image.hashCode()
result = 31 * result + releaseTime.hashCode()
return result
}
}

override fun newPlatformSimulcast() = PrimeVideoSimulcast()
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,24 @@ open class ReleaseDayPlatformSimulcast(
),
)
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is ReleaseDayPlatformSimulcast) return false
if (!super.equals(other)) return false

if (releaseDay != other.releaseDay) return false
if (image != other.image) return false
if (releaseTime != other.releaseTime) return false

return true
}

override fun hashCode(): Int {
var result = super.hashCode()
result = 31 * result + releaseDay
result = 31 * result + image.hashCode()
result = 31 * result + releaseTime.hashCode()
return result
}
}
13 changes: 13 additions & 0 deletions src/main/kotlin/fr/shikkanime/repositories/AnimeRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import fr.shikkanime.entities.Anime_
import fr.shikkanime.entities.Pageable
import fr.shikkanime.entities.SortParameter
import fr.shikkanime.entities.enums.CountryCode
import fr.shikkanime.entities.enums.LangType
import jakarta.persistence.Tuple
import org.hibernate.Hibernate
import org.hibernate.search.engine.search.predicate.dsl.BooleanPredicateClausesStep
Expand Down Expand Up @@ -171,4 +172,16 @@ class AnimeRepository : AbstractRepository<Anime>() {
.resultList
}
}

fun getAllLangTypes(anime: Anime): List<LangType> {
return inTransaction {
createReadOnlyQuery(
it,
"SELECT DISTINCT e.langType FROM Episode e WHERE e.anime = :anime",
LangType::class.java
)
.setParameter("anime", anime)
.resultList
}
}
}
22 changes: 12 additions & 10 deletions src/main/kotlin/fr/shikkanime/services/AnimeService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,23 @@ class AnimeService : AbstractService<Anime, AnimeRepository>() {
dateTitle,
episodes.distinctBy { episode -> episode.anime?.uuid.toString() + episode.langType.toString() }
.map { distinctEpisode ->
val platforms = episodes.filter { it.anime?.uuid == distinctEpisode.anime?.uuid }
.mapNotNull(Episode::platform)
.distinct()

WeeklyAnimeDto(
AbstractConverter.convert(distinctEpisode.anime, AnimeNoStatusDto::class.java).toAnimeDto(),
distinctEpisode.releaseDateTime.withUTC().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME),
distinctEpisode.langType!!,
AbstractConverter.convert(platforms, PlatformDto::class.java)
)
val platforms = episodes.filter { it.anime?.uuid == distinctEpisode.anime?.uuid }
.mapNotNull(Episode::platform)
.distinct()

WeeklyAnimeDto(
AbstractConverter.convert(distinctEpisode.anime, AnimeNoStatusDto::class.java).toAnimeDto(),
distinctEpisode.releaseDateTime.withUTC().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME),
distinctEpisode.langType!!,
AbstractConverter.convert(platforms, PlatformDto::class.java)
)
}.sortedBy { ZonedDateTime.parse(it.releaseDateTime).withZoneSameInstant(zoneId).toLocalTime() }
)
}
}

fun getAllLangTypes(anime: Anime) = animeRepository.getAllLangTypes(anime)

fun addImage(uuid: UUID, image: String, bypass: Boolean = false) {
ImageService.add(uuid, ImageService.Type.IMAGE, image, 480, 720, bypass)
}
Expand Down
Loading

0 comments on commit bea44a7

Please sign in to comment.