Skip to content

Commit

Permalink
allow updating source settings via rest
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Sep 15, 2024
1 parent b2f23ce commit 0bb987f
Show file tree
Hide file tree
Showing 19 changed files with 550 additions and 230 deletions.
474 changes: 328 additions & 146 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class AppleMusicSourceManager extends MirroringAudioSourceManager impleme
private final String countryCode;
private int playlistPageLimit;
private int albumPageLimit;
private final String token;
private String token;
private String origin;
private Instant tokenExpire;

Expand Down Expand Up @@ -98,6 +98,15 @@ public void setAlbumPageLimit(int albumPageLimit) {
this.albumPageLimit = albumPageLimit;
}

public void setMediaAPIToken(String mediaAPIToken) {
this.token = mediaAPIToken;
try {
this.parseTokenData();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@NotNull
@Override
public String getSourceName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public class DeezerAudioSourceManager extends ExtendedAudioSourceManager impleme
private static final Logger log = LoggerFactory.getLogger(DeezerAudioSourceManager.class);

private final String masterDecryptionKey;
private final String arl;
private final DeezerAudioTrack.TrackFormat[] formats;
private String arl;
private DeezerAudioTrack.TrackFormat[] formats;
private final HttpInterfaceManager httpInterfaceManager;
private Tokens tokens;

Expand All @@ -80,6 +80,17 @@ public DeezerAudioSourceManager(String masterDecryptionKey, @Nullable String arl
this.httpInterfaceManager = HttpClientTools.createCookielessThreadLocalManager();
}

public void setFormats(DeezerAudioTrack.TrackFormat[] formats) {
if (formats.length == 0) {
throw new IllegalArgumentException("Deezer track formats must not be empty");
}
this.formats = formats;
}

public void setArl(String arl) {
this.arl = arl;
}

static void checkResponse(JsonBrowser json, String message) throws IllegalStateException {
if (json == null) {
throw new IllegalStateException(message + "No response");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public class SpotifySourceManager extends MirroringAudioSourceManager implements
private static final Logger log = LoggerFactory.getLogger(SpotifySourceManager.class);

private final HttpInterfaceManager httpInterfaceManager = HttpClientTools.createDefaultThreadLocalManager();
private final SpotifyTokenTracker tokenTracker;
private final String spDc;
private SpotifyTokenTracker tokenTracker;
private String spDc;
private final String countryCode;
private int playlistPageLimit = 6;
private int albumPageLimit = 6;
Expand Down Expand Up @@ -112,6 +112,16 @@ public void setResolveArtistsInSearch(boolean resolveArtistsInSearch) {
this.resolveArtistsInSearch = resolveArtistsInSearch;
}

public void setClientIDSecret(String clientId, String clientSecret) {
this.tokenTracker = new SpotifyTokenTracker(this, clientId, clientSecret);
}

public void setSpDc(String spDc) {
this.spDc = spDc;
this.spToken = null;
this.spTokenExpire = null;
}

@NotNull
@Override
public String getSourceName() {
Expand Down Expand Up @@ -365,7 +375,7 @@ public AudioItem getSearch(String query, boolean preview) throws IOException {
}
}

return new BasicAudioPlaylist("Search results for: " + query, this.parseTrackItems(json.get("tracks"), preview), null, true);
return new BasicAudioPlaylist("Spotify Search: " + query, this.parseTrackItems(json.get("tracks"), preview), null, true);
}

public AudioItem getRecommendations(String query, boolean preview) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class VkMusicSourceManager extends ExtendedAudioSourceManager implements

private final HttpInterfaceManager httpInterfaceManager;

private final String userToken;
private String userToken;
private int artistLoadLimit;
private int playlistLoadLimit;
private int recommendationsLoadLimit;
Expand All @@ -64,6 +64,14 @@ public VkMusicSourceManager(String userToken) {
this.httpInterfaceManager = HttpClientTools.createDefaultThreadLocalManager();
}

public void setUserToken(String userToken) {
if (userToken == null || userToken.isEmpty()) {
throw new IllegalArgumentException("Vk Music user token must be set");
}

this.userToken = userToken;
}

public void setArtistLoadLimit(int artistLimit) {
this.artistLoadLimit = artistLimit;
}
Expand Down Expand Up @@ -426,11 +434,7 @@ private AudioItem getArtist(String id) throws IOException {
}

public JsonBrowser getJson(String method, String headers) throws IOException {
var uri = PUBLIC_API_BASE + method + "?v=" + API_VERSION + headers;
if (!this.userToken.isEmpty()) {
uri += "&access_token=" + this.userToken;
}

var uri = PUBLIC_API_BASE + method + "?v=" + API_VERSION + headers + "&access_token=" + this.userToken;
var request = new HttpGet(uri);
request.setHeader("Content-Type", "application/json");
return LavaSrcTools.fetchResponseAsJson(this.httpInterfaceManager.getInterface(), request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class YandexMusicSourceManager extends ExtendedAudioSourceManager impleme

private final HttpInterfaceManager httpInterfaceManager;

private final String accessToken;
private String accessToken;
private int artistLoadLimit;
private int albumLoadLimit;
private int playlistLoadLimit;
Expand All @@ -67,6 +67,13 @@ public YandexMusicSourceManager(String accessToken) {
this.httpInterfaceManager = HttpClientTools.createDefaultThreadLocalManager();
}

public void setAccessToken(String accessToken) {
if (accessToken == null || accessToken.isEmpty()) {
throw new IllegalArgumentException("Yandex Music accessToken must be set");
}
this.accessToken = accessToken;
}

public void setArtistLoadLimit(int artistLimit) {
this.artistLoadLimit = artistLimit;
}
Expand Down Expand Up @@ -179,10 +186,6 @@ private AudioSearchResult getSearchResult(String query, Set<AudioSearchResult.Ty

@Override
public @Nullable AudioSearchResult loadSearch(@NotNull String query, @NotNull Set<AudioSearchResult.Type> setOfTypes) {
if (accessToken == null || accessToken.isEmpty()) {
throw new IllegalArgumentException("Yandex Music accessToken must be set");
}

try {
if (query.startsWith(SEARCH_PREFIX)) {
return this.getSearchResult(query.substring(SEARCH_PREFIX.length()), setOfTypes);
Expand All @@ -205,10 +208,6 @@ private JsonBrowser findLyrics(String identifier) throws IOException {

@Override
public @Nullable AudioLyrics loadLyrics(@NotNull AudioTrack track) throws IllegalStateException {
if (accessToken == null || accessToken.isEmpty()) {
throw new IllegalArgumentException("Yandex Music accessToken must be set");
}

if (track.getSourceManager() instanceof YandexMusicSourceManager) {
try {
var lyricsJson = findLyrics(track.getIdentifier());
Expand Down
1 change: 1 addition & 0 deletions plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ lavalinkPlugin {

dependencies {
implementation project(":main")
implementation project(":protocol")
compileOnly "dev.lavalink.youtube:common:1.1.0"
compileOnly "com.github.topi314.lavasearch:lavasearch:1.0.0"
implementation "com.github.topi314.lavasearch:lavasearch-plugin-api:1.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import com.github.topi314.lavasearch.api.SearchManagerConfiguration;
import com.github.topi314.lavasrc.applemusic.AppleMusicSourceManager;
import com.github.topi314.lavasrc.deezer.DeezerAudioSourceManager;
import com.github.topi314.lavasrc.deezer.DeezerAudioTrack;
import com.github.topi314.lavasrc.flowerytts.FloweryTTSSourceManager;
import com.github.topi314.lavasrc.mirror.DefaultMirroringAudioTrackResolver;
import com.github.topi314.lavasrc.plugin.config.*;
import com.github.topi314.lavasrc.protocol.Config;
import com.github.topi314.lavasrc.spotify.SpotifySourceManager;
import com.github.topi314.lavasrc.vkmusic.VkMusicSourceManager;
import com.github.topi314.lavasrc.yandexmusic.YandexMusicSourceManager;
Expand All @@ -18,8 +21,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.RestController;

@Service
@RestController
public class LavaSrcPlugin implements AudioPlayerManagerConfiguration, SearchManagerConfiguration, LyricsManagerConfiguration {

private static final Logger log = LoggerFactory.getLogger(LavaSrcPlugin.class);
Expand Down Expand Up @@ -211,4 +217,51 @@ public LyricsManager configure(@NotNull LyricsManager manager) {
}
return manager;
}

@PatchMapping("/v4/lavasrc/config")
public void updateConfig(Config config) {
var spotifyConfig = config.getSpotify();
if (spotifyConfig != null && this.spotify != null) {
if (spotifyConfig.getSpDc() != null) {
this.spotify.setSpDc(spotifyConfig.getSpDc());
}
if (spotifyConfig.getClientId() != null && spotifyConfig.getClientSecret() != null) {
this.spotify.setClientIDSecret(spotifyConfig.getClientId(), spotifyConfig.getClientSecret());
}
}

var appleMusicConfig = config.getAppleMusic();
if (appleMusicConfig != null && this.appleMusic != null && appleMusicConfig.getMediaAPIToken() != null) {
this.appleMusic.setMediaAPIToken(appleMusicConfig.getMediaAPIToken());
}

var deezerConfig = config.getDeezer();
if (deezerConfig != null && this.deezer != null) {
if (deezerConfig.getArl() != null) {
this.deezer.setArl(deezerConfig.getArl());
}
if (deezerConfig.getFormats() != null) {
this.deezer.setFormats(deezerConfig.getFormats()
.stream()
.map(deezerTrackFormat -> DeezerAudioTrack.TrackFormat.from(deezerTrackFormat.name()))
.toList()
.toArray(new DeezerAudioTrack.TrackFormat[0])
);
}
}

var yandexMusicConfig = config.getYandexMusic();
if (yandexMusicConfig != null && this.yandexMusic != null) {
if (yandexMusicConfig.getAccessToken() != null) {
this.yandexMusic.setAccessToken(yandexMusicConfig.getAccessToken());
}
}

var vkMusicConfig = config.getVkMusic();
if (vkMusicConfig != null && this.vkMusic != null) {
if (vkMusicConfig.getUserToken() != null) {
this.vkMusic.setUserToken(vkMusicConfig.getUserToken());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.topi314.lavasrc.plugin;
package com.github.topi314.lavasrc.plugin.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.topi314.lavasrc.plugin;
package com.github.topi314.lavasrc.plugin.config;

import com.github.topi314.lavasrc.deezer.DeezerAudioTrack;
import org.springframework.boot.context.properties.ConfigurationProperties;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
package com.github.topi314.lavasrc.plugin;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "plugins.lavasrc.flowerytts")
@Component
public class FloweryTTSConfig {

private String voice = null;
private boolean translate;
private int silence;
private float speed = 1.0F;
private String audioFormat = "mp3";

public String getVoice() {
return this.voice;
}

public void setVoice(String voice) {
this.voice = voice;
}

public boolean getTranslate() {
return this.translate;
}

public void setTranslate(boolean translate) {
this.translate = translate;
}

public int getSilence() {
return this.silence;
}

public void setSilence(int silence) {
this.silence = silence;
}

public float getSpeed() {
return this.speed;
}

public void setSpeed(float speed) {
this.speed = speed;
}

public String getAudioFormat() {
return this.audioFormat;
}

public void setAudioFormat(String audioFormat) {
this.audioFormat = audioFormat;
}
package com.github.topi314.lavasrc.plugin.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "plugins.lavasrc.flowerytts")
@Component
public class FloweryTTSConfig {

private String voice = null;
private boolean translate;
private int silence;
private float speed = 1.0F;
private String audioFormat = "mp3";

public String getVoice() {
return this.voice;
}

public void setVoice(String voice) {
this.voice = voice;
}

public boolean getTranslate() {
return this.translate;
}

public void setTranslate(boolean translate) {
this.translate = translate;
}

public int getSilence() {
return this.silence;
}

public void setSilence(int silence) {
this.silence = silence;
}

public float getSpeed() {
return this.speed;
}

public void setSpeed(float speed) {
this.speed = speed;
}

public String getAudioFormat() {
return this.audioFormat;
}

public void setAudioFormat(String audioFormat) {
this.audioFormat = audioFormat;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.topi314.lavasrc.plugin;
package com.github.topi314.lavasrc.plugin.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.topi314.lavasrc.plugin;
package com.github.topi314.lavasrc.plugin.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.topi314.lavasrc.plugin;
package com.github.topi314.lavasrc.plugin.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
Expand Down
Loading

0 comments on commit 0bb987f

Please sign in to comment.