Skip to content

Commit

Permalink
migrate to LavaSearch 1.0.0-beta.2 & fixed issue with YT Tracks in se…
Browse files Browse the repository at this point in the history
…arch results
  • Loading branch information
topi314 committed Sep 12, 2023
1 parent 7a9bddf commit 9b47514
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 57 deletions.
2 changes: 1 addition & 1 deletion main/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ java {
}

dependencies {
implementation "com.github.topi314.lavasearch:lavasearch:1.0.0-beta.1"
implementation "com.github.topi314.lavasearch:lavasearch:1.0.0-beta.2"
compileOnly "dev.arbjerg:lavaplayer:2.0.1"
implementation "org.jsoup:jsoup:1.15.3"
implementation "commons-io:commons-io:2.7"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.github.topi314.lavasearch.AudioSearchSourceManager;
import com.github.topi314.lavasearch.AudioSearchManager;
import com.github.topi314.lavasearch.result.AudioSearchResult;
import com.github.topi314.lavasearch.result.AudioText;
import com.github.topi314.lavasearch.result.BasicAudioSearchResult;
Expand Down Expand Up @@ -39,7 +39,7 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class AppleMusicSourceManager extends MirroringAudioSourceManager implements AudioSearchSourceManager {
public class AppleMusicSourceManager extends MirroringAudioSourceManager implements AudioSearchManager {

public static final Pattern URL_PATTERN = Pattern.compile("(https?://)?(www\\.)?music\\.apple\\.com/((?<countrycode>[a-zA-Z]{2})/)?(?<type>album|playlist|artist|song)(/[a-zA-Z\\d\\-]+)?/(?<identifier>[a-zA-Z\\d\\-.]+)(\\?i=(?<identifier2>\\d+))?");
public static final String SEARCH_PREFIX = "amsearch:";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.topi314.lavasrc.deezer;

import com.github.topi314.lavasearch.AudioSearchSourceManager;
import com.github.topi314.lavasearch.AudioSearchManager;
import com.github.topi314.lavasearch.result.AudioSearchResult;
import com.github.topi314.lavasearch.result.BasicAudioSearchResult;
import com.github.topi314.lavasrc.ExtendedAudioSourceManager;
Expand Down Expand Up @@ -33,7 +33,7 @@
import java.util.function.Function;
import java.util.regex.Pattern;

public class DeezerAudioSourceManager extends ExtendedAudioSourceManager implements HttpConfigurable, AudioSearchSourceManager {
public class DeezerAudioSourceManager extends ExtendedAudioSourceManager implements HttpConfigurable, AudioSearchManager {

public static final Pattern URL_PATTERN = Pattern.compile("(https?://)?(www\\.)?deezer\\.com/(?<countrycode>[a-zA-Z]{2}/)?(?<type>track|album|playlist|artist)/(?<identifier>[0-9]+)");
public static final String SEARCH_PREFIX = "dzsearch:";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.topi314.lavasrc.spotify;

import com.github.topi314.lavasearch.AudioSearchSourceManager;
import com.github.topi314.lavasearch.AudioSearchManager;
import com.github.topi314.lavasearch.result.AudioSearchResult;
import com.github.topi314.lavasearch.result.BasicAudioSearchResult;
import com.github.topi314.lavasrc.ExtendedAudioPlaylist;
Expand Down Expand Up @@ -36,7 +36,7 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class SpotifySourceManager extends MirroringAudioSourceManager implements HttpConfigurable, AudioSearchSourceManager {
public class SpotifySourceManager extends MirroringAudioSourceManager implements HttpConfigurable, AudioSearchManager {

public static final Pattern URL_PATTERN = Pattern.compile("(https?://)(www\\.)?open\\.spotify\\.com/((?<region>[a-zA-Z-]+)/)?(user/(?<user>[a-zA-Z0-9-_]+)/)?(?<type>track|album|playlist|artist)/(?<identifier>[a-zA-Z0-9-_]+)");
public static final String SEARCH_PREFIX = "spsearch:";
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.topi314.lavasrc.youtube

import com.github.topi314.lavasearch.AudioSearchSourceManager
import com.github.topi314.lavasearch.AudioSearchManager
import com.github.topi314.lavasearch.result.AudioSearchResult
import com.github.topi314.lavasearch.result.AudioText
import com.github.topi314.lavasearch.result.BasicAudioSearchResult
Expand All @@ -9,6 +9,7 @@ import com.github.topi314.lavasrc.ExtendedAudioPlaylist
import com.github.topi314.lavasrc.youtube.innertube.MusicResponsiveListItemRenderer
import com.github.topi314.lavasrc.youtube.innertube.requestMusicAutoComplete
import com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeAudioSourceManager
import com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeAudioTrack
import com.sedmelluq.discord.lavaplayer.tools.io.HttpClientTools
import com.sedmelluq.discord.lavaplayer.track.AudioTrack
import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo
Expand All @@ -30,7 +31,7 @@ private fun MusicResponsiveListItemRenderer.NavigationEndpoint.toUrl() = when {

class YoutubeSearchManager(
private val sourceManager: () -> YoutubeAudioSourceManager
) : AudioSearchSourceManager {
) : AudioSearchManager {
companion object {
const val SEARCH_PREFIX = "ytsearch:"
const val MUSIC_SEARCH_PREFIX = "ytmsearch:"
Expand Down Expand Up @@ -72,9 +73,6 @@ class YoutubeSearchManager(
val artist = item.flexColumns.getOrNull(1)
?.musicResponsiveListItemFlexColumnRenderer
?.text?.joinRuns() ?: "Unknown Author"
val album = item.flexColumns.getOrNull(2)
?.musicResponsiveListItemFlexColumnRenderer
?.text?.joinRuns()
if (item.navigationEndpoint.watchEndpoint != null) {
val info = AudioTrackInfo(
item.flexColumns.first().musicResponsiveListItemFlexColumnRenderer.text.joinRuns(),
Expand All @@ -86,7 +84,7 @@ class YoutubeSearchManager(
thumbnail,
null
)
YouTubeAudioSearchTrack(sourceManager.invoke(), info, album)
YoutubeAudioTrack(info, sourceManager())
} else if (item.navigationEndpoint.browseEndpoint != null) {
val type =
item.navigationEndpoint.browseEndpoint.browseEndpointContextSupportedConfigs.browseEndpointContextMusicConfig.pageType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private inline fun <reified B, reified R> HttpInterface.makeRequest(
addHeader(HttpHeaders.REFERER, domain.toString())
if (body != null) {
addHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString())
entity = StringEntity(json.encodeToString(body).also { println(it) })
entity = StringEntity(json.encodeToString(body))
}
builder()
}
Expand Down
4 changes: 2 additions & 2 deletions plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ targetCompatibility = JavaVersion.VERSION_17

dependencies {
implementation project(":main")
compileOnly "com.github.topi314.lavasearch:lavasearch:1.0.0-beta.1"
implementation "com.github.topi314.lavasearch:lavasearch-plugin-api:1.0.0-beta.1"
compileOnly "com.github.topi314.lavasearch:lavasearch:1.0.0-beta.2"
implementation "com.github.topi314.lavasearch:lavasearch-plugin-api:1.0.0-beta.2"
}

publishing {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.topi314.lavasrc.plugin;

import com.github.topi314.lavasearch.AudioSearchManager;
import com.github.topi314.lavasearch.SearchManager;
import com.github.topi314.lavasearch.api.SearchManagerConfiguration;
import com.github.topi314.lavasrc.applemusic.AppleMusicSourceManager;
import com.github.topi314.lavasrc.deezer.DeezerAudioSourceManager;
Expand Down Expand Up @@ -34,7 +34,6 @@ public LavaSrcPlugin(LavaSrcConfig pluginConfig, SourcesConfig sourcesConfig, Sp
log.info("Loading LavaSrc plugin...");

if (sourcesConfig.isSpotify()) {
log.info("Registering Spotify audio source manager...");
this.spotify = new SpotifySourceManager(pluginConfig.getProviders(), spotifyConfig.getClientId(), spotifyConfig.getClientSecret(), spotifyConfig.getCountryCode(), unused -> manager);
if (spotifyConfig.getPlaylistLoadLimit() > 0) {
this.spotify.setPlaylistPageLimit(spotifyConfig.getPlaylistLoadLimit());
Expand All @@ -44,26 +43,21 @@ public LavaSrcPlugin(LavaSrcConfig pluginConfig, SourcesConfig sourcesConfig, Sp
}
}
if (sourcesConfig.isAppleMusic()) {
log.info("Registering Apple Music audio source manager...");
var appleMusicSourceManager = new AppleMusicSourceManager(pluginConfig.getProviders(), appleMusicConfig.getMediaAPIToken(), appleMusicConfig.getCountryCode(), unused -> manager);
this.appleMusic = new AppleMusicSourceManager(pluginConfig.getProviders(), appleMusicConfig.getMediaAPIToken(), appleMusicConfig.getCountryCode(), unused -> manager);
if (appleMusicConfig.getPlaylistLoadLimit() > 0) {
appleMusicSourceManager.setPlaylistPageLimit(appleMusicConfig.getPlaylistLoadLimit());
appleMusic.setPlaylistPageLimit(appleMusicConfig.getPlaylistLoadLimit());
}
if (appleMusicConfig.getAlbumLoadLimit() > 0) {
appleMusicSourceManager.setAlbumPageLimit(appleMusicConfig.getAlbumLoadLimit());
appleMusic.setAlbumPageLimit(appleMusicConfig.getAlbumLoadLimit());
}
this.appleMusic = appleMusicSourceManager;
}
if (sourcesConfig.isDeezer()) {
log.info("Registering Deezer audio source manager...");
this.deezer = new DeezerAudioSourceManager(deezerConfig.getMasterDecryptionKey());
}
if (sourcesConfig.isYandexMusic()) {
log.info("Registering Yandex Music audio source manager...");
this.yandexMusic = new YandexMusicSourceManager(yandexMusicConfig.getAccessToken());
}
if (sourcesConfig.isFloweryTTS()) {
log.info("Registering Flowery TTS audio source manager...");
this.flowerytts = new FloweryTTSSourceManager(floweryTTSConfig.getVoice());
if (floweryTTSConfig.getTranslate()) {
this.flowerytts.setTranslate(floweryTTSConfig.getTranslate());
Expand All @@ -79,7 +73,6 @@ public LavaSrcPlugin(LavaSrcConfig pluginConfig, SourcesConfig sourcesConfig, Sp
}
}
if (sourcesConfig.isYoutube()) {
log.info("Registering Youtube search manager...");
this.youtube = new YoutubeSearchManager(() -> youtubeAudioSourceManager);
}
}
Expand All @@ -89,43 +82,53 @@ public LavaSrcPlugin(LavaSrcConfig pluginConfig, SourcesConfig sourcesConfig, Sp
public AudioPlayerManager configure(@NotNull AudioPlayerManager manager) {
this.manager = manager;
if (this.spotify != null) {
log.info("Registering Spotify audio source manager...");
manager.registerSourceManager(this.spotify);
}
if (this.appleMusic != null) {
log.info("Registering Apple Music audio source manager...");
manager.registerSourceManager(this.appleMusic);
}
if (this.deezer != null) {
log.info("Registering Deezer audio source manager...");
manager.registerSourceManager(this.deezer);
}
if (this.yandexMusic != null) {
log.info("Registering Yandex Music audio source manager...");
manager.registerSourceManager(this.yandexMusic);
}
if (this.flowerytts != null) {
log.info("Registering Flowery TTS audio source manager...");
manager.registerSourceManager(this.flowerytts);
}
if (this.youtube != null) {
this.youtubeAudioSourceManager = manager.source(YoutubeAudioSourceManager.class);
if (this.youtubeAudioSourceManager == null) {
throw new IllegalStateException("Youtube LavaSearch requires YoutubeAudioSourceManager to be enabled.");
}

}
return manager;
}

@Override
@NotNull
public AudioSearchManager configure(@NotNull AudioSearchManager manager) {
public SearchManager configure(@NotNull SearchManager manager) {
if (this.spotify != null) {
manager.registerSourceManager(this.spotify);
log.info("Registering Spotify search manager...");
manager.registerSearchManager(this.spotify);
}
if (this.appleMusic != null) {
manager.registerSourceManager(this.appleMusic);
log.info("Registering Apple Music search manager...");
manager.registerSearchManager(this.appleMusic);
}
if (this.deezer != null) {
manager.registerSourceManager(this.deezer);
log.info("Registering Deezer search manager...");
manager.registerSearchManager(this.deezer);
}
if (this.youtube != null) {
if (this.youtubeAudioSourceManager == null) {
throw new IllegalStateException("Youtube audio source manager is not initialized but required by Youtube search manager.");
}
manager.registerSourceManager(this.youtube);
log.info("Registering Youtube search manager...");
manager.registerSearchManager(this.youtube);
}
return manager;
}
Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
rootProject.name = "LavaSrc"
enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS")

include(":main")
Expand Down

0 comments on commit 9b47514

Please sign in to comment.