From a4fe8b0912e23d44acd7b69f881b1264a22ee276 Mon Sep 17 00:00:00 2001 From: DatLag Date: Thu, 16 Nov 2023 00:00:19 +0100 Subject: [PATCH] fix wrong season displayed and series not loading if no language available --- .../dev/datlag/burningseries/model/Series.kt | 35 ++++++++++++------- .../network/scraper/BurningSeries.kt | 6 ++-- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/model/src/commonMain/kotlin/dev/datlag/burningseries/model/Series.kt b/model/src/commonMain/kotlin/dev/datlag/burningseries/model/Series.kt index 076bb1d8..d7a0411a 100644 --- a/model/src/commonMain/kotlin/dev/datlag/burningseries/model/Series.kt +++ b/model/src/commonMain/kotlin/dev/datlag/burningseries/model/Series.kt @@ -13,32 +13,41 @@ data class Series( @SerialName("cover") val coverHref: String? = null, @SerialName("href") val href: String, @SerialName("seasonTitle") val seasonTitle: String, - @SerialName("selectedLanguage") val selectedLanguage: String, + @SerialName("selectedLanguage") val selectedLanguage: String?, @SerialName("seasons") val seasons: List, @SerialName("languages") val languages: List, @SerialName("episodes") val episodes: List ) { - val currentSeason: Season? by lazy(LazyThreadSafetyMode.NONE) { - seasons.find { + val currentSeason: Season? by lazy { + seasons.firstOrNull { it.title.equals(seasonTitle, true) - || it.title.trim().equals(seasonTitle.trim(), true) - || it.title.equals(seasonTitle.toIntOrNull()?.toString(), true) - || it.title.toIntOrNull()?.toString().equals(seasonTitle.toIntOrNull()?.toString(), true) - || it.title.equals(seasonTitle.getDigitsOrNull(), true) - || it.title.getDigitsOrNull().equals(seasonTitle, true) - || it.title.getDigitsOrNull().equals(seasonTitle.getDigitsOrNull(), true) + } ?: seasons.firstOrNull { + it.title.trim().equals(seasonTitle.trim(), true) + } ?: seasons.firstOrNull { + it.title.equals(seasonTitle.toIntOrNull()?.toString(), true) + } ?: seasons.firstOrNull { + val titleInt = it.title.toIntOrNull() + val seasonInt = seasonTitle.toIntOrNull() + + titleInt != null && seasonInt != null && titleInt == seasonInt + } ?: seasons.firstOrNull { + it.title.equals(seasonTitle.getDigitsOrNull(), true) + } ?: seasons.firstOrNull { + it.title.getDigitsOrNull().equals(seasonTitle, true) + } ?: seasons.firstOrNull { + it.title.getDigitsOrNull().equals(seasonTitle.getDigitsOrNull(), true) } } - val currentLanguage: Language? by lazy(LazyThreadSafetyMode.NONE) { + val currentLanguage: Language? by lazy { languages.find { it.value.equals(selectedLanguage, true) - || it.value.trim().equals(selectedLanguage.trim(), true) + || it.value.trim().equals(selectedLanguage?.trim(), true) } } - fun hrefBuilder(season: Int? = currentSeason?.value, language: String = currentLanguage?.value ?: selectedLanguage): String { + fun hrefBuilder(season: Int? = currentSeason?.value, language: String? = currentLanguage?.value ?: selectedLanguage): String { val hrefData = BSUtil.hrefDataFromHref( BSUtil.normalizeHref(href) ) @@ -48,7 +57,7 @@ data class Series( Triple( first = hrefData.first, second = season?.toString() ?: hrefData.second, - third = language + third = language ?: hrefData.third ) ) ) diff --git a/network/src/commonMain/kotlin/dev/datlag/burningseries/network/scraper/BurningSeries.kt b/network/src/commonMain/kotlin/dev/datlag/burningseries/network/scraper/BurningSeries.kt index 277cd8af..939e217a 100644 --- a/network/src/commonMain/kotlin/dev/datlag/burningseries/network/scraper/BurningSeries.kt +++ b/network/src/commonMain/kotlin/dev/datlag/burningseries/network/scraper/BurningSeries.kt @@ -164,13 +164,13 @@ data object BurningSeries { if (selectedLanguage.isNullOrBlank()) { selectedLanguage = selectedLanguageValue if (selectedLanguage.isNullOrBlank()) { - selectedLanguage = languages.firstOrNull()?.value ?: return null + selectedLanguage = languages.firstOrNull()?.value } } val episodesDoc = doc.querySelector(".serie")?.querySelector(".episodes")?.querySelectorAll("tr") ?: emptyList() val episodeInfoList = episodesDoc.mapNotNull { episodesElement -> - val episodeList = episodesElement.querySelectorAll("td").mapNotNull { it.querySelector("a") }.mapNotNull { data -> + val episodeList = episodesElement.querySelectorAll("td").mapNotNull { it.querySelector("a") }.map { data -> val text = data.querySelector("a")?.textContent() ?: String() val episodeHref = BSUtil.normalizeHref(data.querySelector("a")?.getHref() ?: String()) @@ -217,7 +217,7 @@ data object BurningSeries { href = docHref, seasonTitle = titleSeason ?: String(), seasons = seasons, - selectedLanguage = selectedLanguage?.trim() ?: return null, + selectedLanguage = selectedLanguage?.trim(), languages = languages, episodes = episodeInfoList )