Skip to content

Commit 1a10014

Browse files
authored
Fix genius not loading all lyrics (#9)
* Got an idea on how to fix this, it's gonna be shit * Bump ytmusic client version * Fix genius lyrics loading * Bump workflows
1 parent c18085c commit 1a10014

3 files changed

Lines changed: 40 additions & 14 deletions

File tree

.github/workflows/release.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,32 @@ jobs:
1111
LAVALINK_MAVEN_PASSWORD: ${{ secrets.LAVALINK_MAVEN_PASSWORD }}
1212
steps:
1313
- name: Checkout
14-
uses: actions/checkout@v3
14+
uses: actions/checkout@v4
1515
with:
1616
fetch-depth: 0
1717

1818
- name: Setup Java
19-
uses: actions/setup-java@v3
19+
uses: actions/setup-java@v4
2020
with:
2121
distribution: zulu
2222
java-version: 17
2323
cache: gradle
2424

2525
- name: Setup Gradle
26-
uses: gradle/gradle-build-action@v2
26+
uses: gradle/actions/setup-gradle@v4
2727

2828
- name: Build and Publish
2929
run: ./gradlew build publish --no-daemon
3030

3131
- name: Upload main Artifact
32-
uses: actions/upload-artifact@v3
32+
uses: actions/upload-artifact@v4
3333
with:
3434
name: java-lyrics-plugin.zip
3535
path: |
3636
build/libs/lyrics-*.jar
3737
3838
- name: Upload plugin Artifact
39-
uses: actions/upload-artifact@v3
39+
uses: actions/upload-artifact@v4
4040
with:
4141
name: java-lyrics-lavalyrics.zip
4242
path: |
@@ -48,15 +48,15 @@ jobs:
4848
if: github.event_name == 'release'
4949
steps:
5050
- name: Checkout
51-
uses: actions/checkout@v3
51+
uses: actions/checkout@v4
5252

5353
- name: Download main Artifact
54-
uses: actions/download-artifact@v3
54+
uses: actions/download-artifact@v4
5555
with:
5656
name: java-lyrics-plugin.zip
5757

5858
- name: Download plugin Artifact
59-
uses: actions/download-artifact@v3
59+
uses: actions/download-artifact@v4
6060
with:
6161
name: java-lyrics-lavalyrics.zip
6262

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

Lines changed: 31 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; Linux x86_64; rv:139.0) Gecko/20100101 Firefox/139.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,40 @@ 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+
final var lyrics = JsonBrowser.parse(json).get("songPage").get("lyricsData").get("body").get("html").text();
133+
134+
if (lyrics == null || lyrics.isEmpty()) {
135+
final var doc = Jsoup.parse(html);
136+
137+
final var lyricsContainer = doc.select("[data-lyrics-container]").first();
138+
139+
if (lyricsContainer == null) {
140+
throw new RuntimeException("Could not find lyrics container, please report this to the developer");
141+
}
120142

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

125-
return lyricsContainer.wholeText()
150+
return Jsoup.parse(lyrics)
151+
.wholeText()
126152
.replace("<br><br>", "\n")
127153
.replace("<br>", "\n")
128154
.replace("\n\n\n", "\n")

protocol/src/main/java/me/duncte123/lyrics/model/Client.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public Client(String clientName, String clientVersion) {
66
}
77

88
public Client(String hl) {
9-
this("ANDROID_MUSIC", "6.31.55", hl);
9+
this("ANDROID_MUSIC", "7.11.50", hl);
1010
}
1111

1212
public Client() {

0 commit comments

Comments
 (0)