Skip to content

Commit c8e0258

Browse files
committed
Got an idea on how to fix this, it's gonna be shit
1 parent 157e4e9 commit c8e0258

1 file changed

Lines changed: 32 additions & 5 deletions

File tree

application/src/main/java/me/duncte123/lyrics/GeniusClient.java

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public class GeniusClient implements AutoCloseable {
2424
private static final ExecutorService executor = Executors.newSingleThreadExecutor();
2525
private final HttpClientProvider httpInterfaceManager;
2626
private final String apiKey;
27+
private static final String BROWSER_USER_AGENT = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:129.0) Gecko/20100101 Firefox/129.0";
28+
private static final String PRELOAD_START = "window.__PRELOADED_STATE__ = JSON.parse('";
29+
private static final String PRELOAD_END = "');";
2730

2831
public GeniusClient(String apiKey, HttpClientProvider httpProvider) {
2932
this.apiKey = apiKey;
@@ -112,17 +115,41 @@ private GeniusData findGeniusData(String query) throws IOException {
112115
private String loadLyrics(String geniusUrl) throws IOException {
113116
final var request = new HttpGet(geniusUrl);
114117

118+
request.setHeader("user-agent", BROWSER_USER_AGENT);
119+
115120
try (final var response = getHttpInterface().execute(request)) {
116121
final String html = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8);
117-
final var doc = Jsoup.parse(html);
118122

119-
final var lyricsContainer = doc.select("div[data-lyrics-container]").first();
123+
// fucking kill me
124+
final var idx1 = html.indexOf(PRELOAD_START);
125+
final var split1 = html.substring(idx1 + PRELOAD_START.length());
126+
final var idx2 = split1.indexOf(PRELOAD_END);
127+
final var json = split1.substring(0, idx2)
128+
.replace("\\\"", "\"")
129+
.replace("\\'", "'")
130+
.replace("\\\\", "\\");
131+
132+
System.out.println(json);
133+
134+
final var lyrics = JsonBrowser.parse(json).get("songPage").get("lyricsData").get("body").text();
135+
136+
if (lyrics == null || lyrics.isEmpty()) {
137+
final var doc = Jsoup.parse(html);
138+
139+
final var lyricsContainer = doc.select("[data-lyrics-container]").first();
140+
141+
if (lyricsContainer == null) {
142+
throw new RuntimeException("Could not find lyrics container, please report this to the developer");
143+
}
120144

121-
if (lyricsContainer == null) {
122-
throw new RuntimeException("Could not find lyrics container, please report this to the developer");
145+
return lyricsContainer.wholeText()
146+
.replace("<br><br>", "\n")
147+
.replace("<br>", "\n")
148+
.replace("\n\n\n", "\n")
149+
.trim();
123150
}
124151

125-
return lyricsContainer.wholeText()
152+
return lyrics
126153
.replace("<br><br>", "\n")
127154
.replace("<br>", "\n")
128155
.replace("\n\n\n", "\n")

0 commit comments

Comments
 (0)