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 Aug 25, 2024
1 parent 791a2db commit c3348ff
Show file tree
Hide file tree
Showing 17 changed files with 237 additions and 69 deletions.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ A collection of additional [Lavaplayer v2](https://github.com/sedmelluq/lavaplay

* [Lavaplayer Usage](#lavaplayer-usage)
* [Lavalink Usage](#lavalink-usage)
* [Configuration](#configuration)
* [Update Settings at Runtime](#update-settings-at-runtime)
* [Supported URLs and Queries](#supported-urls-and-queries)

## Lavaplayer Usage
Expand Down Expand Up @@ -438,6 +440,44 @@ LavaSrc adds the following fields to tracks & playlists in Lavalink
---
### Update Settings at Runtime
Sometimes you may want to update the settings at runtime without restarting Lavalink. This can be done by sending a `PATCH` request to the `/v4/lavasrc/config` endpoint.
Keep in mind this will **NOT** update the settings in the `application.yml` file. If you restart Lavalink the settings will be reset to the ones in the `application.yml` file.

```http
PATCH /v4/lavasrc/config
```

All fields are optional and only the fields you provide will be updated.

Request:
```json
{
"spotify": {
"clientId": "your client id",
"clientSecret": "your client secret",
"spDc": "your sp dc cookie"
},
"applemusic": {
"mediaAPIToken": "your apple music api token"
},
"deezer": {
"arl": "your deezer arl",
"formats": [
"FLAC",
"MP3_320",
"MP3_256",
"MP3_128",
"MP3_64",
"AAC_64"
]
}
}
```

---

## Supported URLs and Queries

### Spotify
Expand Down
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
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.yandexmusic.YandexMusicSourceManager;
import com.github.topi314.lavasrc.youtube.YoutubeSearchManager;
Expand Down Expand Up @@ -49,7 +52,7 @@ public LavaSrcPlugin(LavaSrcConfig pluginConfig, SourcesConfig sourcesConfig, Ly
if (!spotifyConfig.isResolveArtistsInSearch()) {
this.spotify.setResolveArtistsInSearch(spotifyConfig.isResolveArtistsInSearch());
}
if(spotifyConfig.isLocalFiles()) {
if (spotifyConfig.isLocalFiles()) {
this.spotify.setLocalFiles(spotifyConfig.isLocalFiles());
}
}
Expand Down Expand Up @@ -185,4 +188,36 @@ public LyricsManager configure(@NotNull LyricsManager manager) {
}
return manager;
}

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])
);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.topi314.lavasrc.plugin;

import com.github.topi314.lavasrc.protocol.Config;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
public class LavaSrcRest {

private final LavaSrcPlugin plugin;

public LavaSrcRest(LavaSrcPlugin plugin) {
this.plugin = plugin;
}

@PostMapping("/v4/lavasrc/reload")
@PatchMapping("/v4/lavasrc/reload")
@PutMapping("/v4/lavasrc/reload")
public void setConfig(Config config) throws IOException {
plugin.updateConfig(config);
}
}
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
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 c3348ff

Please sign in to comment.