Skip to content

Commit

Permalink
Merge pull request #177 from Shikkanime/dev
Browse files Browse the repository at this point in the history
Fix FetchOldEpisodeDescriptionJob.kt
  • Loading branch information
Ziedelth authored Feb 13, 2024
2 parents ff16e8b + aed9d8e commit 0ab5f38
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.google.gson.JsonObject
import com.google.inject.Inject
import fr.shikkanime.entities.Episode
import fr.shikkanime.entities.enums.ConfigPropertyKey
import fr.shikkanime.entities.enums.CountryCode
import fr.shikkanime.entities.enums.Platform
import fr.shikkanime.services.EpisodeService
import fr.shikkanime.services.caches.ConfigCacheService
Expand Down Expand Up @@ -59,22 +60,22 @@ class FetchOldEpisodeDescriptionJob : AbstractJob() {
httpRequest.close()
}

private fun normalizeUrl(episode: Episode): String {
return when (episode.platform) {
fun normalizeUrl(platform: Platform, countryCode: CountryCode, url: String): String {
return when (platform) {
Platform.CRUN -> {
val other = "https://www.crunchyroll.com/${episode.anime?.countryCode?.name?.lowercase()}/"
episode.url!!.replace("https://www.crunchyroll.com/", other)
val other = "https://www.crunchyroll.com/${countryCode.name.lowercase()}/"
url.replace("https://www.crunchyroll.com/", other)
}

else -> episode.url!!
else -> url
}
}

private suspend fun normalizeContent(episode: Episode, httpRequest: HttpRequest, accessToken: String, cms: CrunchyrollWrapper.CMS): JsonObject? {
return when (episode.platform) {
Platform.CRUN -> {
try {
httpRequest.getBrowser(normalizeUrl(episode))
httpRequest.getBrowser(normalizeUrl(episode.platform!!, episode.anime!!.countryCode!!, episode.url!!))
} catch (e: Exception) {
return null
}
Expand All @@ -93,7 +94,7 @@ class FetchOldEpisodeDescriptionJob : AbstractJob() {
}
}

fun normalizeUrl(url: String) ="/watch/([A-Z0-9]+)/".toRegex().find(url)!!.groupValues[1]
fun normalizeUrl(url: String) = "/watch/([A-Z0-9]+)".toRegex().find(url)!!.groupValues[1]

private fun normalizeDescription(episode: Episode, content: JsonObject): String? {
var description = when (episode.platform) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/fr/shikkanime/utils/HttpRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import kotlin.system.measureTimeMillis

private const val TIMEOUT = 60_000L
private const val TIMEOUT = 15_000L
private val logger = LoggerFactory.getLogger(HttpRequest::class.java)

class HttpRequest : AutoCloseable {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package fr.shikkanime.jobs

import fr.shikkanime.utils.HttpRequest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

import org.junit.jupiter.api.Assertions.*

class FetchOldEpisodeDescriptionJobTest {
private val fetchOldEpisodeDescriptionJob = FetchOldEpisodeDescriptionJob()

Expand All @@ -16,4 +16,17 @@ class FetchOldEpisodeDescriptionJobTest {
assertEquals("GEVUZD021", fetchOldEpisodeDescriptionJob.normalizeUrl("https://www.crunchyroll.com/fr/watch/GEVUZD021/becoming-a-three-star-chef"))
assertEquals("GK9U3KWN4", fetchOldEpisodeDescriptionJob.normalizeUrl("https://www.crunchyroll.com/fr/watch/GK9U3KWN4/yukis-world"))
}

@Test
fun bug() {
val normalizeUrl = "https://www.crunchyroll.com/fr/media-918855"

val lastPage = HttpRequest().use {
it.getBrowser(normalizeUrl)
it.lastPageUrl!!
}

val id = fetchOldEpisodeDescriptionJob.normalizeUrl(lastPage)
assertEquals("GVWU07GP0", id)
}
}

0 comments on commit 0ab5f38

Please sign in to comment.