mirror of
https://github.com/IllusionMan1212/lyrics-grabbr.git
synced 2026-10-24 02:12:27 +00:00
fix: don't crash when it returns an http error
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.illusionman1212.lyricsgrabbr.data
|
||||
|
||||
import android.net.Uri
|
||||
import org.jsoup.HttpStatusException
|
||||
import org.jsoup.Jsoup
|
||||
import org.jsoup.internal.StringUtil
|
||||
import org.jsoup.nodes.Element
|
||||
@@ -10,9 +11,12 @@ import org.jsoup.select.NodeVisitor
|
||||
|
||||
class LyricsRepository {
|
||||
fun fetchLyricsFromGenius(url: Uri): String {
|
||||
val doc = Jsoup.connect(url.toString()).userAgent(USER_AGENT).get()
|
||||
val divs = doc.select("div[data-lyrics-container]")
|
||||
return try {
|
||||
val doc = Jsoup.connect(url.toString())
|
||||
.userAgent(USER_AGENT)
|
||||
.get()
|
||||
|
||||
val divs = doc.select("div[data-lyrics-container]")
|
||||
val lyrics = StringUtil.borrowBuilder()
|
||||
|
||||
divs.forEach {
|
||||
@@ -44,7 +48,23 @@ class LyricsRepository {
|
||||
lyrics.append("\n")
|
||||
}
|
||||
|
||||
return lyrics.toString().replace("\n\n\n", "\n\n")
|
||||
lyrics.toString().replace("\n\n\n", "\n\n")
|
||||
|
||||
} catch (e: HttpStatusException) {
|
||||
when (e.statusCode) {
|
||||
403 -> {
|
||||
println("Access Forbidden: HTTP 403")
|
||||
"Error 403: Cloudflare got in the way, try disabling the VPN or Proxy if enabled"
|
||||
}
|
||||
else -> {
|
||||
println("HTTP Error: ${e.statusCode}")
|
||||
"HTTP Error: ${e.statusCode}"
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
println("An error occurred: ${e.message}")
|
||||
"An error occurred while fetching lyrics."
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
Reference in New Issue
Block a user