Skip to content

Commit

Permalink
Merge pull request #94 from Shikkanime/dev
Browse files Browse the repository at this point in the history
Add anime banner
  • Loading branch information
Ziedelth authored Jan 24, 2024
2 parents b306443 + be638df commit a8d66d7
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class AnimeDtoToAnimeConverter : AbstractConverter<AnimeDto, Anime>() {
name = from.name,
releaseDateTime = ZonedDateTime.parse(from.releaseDateTime),
image = from.image,
banner = from.banner,
description = from.description,
simulcasts = convert(from.simulcasts ?: emptyList(), Simulcast::class.java).toMutableSet()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class AnimeToAnimeDtoConverter : AbstractConverter<Anime, AnimeDto>() {
override fun convert(from: Anime): AnimeDto {
val status = if (
from.image.isNullOrBlank() ||
from.banner.isNullOrBlank() ||
from.description.isNullOrBlank() ||
from.description?.startsWith("(") == true ||
languageDetector.detect(from.description).language.lowercase() != from.countryCode!!.name.lowercase()
Expand All @@ -27,6 +28,7 @@ class AnimeToAnimeDtoConverter : AbstractConverter<Anime, AnimeDto>() {
releaseDateTime = from.releaseDateTime.withUTC()
.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME),
image = from.image,
banner = from.banner,
countryCode = from.countryCode!!,
name = from.name!!,
shortName = StringUtils.getShortName(from.name!!),
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/fr/shikkanime/dtos/AnimeDto.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ data class AnimeDto(
var shortName: String,
var releaseDateTime: String,
val image: String? = null,
val banner: String? = null,
val description: String?,
val simulcasts: List<SimulcastDto>?,
val status: Status? = null,
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/fr/shikkanime/entities/Anime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ data class Anime(
var releaseDateTime: ZonedDateTime = ZonedDateTime.now(),
@Column(nullable = false, columnDefinition = "VARCHAR(1000)")
var image: String? = null,
@Column(nullable = true, columnDefinition = "VARCHAR(1000)")
var banner: String? = null,
@Column(nullable = true, columnDefinition = "VARCHAR(2000)")
var description: String? = null,
@ManyToMany
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class AnimationDigitalNetworkPlatform :
animeName = animeName.replace(Regex(" -.*"), "").trim()

val animeImage = show.getAsString("image2x") ?: throw Exception("Anime image is null")
val animeBanner = show.getAsString("imageHorizontal2x") ?: throw Exception("Anime banner is null")
val animeDescription = show.getAsString("summary")?.replace('\n', ' ') ?: ""
val genres = show.getAsJsonArray("genres") ?: JsonArray()

Expand Down Expand Up @@ -144,6 +145,7 @@ class AnimationDigitalNetworkPlatform :
name = animeName,
releaseDateTime = releaseDate,
image = animeImage,
banner = animeBanner,
description = animeDescription,
),
episodeType = episodeType,
Expand Down
19 changes: 15 additions & 4 deletions src/main/kotlin/fr/shikkanime/platforms/CrunchyrollPlatform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ private const val IMAGE_NULL_ERROR = "Image is null"
class CrunchyrollPlatform : AbstractPlatform<CrunchyrollConfiguration, CountryCode, List<JsonObject>>() {
data class CrunchyrollAnimeContent(
val image: String,
val banner: String = "",
val description: String? = null,
)

Expand Down Expand Up @@ -118,14 +119,16 @@ class CrunchyrollPlatform : AbstractPlatform<CrunchyrollConfiguration, CountryCo

val animeInfoCache = MapCache<CountryCodeAnimeIdKeyCache, CrunchyrollAnimeContent>(Duration.ofDays(1)) {
var image: String? = null
var banner: String? = null
var description: String? = null

if (configCacheService.getValueAsBoolean(ConfigPropertyKey.USE_CRUNCHYROLL_API)) {
val (token, cms) = identifiers[it.countryCode]!!
val `object` = runBlocking { CrunchyrollWrapper.getObject(token, cms, it.animeId) }[0].asJsonObject
val posters = `object`.getAsJsonObject("images").getAsJsonArray("poster_tall")[0].asJsonArray
image =
posters?.maxByOrNull { poster -> poster.asJsonObject.getAsInt("width")!! }?.asJsonObject?.getAsString("source")
val postersTall = `object`.getAsJsonObject("images").getAsJsonArray("poster_tall")[0].asJsonArray
val postersWide = `object`.getAsJsonObject("images").getAsJsonArray("poster_wide")[0].asJsonArray
image = postersTall?.maxByOrNull { poster -> poster.asJsonObject.getAsInt("width")!! }?.asJsonObject?.getAsString("source")
banner = postersWide?.maxByOrNull { poster -> poster.asJsonObject.getAsInt("width")!! }?.asJsonObject?.getAsString("source")
description = `object`.getAsString("description")
} else {
HttpRequest().use { httpRequest ->
Expand All @@ -137,6 +140,8 @@ class CrunchyrollPlatform : AbstractPlatform<CrunchyrollConfiguration, CountryCo
image =
content.selectXpath("//*[@id=\"content\"]/div/div[2]/div/div[1]/div[2]/div/div/div[2]/div[2]/figure/picture/img")
.attr("src")
banner = content.selectXpath("//*[@id=\"content\"]/div/div[2]/div/div[1]/div[2]/div/div/div[2]/div[1]/figure/picture/img")
.attr("src")
description =
content.selectXpath("//*[@id=\"content\"]/div/div[2]/div/div[2]/div[1]/div[1]/div[5]/div/div/div/p")
.text()
Expand All @@ -154,7 +159,11 @@ class CrunchyrollPlatform : AbstractPlatform<CrunchyrollConfiguration, CountryCo
throw Exception("Image is null or empty")
}

return@MapCache CrunchyrollAnimeContent(image!!, description)
if (banner.isNullOrEmpty()) {
throw Exception("Banner is null or empty")
}

return@MapCache CrunchyrollAnimeContent(image!!, banner!!, description)
}

override fun getPlatform(): Platform = Platform.CRUN
Expand Down Expand Up @@ -291,6 +300,7 @@ class CrunchyrollPlatform : AbstractPlatform<CrunchyrollConfiguration, CountryCo
name = animeName,
releaseDateTime = releaseDate,
image = crunchyrollAnimeContent.image,
banner = crunchyrollAnimeContent.banner,
description = crunchyrollAnimeContent.description
),
episodeType = episodeType,
Expand Down Expand Up @@ -390,6 +400,7 @@ class CrunchyrollPlatform : AbstractPlatform<CrunchyrollConfiguration, CountryCo
name = animeName,
releaseDateTime = releaseDate,
image = crunchyrollAnimeContent.image,
banner = crunchyrollAnimeContent.banner,
description = crunchyrollAnimeContent.description
),
episodeType = episodeType,
Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/fr/shikkanime/platforms/DisneyPlusPlatform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ class DisneyPlusPlatform :
val animeImage = jsonObject.getAsJsonObject("image")?.getAsJsonObject("tile")?.getAsJsonObject("0.71")
?.getAsJsonObject("series")?.getAsJsonObject("default")?.getAsString("url")
?: throw Exception("Anime image is null")
val animeBanner = jsonObject.getAsJsonObject("image")?.getAsJsonObject("tile")?.getAsJsonObject("1.33")
?.getAsJsonObject("series")?.getAsJsonObject("default")?.getAsString("url")
?: throw Exception("Anime image is null")
val animeDescription =
texts.getAsJsonObject("description")?.getAsJsonObject("medium")?.getAsJsonObject("series")
?.getAsJsonObject("default")?.getAsString("content")?.replace('\n', ' ') ?: ""
Expand Down Expand Up @@ -167,6 +170,7 @@ class DisneyPlusPlatform :
name = animeName,
releaseDateTime = releaseDateTime,
image = animeImage,
banner = animeBanner,
description = animeDescription,
),
episodeType = EpisodeType.EPISODE,
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/fr/shikkanime/platforms/NetflixPlatform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class NetflixPlatform : AbstractPlatform<NetflixConfiguration, CountryCodeNetfli
val document =
HttpRequest().use { it.getBrowser("https://www.netflix.com/${key.countryCode.name.lowercase()}/title/$id") }
val animeName = document.selectFirst(".title-title")?.text() ?: return emptySet()
val animeBanner = document.selectXpath("//*[@id=\"section-hero\"]/div[1]/div[2]/picture/source[2]").attr("srcset").substringBefore("?")
val animeDescription = document.selectFirst(".title-info-synopsis")?.text()
val episodes = document.selectFirst("ol.episodes-container")?.select("li.episode") ?: emptySet()

Expand All @@ -52,6 +53,7 @@ class NetflixPlatform : AbstractPlatform<NetflixConfiguration, CountryCodeNetfli
name = animeName,
releaseDateTime = releaseDateTime,
image = key.netflixSimulcast.image,
banner = animeBanner,
description = animeDescription,
),
episodeType = EpisodeType.EPISODE,
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/fr/shikkanime/services/AnimeService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class AnimeService : AbstractService<Anime, AnimeRepository>() {
addImage(anime.uuid, anime.image!!)
}

parameters["banner"]?.takeIf { it.isNotBlank() }?.let { anime.banner = it }
parameters["description"]?.takeIf { it.isNotBlank() }?.let { anime.description = it }

val update = super.update(anime)
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/fr/shikkanime/services/EpisodeService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ class EpisodeService : AbstractService<Episode, EpisodeRepository>() {
entity.anime = animeService.findAllByLikeName(entity.anime!!.countryCode!!, entity.anime!!.name!!).firstOrNull()
?: animeService.save(entity.anime!!)

if (entity.anime?.banner.isNullOrBlank()) {
entity.anime?.banner = entity.anime?.image
animeService.update(entity.anime!!)
}

entity.number.takeIf { it == -1 }?.let {
entity.number = episodeRepository.getLastNumber(
entity.anime!!.uuid!!,
Expand Down
26 changes: 26 additions & 0 deletions src/main/resources/db/changelog/2024/01/07-changelog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.25.xsd"
objectQuotingStrategy="QUOTE_ONLY_RESERVED_WORDS">
<property global="false" name="id" value="1706090684070"/>
<property global="false" name="author" value="Ziedelth"/>

<changeSet id="${id}-1" author="${author}">
<preConditions onFail="MARK_RAN">
<tableExists tableName="anime"/>

<not>
<columnExists tableName="anime" columnName="banner"/>
</not>
</preConditions>

<addColumn tableName="anime">
<column name="banner" type="varchar(1000)">
<constraints nullable="true"/>
</column>
</addColumn>
</changeSet>
</databaseChangeLog>
1 change: 1 addition & 0 deletions src/main/resources/db/changelog/db.changelog-master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
<include file="/db/changelog/2024/01/04-changelog.xml"/>
<include file="/db/changelog/2024/01/05-changelog.xml"/>
<include file="/db/changelog/2024/01/06-changelog.xml"/>
<include file="/db/changelog/2024/01/07-changelog.xml"/>
</databaseChangeLog>
4 changes: 4 additions & 0 deletions src/main/resources/templates/admin/animes/edit.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@
<label for="image" class="form-label">Image</label>
<input type="text" class="form-control" id="image" name="image" value="${anime.image}">
</div>
<div class="col-md-6">
<label for="banner" class="form-label">Banner</label>
<input type="text" class="form-control" id="banner" name="banner" value="${anime.banner}">
</div>
<div class="col-md-6">
<label for="description" class="form-label">Description</label>
<textarea class="form-control" id="description" name="description"
Expand Down
1 change: 1 addition & 0 deletions src/test/kotlin/fr/shikkanime/OldEpisodeScraper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ private val animes = MapCache<String, AnimeDto> {
"",
"",
image,
"",
description,
simulcasts = emptyList(),
)
Expand Down

0 comments on commit a8d66d7

Please sign in to comment.