Skip to content

Commit

Permalink
Merge pull request #166 from Shikkanime/dev
Browse files Browse the repository at this point in the history
Fix bugs
  • Loading branch information
Ziedelth authored Feb 11, 2024
2 parents 7afc11f + c19095d commit 7f4e77e
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import fr.shikkanime.utils.ObjectParser.getAsBoolean
import fr.shikkanime.utils.ObjectParser.getAsInt
import fr.shikkanime.utils.ObjectParser.getAsLong
import fr.shikkanime.utils.ObjectParser.getAsString
import fr.shikkanime.utils.StringUtils
import fr.shikkanime.wrappers.AnimationDigitalNetworkWrapper
import java.io.File
import java.time.ZonedDateTime
Expand Down Expand Up @@ -71,7 +72,7 @@ class AnimationDigitalNetworkPlatform :
if ((genres.isEmpty || !genres.any { it.asString.startsWith("Animation ", true) }) && !contains) throw Exception("Anime is not an animation")

var isSimulcasted = show.getAsBoolean("simulcast") == true ||
show.getAsString("firstReleaseYear")!! in (0..1).map { (zonedDateTime.year - it).toString() } ||
show.getAsString("firstReleaseYear") in (0..1).map { (zonedDateTime.year - it).toString() } ||
contains

val descriptionLowercase = animeDescription.lowercase()
Expand Down Expand Up @@ -124,6 +125,7 @@ class AnimationDigitalNetworkPlatform :
image = animeImage,
banner = animeBanner,
description = animeDescription,
slug = StringUtils.toSlug(StringUtils.getShortName(animeName)),
),
episodeType = episodeType,
langType = langType,
Expand Down
11 changes: 7 additions & 4 deletions src/main/kotlin/fr/shikkanime/platforms/CrunchyrollPlatform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import fr.shikkanime.utils.ObjectParser
import fr.shikkanime.utils.ObjectParser.getAsInt
import fr.shikkanime.utils.ObjectParser.getAsLong
import fr.shikkanime.utils.ObjectParser.getAsString
import fr.shikkanime.utils.StringUtils
import fr.shikkanime.wrappers.CrunchyrollWrapper
import io.ktor.client.statement.*
import io.ktor.http.*
Expand Down Expand Up @@ -269,7 +270,7 @@ class CrunchyrollPlatform : AbstractPlatform<CrunchyrollConfiguration, CountryCo
simulcasts.resetWithNewDuration(Duration.ofMinutes(configuration!!.simulcastCheckDelayInMinutes))
}

