Skip to content

Commit

Permalink
support new youtube-source plugin for lavasearch youtube
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed May 6, 2024
1 parent abbc7ca commit 32ec01a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
1 change: 1 addition & 0 deletions main/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ java {
dependencies {
api "com.github.topi314.lavasearch:lavasearch:1.0.0"
compileOnly "dev.arbjerg:lavaplayer:2.0.4"
compileOnly "dev.lavalink.youtube:common:1.1.0"
implementation "org.jsoup:jsoup:1.15.3"
implementation "commons-io:commons-io:2.7"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import com.github.topi314.lavasearch.result.BasicAudioText
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.player.AudioPlayerManager
import com.sedmelluq.discord.lavaplayer.tools.io.HttpClientTools
import com.sedmelluq.discord.lavaplayer.track.AudioTrack
import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo
import dev.lavalink.youtube.YoutubeAudioSourceManager
import dev.lavalink.youtube.track.YoutubeAudioTrack
import org.apache.http.client.methods.HttpGet
import java.net.URLEncoder
import com.github.topi314.lavasrc.youtube.innertube.MusicResponsiveListItemRenderer.NavigationEndpoint.BrowseEndpoint.Configs.Config.Type as PageType
Expand All @@ -30,7 +31,7 @@ private fun MusicResponsiveListItemRenderer.NavigationEndpoint.toUrl() = when {
}

class YoutubeSearchManager(
private val sourceManager: () -> YoutubeAudioSourceManager
private val playerManager: () -> AudioPlayerManager
) : AudioSearchManager {
companion object {
const val SEARCH_PREFIX = "ytsearch:"
Expand Down Expand Up @@ -84,7 +85,7 @@ class YoutubeSearchManager(
thumbnail,
null
)
YoutubeAudioTrack(info, sourceManager())
YoutubeAudioTrack(info, playerManager().source(YoutubeAudioSourceManager::class.java))
} else if (item.navigationEndpoint.browseEndpoint != null) {
val type =
item.navigationEndpoint.browseEndpoint.browseEndpointContextSupportedConfigs.browseEndpointContextMusicConfig.pageType
Expand Down
3 changes: 2 additions & 1 deletion plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ archivesBaseName = "lavasrc-plugin"
lavalinkPlugin {
name = "lavasrc-plugin"
apiVersion = "4.0.0"
serverVersion = "4.0.0"
serverVersion = "4.0.5"
configurePublishing = false
}

Expand All @@ -15,6 +15,7 @@ targetCompatibility = JavaVersion.VERSION_17

dependencies {
implementation project(":main")
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 @@ -9,7 +9,6 @@
import com.github.topi314.lavasrc.yandexmusic.YandexMusicSourceManager;
import com.github.topi314.lavasrc.youtube.YoutubeSearchManager;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
import com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeAudioSourceManager;
import dev.arbjerg.lavalink.api.AudioPlayerManagerConfiguration;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
Expand All @@ -22,7 +21,6 @@ public class LavaSrcPlugin implements AudioPlayerManagerConfiguration, SearchMan
private static final Logger log = LoggerFactory.getLogger(LavaSrcPlugin.class);

private AudioPlayerManager manager;
private YoutubeAudioSourceManager youtubeAudioSourceManager;
private SpotifySourceManager spotify;
private AppleMusicSourceManager appleMusic;
private DeezerAudioSourceManager deezer;
Expand Down Expand Up @@ -73,7 +71,21 @@ public LavaSrcPlugin(LavaSrcConfig pluginConfig, SourcesConfig sourcesConfig, Sp
}
}
if (sourcesConfig.isYoutube()) {
this.youtube = new YoutubeSearchManager(() -> youtubeAudioSourceManager);
if (hasNewYoutubeSource()) {
log.info("Registering Youtube Source audio source manager...");
this.youtube = new YoutubeSearchManager(() -> manager);
} else {
throw new IllegalStateException("Youtube LavaSearch requires either Lavaplayer Youtube or Youtube Source plugin to be enabled.");
}
}
}

private boolean hasNewYoutubeSource() {
try {
Class.forName("dev.lavalink.youtube.YoutubeAudioSourceManager");
return true;
} catch (ClassNotFoundException ignored) {
return false;
}
}

Expand Down Expand Up @@ -101,13 +113,6 @@ public AudioPlayerManager configure(@NotNull AudioPlayerManager manager) {
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;
}

Expand Down

0 comments on commit 32ec01a

Please sign in to comment.