Skip to content

Commit

Permalink
Local File Support (topi314#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
clapann authored Jul 22, 2024
1 parent 4b9b4ef commit ee8fe43
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 19 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ plugins:
countryCode: "US" # the country code you want to use for filtering the artists top tracks. See https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
playlistLoadLimit: 6 # The number of pages at 100 tracks each
albumLoadLimit: 6 # The number of pages at 50 tracks each
localFiles: false # Enable local files support with Spotify playlists. Please note `uri` & `iscr` will be `null` & `identifier` will be `"local"`
applemusic:
countryCode: "US" # the country code you want to use for filtering the artists top tracks and language. See https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
mediaAPIToken: "your apple music api token" # apple music api token
Expand Down Expand Up @@ -316,7 +317,7 @@ LavaSrc adds the following fields to tracks & playlists in Lavalink
(check out [Spotify Recommendations Docs](https://developer.spotify.com/documentation/web-api/reference/get-recommendations) for the full query parameter list)
* https://open.spotify.com/track/0eG08cBeKk0mzykKjw4hcQ
* https://open.spotify.com/album/7qemUq4n71awwVPOaX7jw4
* https://open.spotify.com/playlist/7HAO9R9v203gkaPAgknOMp
* https://open.spotify.com/playlist/7HAO9R9v203gkaPAgknOMp (playlists can include local files if you enabled this via: `plugins.lavasrc.spotify.localFiles: true`. Please note `uri` & `iscr` will be `null` & `identifier` will be `"local"`)
* https://open.spotify.com/artist/3ZztVuWxHzNpl0THurTFCv

(including new regional links like https://open.spotify.com/intl-de/track/0eG08cBeKk0mzykKjw4hcQ)
Expand Down
1 change: 1 addition & 0 deletions application.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ plugins:
countryCode: "US" # the country code you want to use for filtering the artists top tracks. See https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
playlistLoadLimit: 6 # The number of pages at 100 tracks each
albumLoadLimit: 6 # The number of pages at 50 tracks each
localFiles: false # Enable local files support with Spotify playlists. Please note `uri` & `iscr` will be `null` & `identifier` will be `"local"`
applemusic:
countryCode: "US" # the country code you want to use for filtering the artists top tracks and language. See https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
mediaAPIToken: "..." # apple music api token
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ protected AudioTrack makeShallowClone() {
return new SpotifyAudioTrack(this.trackInfo, (SpotifySourceManager) this.sourceManager);
}

public boolean isLocal() {
return this.trackInfo.identifier.equals("local");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class SpotifySourceManager extends MirroringAudioSourceManager implements
private final String countryCode;
private int playlistPageLimit = 6;
private int albumPageLimit = 6;
private boolean localFiles;

private String spToken;
private Instant spTokenExpire;
Expand Down Expand Up @@ -99,6 +100,10 @@ public void setAlbumPageLimit(int albumPageLimit) {
this.albumPageLimit = albumPageLimit;
}

public void setLocalFiles(boolean localFiles) {
this.localFiles = localFiles;
}

@NotNull
@Override
public String getSourceName() {
Expand Down Expand Up @@ -422,9 +427,10 @@ public AudioItem getPlaylist(String id, boolean preview) throws IOException {

for (var value : page.get("items").values()) {
var track = value.get("track");
if (track.isNull() || track.get("is_local").asBoolean(false) || track.get("type").text().equals("episode")) {
if (track.isNull() || track.get("type").text().equals("episode") || (!this.localFiles && track.get("is_local").asBoolean(false))) {
continue;
}

tracks.add(this.parseTrack(track, preview));
}

Expand Down Expand Up @@ -493,9 +499,9 @@ private AudioTrack parseTrack(JsonBrowser json, boolean preview) {
return new SpotifyAudioTrack(
new AudioTrackInfo(
json.get("name").text(),
json.get("artists").index(0).get("name").text(),
json.get("artists").index(0).get("name").text().isEmpty() ? "Unknown" : json.get("artists").index(0).get("name").text(),
preview ? PREVIEW_LENGTH : json.get("duration_ms").asLong(0),
json.get("id").text(),
json.get("id").text() != null ? json.get("id").text() : "local",
false,
json.get("external_urls").get("spotify").text(),
json.get("album").get("images").index(0).get("url").text(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.topi314.lavasrc.ExtendedAudioPlaylist;
import com.github.topi314.lavasrc.ExtendedAudioTrack;
import com.github.topi314.lavasrc.spotify.SpotifyAudioTrack;
import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import dev.arbjerg.lavalink.api.AudioPluginInfoModifier;
Expand All @@ -12,6 +13,7 @@
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.HashMap;

@Component
public class LavaSrcAudioPluginInfoModifier implements AudioPluginInfoModifier {
Expand All @@ -31,19 +33,24 @@ public JsonObject modifyAudioPlaylistPluginInfo(@NotNull AudioPlaylist playlist)
}

@Nullable
@Override
public JsonObject modifyAudioTrackPluginInfo(@NotNull AudioTrack track) {
if (track instanceof ExtendedAudioTrack extendedTrack) {
return new JsonObject(Map.of(
"albumName", JsonElementKt.JsonPrimitive(extendedTrack.getAlbumName()),
"albumUrl", JsonElementKt.JsonPrimitive(extendedTrack.getAlbumUrl()),
"artistUrl", JsonElementKt.JsonPrimitive(extendedTrack.getArtistUrl()),
"artistArtworkUrl", JsonElementKt.JsonPrimitive(extendedTrack.getArtistArtworkUrl()),
"previewUrl", JsonElementKt.JsonPrimitive(extendedTrack.getPreviewUrl()),
"isPreview", JsonElementKt.JsonPrimitive(extendedTrack.isPreview())
@Override
public JsonObject modifyAudioTrackPluginInfo(@NotNull AudioTrack track) {
if (track instanceof ExtendedAudioTrack extendedTrack) {
var json = new HashMap<>(Map.of(
"albumName", JsonElementKt.JsonPrimitive(extendedTrack.getAlbumName()),
"albumUrl", JsonElementKt.JsonPrimitive(extendedTrack.getAlbumUrl()),
"artistUrl", JsonElementKt.JsonPrimitive(extendedTrack.getArtistUrl()),
"artistArtworkUrl", JsonElementKt.JsonPrimitive(extendedTrack.getArtistArtworkUrl()),
"previewUrl", JsonElementKt.JsonPrimitive(extendedTrack.getPreviewUrl()),
"isPreview", JsonElementKt.JsonPrimitive(extendedTrack.isPreview())
));

));
}
return null;
}
if (track instanceof SpotifyAudioTrack spotifyTrack) {
json.put("isLocal", JsonElementKt.JsonPrimitive(spotifyTrack.isLocal()));
}

return new JsonObject(json);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public LavaSrcPlugin(LavaSrcConfig pluginConfig, SourcesConfig sourcesConfig, Ly
if (spotifyConfig.getAlbumLoadLimit() > 0) {
this.spotify.setAlbumPageLimit(spotifyConfig.getAlbumLoadLimit());
}
if(spotifyConfig.isLocalFiles()) {
this.spotify.setLocalFiles(spotifyConfig.isLocalFiles());
}
}
if (sourcesConfig.isAppleMusic()) {
this.appleMusic = new AppleMusicSourceManager(pluginConfig.getProviders(), appleMusicConfig.getMediaAPIToken(), appleMusicConfig.getCountryCode(), unused -> manager);
Expand Down Expand Up @@ -153,7 +156,7 @@ public SearchManager configure(@NotNull SearchManager manager) {
}
if (this.yandexMusic != null && this.sourcesConfig.isYandexMusic()) {
log.info("Registering Yandex Music search manager...");
manager.registerSearchManager(this.yandexMusic);
manager.registerSearchManager(this.yandexMusic);
}
return manager;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class SpotifyConfig {
private String countryCode = "US";
private int playlistLoadLimit = 6;
private int albumLoadLimit = 6;
private boolean localFiles = false;

public String getClientId() {
return this.clientId;
Expand Down Expand Up @@ -62,4 +63,11 @@ public void setAlbumLoadLimit(int albumLoadLimit) {
this.albumLoadLimit = albumLoadLimit;
}

public boolean isLocalFiles() {
return this.localFiles;
}

public void setLocalFiles(boolean localFiles) {
this.localFiles = localFiles;
}
}

0 comments on commit ee8fe43

Please sign in to comment.