private fun convertJsonEpisode(countryCode: CountryCode, jsonObject: JsonObject): Episode {
fun convertJsonEpisode(countryCode: CountryCode, jsonObject: JsonObject): Episode {
val episodeMetadata = jsonObject.getAsJsonObject("episode_metadata")

val animeName = requireNotNull(episodeMetadata.getAsString("series_title")) { "Anime name is null" }
Expand All @@ -295,7 +296,7 @@ class CrunchyrollPlatform : AbstractPlatform<CrunchyrollConfiguration, CountryCo
requireNotNull(episodeMetadata.getAsString("premium_available_date")?.let { ZonedDateTime.parse(it) }) { "Release date is null" }

val season = episodeMetadata.getAsInt("season_number") ?: 1
val number = episodeMetadata.getAsInt("sequence_number") ?: -1
val number = episodeMetadata.getAsInt("episode_number") ?: -1
val episodeType = if (number == -1) EpisodeType.SPECIAL else EpisodeType.EPISODE

val title = jsonObject.getAsString("title")
Expand Down Expand Up @@ -328,7 +329,8 @@ class CrunchyrollPlatform : AbstractPlatform<CrunchyrollConfiguration, CountryCo
releaseDateTime = releaseDate,
image = crunchyrollAnimeContent.image,
banner = crunchyrollAnimeContent.banner,
description = crunchyrollAnimeContent.description
description = crunchyrollAnimeContent.description,
slug = StringUtils.toSlug(StringUtils.getShortName(animeName)),
),
episodeType = episodeType,
langType = langType,
Expand Down Expand Up @@ -432,7 +434,8 @@ class CrunchyrollPlatform : AbstractPlatform<CrunchyrollConfiguration, CountryCo
releaseDateTime = releaseDate,
image = crunchyrollAnimeContent.image,
banner = crunchyrollAnimeContent.banner,
description = crunchyrollAnimeContent.description
description = crunchyrollAnimeContent.description,
slug = StringUtils.toSlug(StringUtils.getShortName(animeName)),
),
episodeType = episodeType,
langType = langType,
Expand Down
6 changes: 2 additions & 4 deletions src/main/kotlin/fr/shikkanime/platforms/DisneyPlusPlatform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ import fr.shikkanime.entities.enums.LangType
import fr.shikkanime.entities.enums.Platform
import fr.shikkanime.exceptions.AnimeException
import fr.shikkanime.platforms.configuration.DisneyPlusConfiguration
import fr.shikkanime.utils.HttpRequest
import fr.shikkanime.utils.ObjectParser
import fr.shikkanime.utils.*
import fr.shikkanime.utils.ObjectParser.getAsBoolean
import fr.shikkanime.utils.ObjectParser.getAsInt
import fr.shikkanime.utils.ObjectParser.getAsLong
import fr.shikkanime.utils.ObjectParser.getAsString
import fr.shikkanime.utils.isEqualOrAfter
import fr.shikkanime.utils.withUTC
import io.ktor.client.statement.*
import java.io.File
import java.time.LocalTime
Expand Down Expand Up @@ -175,6 +172,7 @@ class DisneyPlusPlatform :
image = animeImage,
banner = animeBanner,
description = animeDescription,
slug = StringUtils.toSlug(StringUtils.getShortName(animeName)),
),
episodeType = EpisodeType.EPISODE,
langType = langType,
Expand Down
6 changes: 2 additions & 4 deletions src/main/kotlin/fr/shikkanime/platforms/NetflixPlatform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import fr.shikkanime.entities.enums.EpisodeType
import fr.shikkanime.entities.enums.LangType
import fr.shikkanime.entities.enums.Platform
import fr.shikkanime.platforms.configuration.NetflixConfiguration
import fr.shikkanime.utils.EncryptionManager
import fr.shikkanime.utils.HttpRequest
import fr.shikkanime.utils.isEqualOrAfter
import fr.shikkanime.utils.withUTC
import fr.shikkanime.utils.*
import java.io.File
import java.time.LocalTime
import java.time.ZonedDateTime
Expand Down Expand Up @@ -58,6 +55,7 @@ class NetflixPlatform : AbstractPlatform<NetflixConfiguration, CountryCodeNetfli
image = key.netflixSimulcast.image,
banner = animeBanner,
description = animeDescription,
slug = StringUtils.toSlug(StringUtils.getShortName(animeName)),
),
episodeType = EpisodeType.EPISODE,
langType = LangType.SUBTITLES,
Expand Down
96 changes: 86 additions & 10 deletions src/test/kotlin/fr/shikkanime/OldEpisodeScraper.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package fr.shikkanime

