Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix genius not loading all lyrics #9

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions application/src/main/java/me/duncte123/lyrics/GeniusClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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("<br><br>", "\n")
.replace("<br>", "\n")
.replace("\n\n\n", "\n")
.trim();
}

return lyricsContainer.wholeText()
return lyrics
.replace("<br><br>", "\n")
.replace("<br>", "\n")
.replace("\n\n\n", "\n")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Loading