mirror of
https://github.com/IllusionMan1212/lyrics-grabbr.git
synced 2026-10-24 18:12:27 +00:00
Compare commits
2
Commits
2f056d452e
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
78880e12ff | ||
|
|
08cf7b613e |
@@ -1,6 +1,7 @@
|
|||||||
package com.illusionman1212.lyricsgrabbr.data
|
package com.illusionman1212.lyricsgrabbr.data
|
||||||
|
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
|
import org.jsoup.HttpStatusException
|
||||||
import org.jsoup.Jsoup
|
import org.jsoup.Jsoup
|
||||||
import org.jsoup.internal.StringUtil
|
import org.jsoup.internal.StringUtil
|
||||||
import org.jsoup.nodes.Element
|
import org.jsoup.nodes.Element
|
||||||
@@ -10,41 +11,60 @@ import org.jsoup.select.NodeVisitor
|
|||||||
|
|
||||||
class LyricsRepository {
|
class LyricsRepository {
|
||||||
fun fetchLyricsFromGenius(url: Uri): String {
|
fun fetchLyricsFromGenius(url: Uri): String {
|
||||||
val doc = Jsoup.connect(url.toString()).userAgent(USER_AGENT).get()
|
return try {
|
||||||
val divs = doc.select("div[data-lyrics-container]")
|
val doc = Jsoup.connect(url.toString())
|
||||||
|
.userAgent(USER_AGENT)
|
||||||
|
.get()
|
||||||
|
|
||||||
val lyrics = StringUtil.borrowBuilder()
|
val divs = doc.select("div[data-lyrics-container]")
|
||||||
|
val lyrics = StringUtil.borrowBuilder()
|
||||||
|
|
||||||
divs.forEach {
|
divs.forEach {
|
||||||
it.traverse(object : NodeVisitor {
|
it.traverse(object : NodeVisitor {
|
||||||
override fun head(node: Node, depth: Int) {
|
override fun head(node: Node, depth: Int) {
|
||||||
if (node is TextNode) {
|
if (node is TextNode) {
|
||||||
lyrics.append(node.text().trim())
|
lyrics.append(node.text().trim())
|
||||||
// append space here to get proper spacing when there's a <i> tag or other inline text tags
|
// append space here to get proper spacing when there's a <i> tag or other inline text tags
|
||||||
lyrics.append(" ")
|
lyrics.append(" ")
|
||||||
} else if (node is Element) {
|
} else if (node is Element) {
|
||||||
if (lyrics.isNotEmpty() && node.normalName() == "br") {
|
if (lyrics.isNotEmpty() && node.normalName() == "br") {
|
||||||
lyrics.append("\n")
|
lyrics.append("\n")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
override fun tail(node: Node, depth: Int) {
|
override fun tail(node: Node, depth: Int) {
|
||||||
// since we append a space after every text node, we only append a newline if
|
// since we append a space after every text node, we only append a newline if
|
||||||
// the last char isn't a space
|
// the last char isn't a space
|
||||||
val lastCharIsWhitespace = lyrics.isNotEmpty() && lyrics[lyrics.length - 1] == ' '
|
val lastCharIsWhitespace = lyrics.isNotEmpty() && lyrics[lyrics.length - 1] == ' '
|
||||||
if (node is Element) {
|
if (node is Element) {
|
||||||
if (node.isBlock && node.nextSibling() is TextNode && !lastCharIsWhitespace) {
|
if (node.isBlock && node.nextSibling() is TextNode && !lastCharIsWhitespace) {
|
||||||
lyrics.append("\n")
|
lyrics.append("\n")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
})
|
|
||||||
|
|
||||||
lyrics.append("\n")
|
lyrics.append("\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."
|
||||||
}
|
}
|
||||||
|
|
||||||
return lyrics.toString().replace("\n\n\n", "\n\n")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
The app listens to your notifications and provides lyrics for the currently playing song.
|
||||||
|
It supports any player that uses the MediaSession API.
|
||||||
Reference in New Issue
Block a user