From c8e025859b9ffc6c9c9ef16111145ebc705248f5 Mon Sep 17 00:00:00 2001 From: duncte123 Date: Sun, 1 Sep 2024 14:37:55 +0200 Subject: [PATCH 1/2] Got an idea on how to fix this, it's gonna be shit --- .../me/duncte123/lyrics/GeniusClient.java | 37 ++++++++++++++++--- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/application/src/main/java/me/duncte123/lyrics/GeniusClient.java b/application/src/main/java/me/duncte123/lyrics/GeniusClient.java index 0d3779d..df47423 100644 --- a/application/src/main/java/me/duncte123/lyrics/GeniusClient.java +++ b/application/src/main/java/me/duncte123/lyrics/GeniusClient.java @@ -24,6 +24,9 @@ public class GeniusClient implements AutoCloseable { private static final ExecutorService executor = Executors.newSingleThreadExecutor(); private final HttpClientProvider httpInterfaceManager; private final String apiKey; + private static final String BROWSER_USER_AGENT = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:129.0) Gecko/20100101 Firefox/129.0"; + private static final String PRELOAD_START = "window.__PRELOADED_STATE__ = JSON.parse('"; + private static final String PRELOAD_END = "');"; public GeniusClient(String apiKey, HttpClientProvider httpProvider) { this.apiKey = apiKey; @@ -112,17 +115,41 @@ private GeniusData findGeniusData(String query) throws IOException { private String loadLyrics(String geniusUrl) throws IOException { final var request = new HttpGet(geniusUrl); + request.setHeader("user-agent", BROWSER_USER_AGENT); + try (final var response = getHttpInterface().execute(request)) { final String html = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8); - final var doc = Jsoup.parse(html); - final var lyricsContainer = doc.select("div[data-lyrics-container]").first(); + // fucking kill me + final var idx1 = html.indexOf(PRELOAD_START); + final var split1 = html.substring(idx1 + PRELOAD_START.length()); + final var idx2 = split1.indexOf(PRELOAD_END); + final var json = split1.substring(0, idx2) + .replace("\\\"", "\"") + .replace("\\'", "'") + .replace("\\\\", "\\"); + + System.out.println(json); + + final var lyrics = JsonBrowser.parse(json).get("songPage").get("lyricsData").get("body").text(); + + if (lyrics == null || lyrics.isEmpty()) { + final var doc = Jsoup.parse(html); + + final var lyricsContainer = doc.select("[data-lyrics-container]").first(); + + if (lyricsContainer == null) { + throw new RuntimeException("Could not find lyrics container, please report this to the developer"); + } - if (lyricsContainer == null) { - throw new RuntimeException("Could not find lyrics container, please report this to the developer"); + return lyricsContainer.wholeText() + .replace("

", "\n") + .replace("
", "\n") + .replace("\n\n\n", "\n") + .trim(); } - return lyricsContainer.wholeText() + return lyrics .replace("

", "\n") .replace("
", "\n") .replace("\n\n\n", "\n") From a8b7753dad638cf79982e5319e74dc270d2bd746 Mon Sep 17 00:00:00 2001 From: duncte123 Date: Sun, 1 Sep 2024 14:47:16 +0200 Subject: [PATCH 2/2] Bump ytmusic client version --- protocol/src/main/java/me/duncte123/lyrics/model/Client.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocol/src/main/java/me/duncte123/lyrics/model/Client.java b/protocol/src/main/java/me/duncte123/lyrics/model/Client.java index 7d66079..315c881 100644 --- a/protocol/src/main/java/me/duncte123/lyrics/model/Client.java +++ b/protocol/src/main/java/me/duncte123/lyrics/model/Client.java @@ -6,7 +6,7 @@ public Client(String clientName, String clientVersion) { } public Client(String hl) { - this("ANDROID_MUSIC", "6.31.55", hl); + this("ANDROID_MUSIC", "7.11.50", hl); } public Client() {