Skip to content

Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.

Add Better Lyrics and KuGou as lyrics sources

Lelonio77893a9c4fc0
diff --git a/app/src/main/java/dev/lelonio/square/backend/lyrics/BetterLyrics.kt b/app/src/main/java/dev/lelonio/square/backend/lyrics/BetterLyrics.kt
new file mode 100644
index 0000000..10bbebc
--- /dev/null
+++ b/app/src/main/java/dev/lelonio/square/backend/lyrics/BetterLyrics.kt
@@ -0,0 +1,67 @@
+package dev.lelonio.square.backend.lyrics
+
+import dev.lelonio.square.data.Lyrics
+import java.net.URLEncoder
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.withContext
+import okhttp3.OkHttpClient
+import okhttp3.Request
+import org.json.JSONObject
+
+/**
+ * Word-timed lyrics from Better Lyrics, the YouTube Music extension's service.
+ *
+ * One request, by title, artist and length, answered with an Apple-style TTML
+ * document timed to the word, so the parsing, the word timings and any
+ * translation come from [Ttml] as they do for the archive in [Lossless]. A
+ * different archive from that one: where one misses a song the other often
+ * has it, which is why both are asked.
+ *
+ * A miss is ordinary and answers null; so does anything that is not a
+ * document this can read.
+ */
+object BetterLyrics {
+
+ private val http = OkHttpClient()
+
+ suspend fun lyrics(title: String, artist: String, durationMs: Long): Lyrics? =
+ withContext(Dispatchers.IO) {
+ val cleaned = title.songTitle()
+ if (cleaned.isBlank()) return@withContext null
+ val url = buildString {
+ append(API_URL).append("?s=").append(encode(cleaned))
+ append("&a=").append(encode(artist.primaryArtist()))
+ if (durationMs > 0) append("&d=").append(durationMs / 1000)
+ }
+ val body = get(url) ?: return@withContext null
+ val ttml = runCatching { JSONObject(body).optString("ttml") }.getOrNull()
+ ?.takeIf { it.startsWith("<") } ?: return@withContext null
+ val parsed = Ttml.parse(ttml)
+ android.util.Log.i(
+ TAG,
+ "$cleaned: ${parsed?.lines?.size ?: 0} lines, " +
+ "${parsed?.lines?.count { it.words.isNotEmpty() } ?: 0} timed by word",
+ )
+ parsed?.takeIf { it.lines.isNotEmpty() }
+ }
+
+ /** Null on anything but a 200, which includes the 404 for "no match". */
+ private fun get(url: String): String? = runCatching {
+ val request = Request.Builder()
+ .url(url)
+ .header("Accept", "application/json")
+ .header("User-Agent", USER_AGENT)
+ .build()
+ http.newCall(request).execute().use { response ->
+ if (!response.isSuccessful) null else response.body?.string()?.takeIf(String::isNotBlank)
+ }
+ }.getOrNull()
+
+ private fun encode(value: String): String = URLEncoder.encode(value, "UTF-8")
+
+ private const val TAG = "BetterLyrics"
+
+ private const val API_URL = "https://lyrics-api.boidu.dev/getLyrics"
+
+ private const val USER_AGENT = "Square (Android music player)"
+}
diff --git a/app/src/main/java/dev/lelonio/square/backend/lyrics/KuGou.kt b/app/src/main/java/dev/lelonio/square/backend/lyrics/KuGou.kt
new file mode 100644
index 0000000..531d96c
--- /dev/null
+++ b/app/src/main/java/dev/lelonio/square/backend/lyrics/KuGou.kt
@@ -0,0 +1,141 @@
+package dev.lelonio.square.backend.lyrics
+
+import dev.lelonio.square.data.LyricLine
+import dev.lelonio.square.data.LyricWord
+import dev.lelonio.square.data.Lyrics
+import java.net.URLEncoder
+import kotlin.math.abs
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.withContext
+import okhttp3.OkHttpClient
+import okhttp3.Request
+import org.json.JSONObject
+
+/**
+ * Word-timed lyrics from KuGou, strongest on Chinese, Korean and Japanese
+ * music, which the other sources hold little of.
+ *
+ * Two requests: a search by artist, title and length that names candidates
+ * with a key each, then the lyrics of the closest one, in KRC. A KRC file is
+ * encrypted the same way by every client: base64, a "krc1" header, a fixed
+ * sixteen-byte XOR, then zlib. Inside, each line is `[start,length]` and each
+ * word `<offset,length,0>text`, the offsets counted from the line's start.
+ *
+ * The first lines of a KRC are credits ("Lyrics by: …"), sung by nobody and
+ * timed as if they were; they are left out.
+ */
+object KuGou {
+
+ private val http = OkHttpClient()
+
+ suspend fun lyrics(title: String, artist: String, durationMs: Long): Lyrics? =
+ withContext(Dispatchers.IO) {
+ val cleaned = title.songTitle()
+ if (cleaned.isBlank()) return@withContext null
+ val candidate = search(cleaned, artist.primaryArtist(), durationMs) ?: return@withContext null
+ val krc = download(candidate.first, candidate.second) ?: return@withContext null
+ parse(krc, cleaned)?.also {
+ android.util.Log.i(TAG, "$cleaned: ${it.lines.size} lines, ${it.lines.count { l -> l.words.isNotEmpty() }} timed by word")
+ }
+ }
+
+ /** The id and key of the candidate closest in length, or null. */
+ private fun search(title: String, artist: String, durationMs: Long): Pair<String, String>? {
+ val url = buildString {
+ append(SEARCH_URL).append("?ver=1&man=yes&client=pc&hash=")
+ append("&keyword=").append(encode("$artist - $title"))
+ if (durationMs > 0) append("&duration=").append(durationMs)
+ }
+ val body = get(url) ?: return null
+ val candidates = runCatching { JSONObject(body).optJSONArray("candidates") }.getOrNull() ?: return null
+ val best = (0 until candidates.length())
+ .mapNotNull { candidates.optJSONObject(it) }
+ .filter { it.optString("id").isNotEmpty() && it.optString("accesskey").isNotEmpty() }
+ // Of a length that can be the same recording; the search answers
+ // something for nearly everything, and a cover or a live take timed
+ // to a different master would sing along out of step.
+ .filter { durationMs <= 0 || abs(it.optLong("duration") - durationMs) <= MAX_DRIFT_MS }
+ .minByOrNull { if (durationMs > 0) abs(it.optLong("duration") - durationMs) else 0L }
+ ?: return null
+ return best.optString("id") to best.optString("accesskey")
+ }
+
+ private fun download(id: String, key: String): String? {
+ val url = "$DOWNLOAD_URL?ver=1&client=pc&fmt=krc&charset=utf8&id=${encode(id)}&accesskey=${encode(key)}"
+ val body = get(url) ?: return null
+ val content = runCatching { JSONObject(body).optString("content") }.getOrNull()
+ ?.takeIf { it.isNotEmpty() } ?: return null
+ return runCatching { decrypt(android.util.Base64.decode(content, android.util.Base64.DEFAULT)) }.getOrNull()
+ }
+
+ /** KRC's own encryption, the same in every client that reads it. */
+ internal fun decrypt(raw: ByteArray): String? {
+ if (raw.size <= 4 || String(raw, 0, 4, Charsets.US_ASCII) != "krc1") return null
+ val body = ByteArray(raw.size - 4) { i -> (raw[i + 4].toInt() xor KEY[i % KEY.size]).toByte() }
+ val inflater = java.util.zip.Inflater()
+ inflater.setInput(body)
+ val out = java.io.ByteArrayOutputStream()
+ val buffer = ByteArray(8192)
+ while (!inflater.finished()) {
+ val n = inflater.inflate(buffer)
+ if (n == 0 && (inflater.needsInput() || inflater.needsDictionary())) break
+ out.write(buffer, 0, n)
+ }
+ inflater.end()
+ return out.toString("UTF-8")
+ }
+
+ /**
+ * The lines and their words, credits left out.
+ *
+ * A credit is any line before the first real lyric that names the song
+ * itself or carries a colon (including the full-width one Chinese text
+ * uses): "Lyrics by:", "作词:".
+ */
+ internal fun parse(krc: String, title: String): Lyrics? {
+ val lines = krc.lineSequence().mapNotNull { line ->
+ val head = LINE.find(line) ?: return@mapNotNull null
+ val start = head.groupValues[1].toLong()
+ val pieces = WORD.findAll(line.substring(head.range.last + 1)).toList()
+ // The line keeps the spaces between the pieces; the words do not,
+ // as TTML's do not either: the view finds each word in the line.
+ val text = pieces.joinToString("") { it.groupValues[3] }.trim()
+ val words = pieces.mapNotNull { word ->
+ val offset = word.groupValues[1].toLong()
+ val length = word.groupValues[2].toLong()
+ word.groupValues[3].trim().takeIf { it.isNotEmpty() }
+ ?.let { LyricWord(start + offset, start + offset + length, it) }
+ }
+ if (text.isEmpty()) null else LyricLine(startTimeMs = start, text = text, words = words)
+ }.toList()
+ val firstSung = lines.indexOfFirst { line ->
+ val text = line.text
+ !(text.contains(':') || text.contains(':') ||
+ text.startsWith(title, ignoreCase = true) && text.contains(" - "))
+ }
+ val sung = if (firstSung < 0) emptyList() else lines.drop(firstSung)
+ return sung.takeIf { it.isNotEmpty() }?.let { Lyrics(lines = it, synced = true) }
+ }
+
+ private fun get(url: String): String? = runCatching {
+ val request = Request.Builder().url(url).header("User-Agent", USER_AGENT).build()
+ http.newCall(request).execute().use { response ->
+ if (!response.isSuccessful) null else response.body?.string()?.takeIf(String::isNotBlank)
+ }
+ }.getOrNull()
+
+ private fun encode(value: String): String = URLEncoder.encode(value, "UTF-8")
+
+ private val LINE = Regex("""^\[(\d+),(\d+)]""")
+ private val WORD = Regex("""<(\d+),(\d+),\d+>([^<]*)""")
+
+ private val KEY = intArrayOf(64, 71, 97, 119, 94, 50, 116, 71, 81, 54, 49, 45, 206, 210, 110, 105)
+
+ /** Further apart than this, the candidate is another recording. */
+ private const val MAX_DRIFT_MS = 4_000L
+
+ private const val TAG = "KuGou"
+ private const val SEARCH_URL = "https://lyrics.kugou.com/search"
+ private const val DOWNLOAD_URL = "https://lyrics.kugou.com/download"
+ private const val USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
+}
diff --git a/app/src/test/java/dev/lelonio/square/backend/lyrics/KuGouTest.kt b/app/src/test/java/dev/lelonio/square/backend/lyrics/KuGouTest.kt
new file mode 100644
index 0000000..63a9b65
--- /dev/null
+++ b/app/src/test/java/dev/lelonio/square/backend/lyrics/KuGouTest.kt
@@ -0,0 +1,36 @@
+package dev.lelonio.square.backend.lyrics
+
+import org.junit.Assert.*
+import org.junit.Test
+
+class KuGouTest {
+ private val krc = """
+ [ti:Blinding Lights]
+ [ar:The Weeknd]
+ [0,1756]<0,439,0>Blinding <439,439,0>Lights - <878,439,0>The <1317,439,0>Weeknd
+ [1757,4829]<0,439,0>Lyrics <439,439,0>by:<878,439,0>Max <1317,439,0>Martin
+ [14075,13597]<0,177,0>Yeah
+ [27672,2784]<0,200,0>I've <200,144,0>been <344,176,0>tryna <520,632,0>call
+ """.trimIndent()
+
+ @Test fun readsLinesAndWordsAndLeavesOutTheCredits() {
+ val lyrics = KuGou.parse(krc, "Blinding Lights")!!
+ assertEquals(listOf("Yeah", "I've been tryna call"), lyrics.lines.map { it.text })
+ val call = lyrics.lines[1]
+ assertEquals(27672L, call.startTimeMs)
+ assertEquals(listOf("I've", "been", "tryna", "call"), call.words.map { it.text })
+ assertEquals(27672L + 520, call.words[3].startMs)
+ assertEquals(27672L + 520 + 632, call.words[3].endMs)
+ assertTrue(lyrics.synced)
+ }
+
+ @Test fun decryptsWhatKuGouEncrypts() {
+ val key = intArrayOf(64, 71, 97, 119, 94, 50, 116, 71, 81, 54, 49, 45, 206, 210, 110, 105)
+ val deflater = java.util.zip.Deflater().apply { setInput(krc.toByteArray()); finish() }
+ val packed = ByteArray(4096).let { buffer -> buffer.copyOf(deflater.deflate(buffer)) }
+ val body = ByteArray(packed.size) { i -> (packed[i].toInt() xor key[i % key.size]).toByte() }
+ val raw = "krc1".toByteArray() + body
+ assertEquals(krc, KuGou.decrypt(raw))
+ assertNull(KuGou.decrypt("nope".toByteArray()))
+ }
+}