import fr.shikkanime.entities.Config
import fr.shikkanime.entities.Episode
import fr.shikkanime.entities.enums.ConfigPropertyKey
import fr.shikkanime.entities.enums.CountryCode
import fr.shikkanime.platforms.AnimationDigitalNetworkPlatform
import fr.shikkanime.platforms.CrunchyrollPlatform
import fr.shikkanime.services.ConfigService
import fr.shikkanime.services.EpisodeService
import fr.shikkanime.utils.Constant
import fr.shikkanime.utils.HttpRequest
import fr.shikkanime.utils.ObjectParser.getAsString
import fr.shikkanime.wrappers.CrunchyrollWrapper
import kotlinx.coroutines.runBlocking
import java.time.LocalDate
import java.time.Period
Expand Down Expand Up @@ -48,28 +55,97 @@ fun main() {
.map { ZonedDateTime.of(it.atTime(23, 59, 59), Constant.utcZoneId) }
.sorted()

val simulcasts = dates.map {
"${Constant.seasons[(it.monthValue - 1) / 3]}-${it.year}".lowercase().replace("autumn", "fall")
}.toSet()

println("Checking ${dates.size} dates...")
println("Simulcasts: $simulcasts")

val episodes = mutableListOf<Episode>()
val configService = Constant.injector.getInstance(ConfigService::class.java)

configService.findByName(ConfigPropertyKey.USE_CRUNCHYROLL_API.key)?.let {
configService.delete(it)
}

configService.save(Config(propertyKey = ConfigPropertyKey.USE_CRUNCHYROLL_API.key, propertyValue = "true"))
val episodeService = Constant.injector.getInstance(EpisodeService::class.java)
val adnPlatform = Constant.injector.getInstance(AnimationDigitalNetworkPlatform::class.java)
val crunchyrollPlatform = Constant.injector.getInstance(CrunchyrollPlatform::class.java)

// dates.forEach { date ->
// runBlocking { adnPlatform.fetchApiContent(CountryCode.FR, date) }.forEach { episodeJson ->
// try {
// episodes.addAll(adnPlatform.convertEpisode(
// CountryCode.FR,
// episodeJson.asJsonObject,
// date
// ))
// } catch (e: Exception) {
// e.printStackTrace()
// }
// }
// }

val minDate = dates.minOrNull()!!
val maxDate = dates.maxOrNull()!!
val accessToken = runBlocking { CrunchyrollWrapper.getAnonymousAccessToken() }
var page = 1
val fetch = 1000

val series = simulcasts.flatMap { simulcastId ->
runBlocking {
CrunchyrollWrapper.getBrowse(
CountryCode.FR.locale,
accessToken,
sortBy = CrunchyrollWrapper.SortType.POPULARITY,
type = CrunchyrollWrapper.MediaType.SERIES,
100,
simulcast = simulcastId
)
}
}.map { jsonObject -> jsonObject.getAsString("title")!!.lowercase() }.toSet()
println("Simulcasts: $series")

crunchyrollPlatform.simulcasts[CountryCode.FR] = series

dates.forEach { date ->
runBlocking { adnPlatform.fetchApiContent(CountryCode.FR, date) }.forEach { episodeJson ->
while (true) {
val fetchApi = runBlocking { CrunchyrollWrapper.getBrowse(CountryCode.FR.locale, accessToken, size = fetch, start = (page - 1) * fetch) }.toMutableList()

val episodeDates = fetchApi
.map { episodeJson ->
val episodeMetadata = episodeJson.getAsJsonObject("episode_metadata")
episodeJson to requireNotNull(episodeMetadata.getAsString("premium_available_date")?.let { ZonedDateTime.parse(it) }) { "Release date is null" }
}.toMutableList()

val lastDate = episodeDates.maxByOrNull { it.second }!!.second
val firstDate = episodeDates.minByOrNull { it.second }!!.second
episodeDates.removeIf { it.second !in minDate..maxDate }

episodeDates.forEach { (episodeJson, _) ->
try {
adnPlatform.convertEpisode(
episodes.add(crunchyrollPlatform.convertJsonEpisode(
CountryCode.FR,
episodeJson.asJsonObject,
date
).forEach {
episodeService.save(it)
}
} catch (e: Exception) {
e.printStackTrace()
episodeJson
))
} catch (_: Exception) {

}
}

if (lastDate.isBefore(minDate) || firstDate.isAfter(maxDate)) {
break
}

page++
}

httpRequest.close()

episodes.forEach { episode ->
episodeService.save(episode)
}

println("Take ${(System.currentTimeMillis() - start) / 1000}s to check ${dates.size} dates")
}

0 comments on commit 7f4e77e

Please sign in to comment.