-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGeniusClient.java
More file actions
171 lines (140 loc) · 6.13 KB
/
GeniusClient.java
File metadata and controls
171 lines (140 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package me.duncte123.lyrics;
import com.sedmelluq.discord.lavaplayer.tools.JsonBrowser;
import com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import me.duncte123.lyrics.exception.LyricsNotFoundException;
import me.duncte123.lyrics.model.AlbumArt;
import me.duncte123.lyrics.model.Lyrics;
import me.duncte123.lyrics.model.TextLyrics;
import me.duncte123.lyrics.model.Track;
import org.apache.commons.io.IOUtils;
import org.apache.http.client.methods.HttpGet;
import org.jsoup.Jsoup;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
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; Linux x86_64; rv:139.0) Gecko/20100101 Firefox/139.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;
this.httpInterfaceManager = httpProvider;
}
public Future<Lyrics> findLyrics(AudioTrack track) {
return search(
"%s - %s".formatted(track.getInfo().title, track.getInfo().author)
);
}
public Future<Lyrics> search(String query) {
return executor.submit(() -> {
try {
final var geniusData = findGeniusData(query);
final var lyricsText = loadLyrics(geniusData.url());
return new TextLyrics(
new Track(
geniusData.title(),
geniusData.author(),
null,
List.of(
new AlbumArt(
geniusData.artwork(),
-1, -1
)
)
),
"genius.com",
lyricsText
);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
public HttpInterface getHttpInterface() {
return this.httpInterfaceManager.getHttpInterface();
}
private GeniusData findGeniusData(String query) throws IOException {
final var request = new HttpGet("https://api.genius.com/search?q=" + URLEncoder.encode(query, StandardCharsets.UTF_8));
request.setHeader("Authorization", "Bearer " + this.apiKey);
try (final var response = getHttpInterface().execute(request)) {
final var browser = JsonBrowser.parse(response.getEntity().getContent());
final long httpStatus = browser.get("meta").get("status").asLong(-1L);
if (httpStatus != 200L) {
throw new LyricsNotFoundException();
}
final var hits = browser.get("response").get("hits");
// wat??
if (!hits.isList()) {
throw new LyricsNotFoundException();
}
final var hitValues = hits.values();
if (hitValues.isEmpty()) {
throw new LyricsNotFoundException();
}
final var firstHit = hitValues.stream()
.filter((it) -> "song".equals(it.get("type").text()))
.findFirst()
.orElseThrow(LyricsNotFoundException::new);
final var result = firstHit.get("result");
return new GeniusData(
result.get("url").text(),
result.get("title").text(),
result.get("artist_names").text(),
result.get("song_art_image_url").text()
);
}
}
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);
// 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("\\\\", "\\");
final var lyrics = JsonBrowser.parse(json).get("songPage").get("lyricsData").get("body").get("html").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");
}
return lyricsContainer.wholeText()
.replace("<br><br>", "\n")
.replace("<br>", "\n")
.replace("\n\n\n", "\n")
.trim();
}
return Jsoup.parse(lyrics)
.wholeText()
.replace("<br><br>", "\n")
.replace("<br>", "\n")
.replace("\n\n\n", "\n")
.trim();
}
}
@Override
public void close() {
executor.shutdown();
}
private record GeniusData(
String url,
String title,
String author,
String artwork
) {
}
}