Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(en/uhdmovies): Fix crashes when fetching the current baseUrl #2605

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/en/uhdmovies/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ext {
extName = 'UHD Movies'
pkgNameSuffix = 'en.uhdmovies'
extClass = '.UHDMovies'
extVersionCode = 18
extVersionCode = 19
libVersion = '13'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,29 @@ class UHDMovies : ConfigurableAnimeSource, ParsedAnimeHttpSource() {
override val name = "UHD Movies"

override val baseUrl by lazy {
val url = preferences.getString(PREF_DOMAIN_KEY, PREF_DOMAIN_DEFAULT)!!
runBlocking {
withContext(Dispatchers.Default) {
client.newBuilder()
.followRedirects(false)
.build()
.newCall(GET("$url/")).execute().use { resp ->
when (resp.code) {
301 -> {
(resp.headers["location"]?.substringBeforeLast("/") ?: url).also {
preferences.edit().putString(PREF_DOMAIN_KEY, it).apply()
preferences.getString(PREF_DOMAIN_KEY, PREF_DOMAIN_DEFAULT)!!
}

private val currentBaseUrl by lazy {
runCatching {
runBlocking {
withContext(Dispatchers.Default) {
client.newBuilder()
.followRedirects(false)
.build()
.newCall(GET("$baseUrl/")).execute().use { resp ->
when (resp.code) {
301 -> {
(resp.headers["location"]?.substringBeforeLast("/") ?: baseUrl).also {
preferences.edit().putString(PREF_DOMAIN_KEY, it).apply()
}
}
else -> baseUrl
}
else -> url
}
}
}
}
}
}.getOrDefault(baseUrl)
}

override val lang = "en"
Expand All @@ -71,7 +76,7 @@ class UHDMovies : ConfigurableAnimeSource, ParsedAnimeHttpSource() {
}

// ============================== Popular ===============================
override fun popularAnimeRequest(page: Int): Request = GET("$baseUrl/page/$page/")
override fun popularAnimeRequest(page: Int): Request = GET("$currentBaseUrl/page/$page/")

override fun popularAnimeSelector(): String = "div#content div.gridlove-posts > div.layout-masonry"

Expand All @@ -97,7 +102,7 @@ class UHDMovies : ConfigurableAnimeSource, ParsedAnimeHttpSource() {
// =============================== Search ===============================
override fun searchAnimeRequest(page: Int, query: String, filters: AnimeFilterList): Request {
val cleanQuery = query.replace(" ", "+").lowercase()
return GET("$baseUrl/page/$page/?s=$cleanQuery")
return GET("$currentBaseUrl/page/$page/?s=$cleanQuery")
}

override fun searchAnimeSelector(): String = popularAnimeSelector()
Expand All @@ -116,6 +121,8 @@ class UHDMovies : ConfigurableAnimeSource, ParsedAnimeHttpSource() {
}

// ============================== Episodes ==============================
override fun episodeListRequest(anime: SAnime) = GET(currentBaseUrl + anime.url, headers)

private fun Regex.firstValue(text: String) =
find(text)?.groupValues?.get(1)?.let { Pair(text, it) }

Expand Down