Skip to content

Commit

Permalink
Merge branch 'master' into deezer/cache-tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Mar 10, 2024
2 parents 5a24554 + 32b7986 commit 116bd1a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ dependencies {
</dependency>
<dependency>
<groupId>com.github.topi314.lavasrc</groupId>
<artifactId>lavasrc-protocol</artifactId>
<artifactId>lavasrc-protocol-jvm</artifactId>
<version>x.y.z</version>
</dependency>
</dependencies>
Expand Down Expand Up @@ -318,6 +318,7 @@ LavaSrc adds the following fields to tracks & playlists in Lavalink
* `ymsearch:animals architects`
* https://music.yandex.ru/album/13886032/track/71663565
* https://music.yandex.ru/album/13886032
* https://music.yandex.ru/track/71663565
* https://music.yandex.ru/users/yamusic-bestsongs/playlists/701626
* https://music.yandex.ru/artist/701626

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public AudioSearchResult getSearchSuggestions(String query, Set<AudioSearchResul
}
}

return new BasicAudioSearchResult(tracks, playLists, albums, artists, terms);
return new BasicAudioSearchResult(tracks, albums, artists, playLists, terms);
}

public JsonBrowser getJson(String uri) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class YandexMusicSourceManager implements AudioSourceManager, HttpConfigurable {
public static final Pattern URL_PATTERN = Pattern.compile("(https?://)?music\\.yandex\\.(ru|com)/(?<type1>artist|album)/(?<identifier>[0-9]+)/?((?<type2>track/)(?<identifier2>[0-9]+)/?)?");
public static final Pattern URL_PATTERN = Pattern.compile("(https?://)?music\\.yandex\\.(ru|com)/(?<type1>artist|album|track)/(?<identifier>[0-9]+)(/(?<type2>track)/(?<identifier2>[0-9]+))?/?");
public static final Pattern URL_PLAYLIST_PATTERN = Pattern.compile("(https?://)?music\\.yandex\\.(ru|com)/users/(?<identifier>[0-9A-Za-z@.-]+)/playlists/(?<identifier2>[0-9]+)/?");
public static final String SEARCH_PREFIX = "ymsearch:";
public static final String PUBLIC_API_BASE = "https://api.music.yandex.net";
Expand Down Expand Up @@ -72,6 +73,9 @@ public AudioItem loadItem(AudioPlayerManager manager, AudioReference reference)
case "artist":
var artistId = matcher.group("identifier");
return this.getArtist(artistId);
case "track":
var trackId = matcher.group("identifier");
return this.getTrack(trackId);
}
return null;
}
Expand Down Expand Up @@ -152,8 +156,12 @@ private AudioItem getPlaylist(String userString, String id) throws IOException {
return AudioReference.NO_TRACK;
}
var tracks = new ArrayList<AudioTrack>();
for (var track : json.get("result").get("tracks").values()) {
var parsedTrack = this.parseTrack(track.get("track"));
var tracksToParse = json.get("result").get("tracks").values();
if (tracksToParse.get(0).get("track").isNull()) {
tracksToParse = getTracks(getTrackIds(tracksToParse));
}
for (var track : tracksToParse) {
var parsedTrack = track.get("track").isNull() ? this.parseTrack(track) : this.parseTrack(track.get("track"));
if (parsedTrack != null) {
tracks.add(parsedTrack);
}
Expand All @@ -167,6 +175,16 @@ private AudioItem getPlaylist(String userString, String id) throws IOException {
return new YandexMusicAudioPlaylist(playlistTitle, tracks, ExtendedAudioPlaylist.Type.PLAYLIST, json.get("result").get("url").text(), this.formatCoverUri(coverUri), author);
}

private List<JsonBrowser> getTracks(String trackIds) throws IOException {
return getJson(PUBLIC_API_BASE + "/tracks?track-ids=" + URLEncoder.encode(trackIds, StandardCharsets.UTF_8)).get("result").values();
}

private String getTrackIds(List<JsonBrowser> tracksToParse) {
return tracksToParse.stream()
.map(node -> node.get("id").text())
.collect(Collectors.joining(","));
}

public JsonBrowser getJson(String uri) throws IOException {
var request = new HttpGet(uri);
request.setHeader("Accept", "application/json");
Expand All @@ -193,27 +211,49 @@ private List<AudioTrack> parseTracks(JsonBrowser json) {
}

private AudioTrack parseTrack(JsonBrowser json) {
if (!json.get("available").asBoolean(false) || json.get("albums").values().isEmpty()) {
if (!json.get("available").asBoolean(false)) {
return null;
}
var id = json.get("id").text();
var artist = json.get("major").get("name").text().equals("PODCASTS") ? json.get("albums").values().get(0).get("title").text() : json.get("artists").values().get(0).get("name").text();
var coverUri = json.get("albums").values().get(0).get("coverUri").text();
var artist = parseArtist(json);
var coverUri = json.get("coverUri").text();
return new YandexMusicAudioTrack(
new AudioTrackInfo(
json.get("title").text(),
artist,
json.get("durationMs").as(Long.class),
id,
false,
"https://music.yandex.ru/album/" + json.get("albums").values().get(0).get("id").text() + "/track/" + id,
"https://music.yandex.ru/track/" + id,
this.formatCoverUri(coverUri),
null
),
this
);
}

private String parseArtist(JsonBrowser json) {
if (!json.get("major").isNull() && json.get("major").get("name").text().equals("PODCASTS")) {
return json.get("albums").values().get(0).get("title").text();
}

if (!json.get("artists").values().isEmpty()) {
return extractArtists(json.get("artists"));
}

if (!json.get("matchedTrack").isNull()) {
return extractArtists(json.get("matchedTrack").get("artists"));
}

return "Unknown";
}

private String extractArtists(JsonBrowser artistNode) {
return artistNode.values().stream()
.map(node -> node.get("name").text())
.collect(Collectors.joining(", "));
}

private String formatCoverUri(String coverUri) {
return coverUri != null ? "https://" + coverUri.replace("%%", "400x400") : null;
}
Expand Down Expand Up @@ -254,4 +294,4 @@ public void shutdown() {
public HttpInterface getHttpInterface() {
return this.httpInterfaceManager.getInterface();
}
}
}

0 comments on commit 116bd1a

Please sign in to comment.