Skip to content

Commit b16ecf0

Browse files
authored
Merge pull request #7 from LewdHuTao/fix-6
fix: youtube return wrong lyrics
2 parents 3662a2c + 9de17f8 commit b16ecf0

File tree

4 files changed

+43
-15
lines changed

4 files changed

+43
-15
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111
<groupId>com.example</groupId>
1212
<artifactId>lyrics_api_v2</artifactId>
13-
<version>2.0.0</version>
13+
<version>2.0.1</version>
1414
<name>lyrics_api_v2</name>
1515
<description>lyrics_api_v2</description>
1616

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.example.lyrics_api_v2.model;
2+
3+
public class MusixmatchToken {
4+
private String message;
5+
private String response;
6+
private int code;
7+
8+
public MusixmatchToken(String message, String response, int code) {
9+
this.message = message;
10+
this.response = response;
11+
this.code = code;
12+
}
13+
14+
public String getMessage() {
15+
return message;
16+
}
17+
public String getRespone() {
18+
return response;
19+
}
20+
public int getCode() {
21+
return code;
22+
}
23+
}

src/main/java/com/example/lyrics_api_v2/service/platform/Musixmatch.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.example.lyrics_api_v2.service.platform;
22

33
import com.example.lyrics_api_v2.model.Lyrics;
4+
import com.example.lyrics_api_v2.model.MusixmatchToken;
45
import org.springframework.stereotype.Service;
56

67
import java.io.BufferedReader;
@@ -66,6 +67,7 @@ private synchronized void refreshToken() {
6667
cachedToken = matcher.group(1);
6768
tokenExpiry = System.currentTimeMillis() + (30 * 60 * 1000); // 30 mins expiry
6869
} else {
70+
new MusixmatchToken("Failed to extract user token from Musixmatch", "500 - Internal Server Error", 500);
6971
throw new RuntimeException("Failed to extract user token from Musixmatch.");
7072
}
7173
} catch (Exception e) {
@@ -92,7 +94,7 @@ private String fetchLyricsByTrackId(String trackId, String userToken) throws Exc
9294
lyrics = lyrics.replace("\\n", "\n").replaceAll("\\[\\d+:\\d+\\.\\d+\\]", "").trim();
9395
return lyrics;
9496
} else {
95-
throw new RuntimeException("Lyrics not found for track ID: " + trackId);
97+
return null;
9698
}
9799
}
98100

@@ -116,7 +118,7 @@ private Lyrics searchTrackByTitle(String title) {
116118

117119
return new Lyrics(artistName, trackName, trackId, "Musixmatch", artworkUrl, lyrics);
118120
} else {
119-
throw new RuntimeException("No track found for title: " + title);
121+
return null;
120122
}
121123

122124
} catch (Exception e) {
@@ -148,7 +150,7 @@ private Lyrics searchTrackByTitleAndArtist(String title, String artist) {
148150

149151
return new Lyrics(artistName, trackName, trackId, "Musixmatch", artworkUrl, lyrics);
150152
} else {
151-
throw new RuntimeException("No lyrics found for title: " + title + " and artist: " + artist);
153+
return null;
152154
}
153155

154156
} catch (Exception e) {

src/main/java/com/example/lyrics_api_v2/service/platform/YouTube.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,24 @@ public Lyrics fetchLyrics(String title, String artist) {
6363

6464
if (songVideoIds.isEmpty()) return null;
6565

66-
for (int i = 0; i < songVideoIds.size(); i++) {
67-
String videoId = songVideoIds.get(i);
68-
String browseId = getLyricsBrowseId(videoId);
69-
if (browseId == null) continue;
70-
fetchSongContent(videoId);
71-
72-
String lyricsContent = fetchLyricsContent(browseId);
73-
if (lyricsContent != null && !lyricsContent.isBlank()) {
74-
return new Lyrics(this.artistName, this.trackTitle, videoId, "YouTube", this.artworkUrl, lyricsContent);
75-
}
66+
String videoId = songVideoIds.get(0);
67+
String browseId = getLyricsBrowseId(videoId);
68+
69+
if (browseId == null) {
70+
return null;
7671
}
72+
73+
fetchSongContent(videoId);
74+
String lyricsContent = fetchLyricsContent(browseId);
75+
76+
if (lyricsContent != null && !lyricsContent.isBlank()) {
77+
return new Lyrics(this.artistName, this.trackTitle, videoId, "YouTube", this.artworkUrl, lyricsContent);
78+
}
79+
80+
return null;
7781
} catch (Exception e) {
7882
throw new RuntimeException("Error fetching YouTube lyrics: " + e.getMessage());
7983
}
80-
return null;
8184
}
8285

8386
private List<String> searchSongVideoIds(String query) throws IOException {

0 commit comments

Comments
 (0)