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.

Read Spicy Lyrics from the answer's envelope, and log why a key is refused

Lelonio38824fea8546
diff --git a/app/src/main/java/dev/lelonio/square/backend/lyrics/SpicyLyrics.kt b/app/src/main/java/dev/lelonio/square/backend/lyrics/SpicyLyrics.kt
index 977ff28..13cc218 100644
--- a/app/src/main/java/dev/lelonio/square/backend/lyrics/SpicyLyrics.kt
+++ b/app/src/main/java/dev/lelonio/square/backend/lyrics/SpicyLyrics.kt
@@ -59,7 +59,14 @@ object SpicyLyrics {
null
}
!response.isSuccessful -> {
- if (response.code != 404) android.util.Log.w(TAG, "HTTP ${response.code} for $id")
+ // The API says why in `error` (key_not_found,
+ // origin_not_allowed, ...): logged, never the key.
+ if (response.code != 404) {
+ val reason = runCatching {
+ JSONObject(response.body?.string().orEmpty()).optJSONObject("Body")?.optString("error")
+ }.getOrNull()
+ android.util.Log.w(TAG, "HTTP ${response.code} for $id: $reason")
+ }
null
}
else -> response.body?.string()
@@ -69,7 +76,13 @@ object SpicyLyrics {
runCatching { parse(JSONObject(body)) }.getOrNull()
}
- internal fun parse(json: JSONObject): Lyrics? {
+ /**
+ * The lyrics in an answer. They come inside an envelope, under `Body`
+ * beside `Status` and `Type`: read at the top, where `Type` is "object",
+ * every song came back with no lyrics.
+ */
+ internal fun parse(answer: JSONObject): Lyrics? {
+ val json = answer.optJSONObject("Body") ?: answer
val credit = credit(json)
val lines = when (json.optString("Type")) {
"Syllable" -> json.optJSONArray("Content").objects().mapNotNull { vocal ->
diff --git a/app/src/test/java/dev/lelonio/square/backend/lyrics/SpicyLyricsTest.kt b/app/src/test/java/dev/lelonio/square/backend/lyrics/SpicyLyricsTest.kt
index 53905fb..34f185b 100644
--- a/app/src/test/java/dev/lelonio/square/backend/lyrics/SpicyLyricsTest.kt
+++ b/app/src/test/java/dev/lelonio/square/backend/lyrics/SpicyLyricsTest.kt
@@ -33,6 +33,16 @@ class SpicyLyricsTest {
assertNull(lyrics.credit?.maker)
}
+ @Test fun theLyricsAreReadFromTheEnvelope() {
+ val lyrics = SpicyLyrics.parse(
+ JSONObject(
+ """{"Body":{"Type":"Line","source":"spicy_lyrics","Content":[{"Type":"Vocal","Text":"Wrapped","StartTime":1,"EndTime":2}]},"Status":200,"Type":"object"}""",
+ ),
+ )!!
+ assertEquals("Wrapped", lyrics.lines.single().text)
+ assertEquals(1000L, lyrics.lines.single().startTimeMs)
+ }
+
@Test fun lineAndStaticLyrics() {
val line = SpicyLyrics.parse(
JSONObject("""{"Type":"Line","source":"apple_music","Content":[{"Type":"Vocal","Text":"A line","StartTime":4.25,"EndTime":6}]}"""),