From 9b47514b6021eba63af1330e55c6ce59ef5f5fd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?To=CF=80?= Date: Tue, 12 Sep 2023 18:21:34 +0200 Subject: [PATCH] migrate to LavaSearch 1.0.0-beta.2 & fixed issue with YT Tracks in search results --- main/build.gradle | 2 +- .../applemusic/AppleMusicSourceManager.java | 4 +- .../deezer/DeezerAudioSourceManager.java | 4 +- .../lavasrc/spotify/SpotifySourceManager.java | 4 +- .../youtube/YouTubeAudioSearchTrack.kt | 22 ---------- .../lavasrc/youtube/YoutubeSearchManager.kt | 10 ++--- .../lavasrc/youtube/innertube/client.kt | 2 +- plugin/build.gradle | 4 +- .../topi314/lavasrc/plugin/LavaSrcPlugin.java | 41 ++++++++++--------- settings.gradle | 1 + 10 files changed, 37 insertions(+), 57 deletions(-) delete mode 100644 main/src/main/java/com/github/topi314/lavasrc/youtube/YouTubeAudioSearchTrack.kt diff --git a/main/build.gradle b/main/build.gradle index a8bcf712..85d003a1 100644 --- a/main/build.gradle +++ b/main/build.gradle @@ -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" diff --git a/main/src/main/java/com/github/topi314/lavasrc/applemusic/AppleMusicSourceManager.java b/main/src/main/java/com/github/topi314/lavasrc/applemusic/AppleMusicSourceManager.java index 8032bd24..b1dcdec6 100644 --- a/main/src/main/java/com/github/topi314/lavasrc/applemusic/AppleMusicSourceManager.java +++ b/main/src/main/java/com/github/topi314/lavasrc/applemusic/AppleMusicSourceManager.java @@ -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; @@ -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/((?[a-zA-Z]{2})/)?(?album|playlist|artist|song)(/[a-zA-Z\\d\\-]+)?/(?[a-zA-Z\\d\\-.]+)(\\?i=(?\\d+))?"); public static final String SEARCH_PREFIX = "amsearch:"; diff --git a/main/src/main/java/com/github/topi314/lavasrc/deezer/DeezerAudioSourceManager.java b/main/src/main/java/com/github/topi314/lavasrc/deezer/DeezerAudioSourceManager.java index cd3398df..48ab1d82 100644 --- a/main/src/main/java/com/github/topi314/lavasrc/deezer/DeezerAudioSourceManager.java +++ b/main/src/main/java/com/github/topi314/lavasrc/deezer/DeezerAudioSourceManager.java @@ -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; @@ -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/(?[a-zA-Z]{2}/)?(?track|album|playlist|artist)/(?[0-9]+)"); public static final String SEARCH_PREFIX = "dzsearch:"; diff --git a/main/src/main/java/com/github/topi314/lavasrc/spotify/SpotifySourceManager.java b/main/src/main/java/com/github/topi314/lavasrc/spotify/SpotifySourceManager.java index e11940d7..c2253118 100644 --- a/main/src/main/java/com/github/topi314/lavasrc/spotify/SpotifySourceManager.java +++ b/main/src/main/java/com/github/topi314/lavasrc/spotify/SpotifySourceManager.java @@ -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; @@ -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/((?[a-zA-Z-]+)/)?(user/(?[a-zA-Z0-9-_]+)/)?(?track|album|playlist|artist)/(?[a-zA-Z0-9-_]+)"); public static final String SEARCH_PREFIX = "spsearch:"; diff --git a/main/src/main/java/com/github/topi314/lavasrc/youtube/YouTubeAudioSearchTrack.kt b/main/src/main/java/com/github/topi314/lavasrc/youtube/YouTubeAudioSearchTrack.kt deleted file mode 100644 index bdf81d1f..00000000 --- a/main/src/main/java/com/github/topi314/lavasrc/youtube/YouTubeAudioSearchTrack.kt +++ /dev/null @@ -1,22 +0,0 @@ -package com.github.topi314.lavasrc.youtube - -import com.github.topi314.lavasrc.ExtendedAudioTrack -import com.sedmelluq.discord.lavaplayer.source.AudioSourceManager -import com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeAudioSourceManager -import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo -import com.sedmelluq.discord.lavaplayer.track.playback.LocalAudioTrackExecutor - -class YouTubeAudioSearchTrack( - private val sourceManager: YoutubeAudioSourceManager, - trackInfo: AudioTrackInfo, - albumName: String? -) : - ExtendedAudioTrack(trackInfo, albumName, null, null, null, null, false) { - override fun process(executor: LocalAudioTrackExecutor?) = Unit - - override fun makeClone() = YouTubeAudioSearchTrack(sourceManager, trackInfo, albumName) - - override fun getSourceManager(): AudioSourceManager { - return sourceManager - } -} diff --git a/main/src/main/java/com/github/topi314/lavasrc/youtube/YoutubeSearchManager.kt b/main/src/main/java/com/github/topi314/lavasrc/youtube/YoutubeSearchManager.kt index 8eb14e07..dadc2fd2 100644 --- a/main/src/main/java/com/github/topi314/lavasrc/youtube/YoutubeSearchManager.kt +++ b/main/src/main/java/com/github/topi314/lavasrc/youtube/YoutubeSearchManager.kt @@ -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 @@ -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 @@ -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:" @@ -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(), @@ -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 diff --git a/main/src/main/java/com/github/topi314/lavasrc/youtube/innertube/client.kt b/main/src/main/java/com/github/topi314/lavasrc/youtube/innertube/client.kt index 0cbaadbd..077c5781 100644 --- a/main/src/main/java/com/github/topi314/lavasrc/youtube/innertube/client.kt +++ b/main/src/main/java/com/github/topi314/lavasrc/youtube/innertube/client.kt @@ -49,7 +49,7 @@ private inline fun 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() } diff --git a/plugin/build.gradle b/plugin/build.gradle index 9eee0b77..b8c27f22 100644 --- a/plugin/build.gradle +++ b/plugin/build.gradle @@ -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 { diff --git a/plugin/src/main/java/com/github/topi314/lavasrc/plugin/LavaSrcPlugin.java b/plugin/src/main/java/com/github/topi314/lavasrc/plugin/LavaSrcPlugin.java index 526fa1f6..698769ae 100644 --- a/plugin/src/main/java/com/github/topi314/lavasrc/plugin/LavaSrcPlugin.java +++ b/plugin/src/main/java/com/github/topi314/lavasrc/plugin/LavaSrcPlugin.java @@ -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; @@ -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()); @@ -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()); @@ -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); } } @@ -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; } diff --git a/settings.gradle b/settings.gradle index 9f52c112..c6006e4e 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,3 +1,4 @@ +rootProject.name = "LavaSrc" enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS") include(":main")