Skip to content

Commit

Permalink
fix not npe when searching yt tracks via lavasearch
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Sep 1, 2023
1 parent 8e8839b commit 7a9bddf
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
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(trackInfo: AudioTrackInfo, albumName: String?) :
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ 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.tools.io.HttpClientTools
import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist
import com.sedmelluq.discord.lavaplayer.track.AudioTrack
import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo
import org.apache.http.client.methods.HttpGet
Expand All @@ -28,11 +28,18 @@ private fun MusicResponsiveListItemRenderer.NavigationEndpoint.toUrl() = when {
else -> error("Unknown endpoint: $this")
}

class YoutubeSearchManager : AudioSearchSourceManager {
class YoutubeSearchManager(
private val sourceManager: () -> YoutubeAudioSourceManager
) : AudioSearchSourceManager {
companion object {
const val SEARCH_PREFIX = "ytsearch:"
const val MUSIC_SEARCH_PREFIX = "ytmsearch:"
val SEARCH_TYPES = setOf(AudioSearchResult.Type.ALBUM, AudioSearchResult.Type.ARTIST, AudioSearchResult.Type.TRACK, AudioSearchResult.Type.TEXT)
val SEARCH_TYPES = setOf(
AudioSearchResult.Type.ALBUM,
AudioSearchResult.Type.ARTIST,
AudioSearchResult.Type.TRACK,
AudioSearchResult.Type.TEXT
)
}

private val httpInterfaceManager = HttpClientTools.createDefaultThreadLocalManager()
Expand Down Expand Up @@ -79,8 +86,7 @@ class YoutubeSearchManager : AudioSearchSourceManager {
thumbnail,
null
)

YouTubeAudioSearchTrack(info, album)
YouTubeAudioSearchTrack(sourceManager.invoke(), info, album)
} else if (item.navigationEndpoint.browseEndpoint != null) {
val type =
item.navigationEndpoint.browseEndpoint.browseEndpointContextSupportedConfigs.browseEndpointContextMusicConfig.pageType
Expand Down Expand Up @@ -146,4 +152,5 @@ private inline fun <reified T : Any> List<Any>.filter(enabled: Boolean) =
if (enabled) filterIsInstance<T>() else emptyList()

private fun List<Any>.filter(enabled: Boolean, type: ExtendedAudioPlaylist.Type) =
if (enabled) asSequence().filterIsInstance<ExtendedAudioPlaylist>().filter { it.type == type }.toList() else emptyList()
if (enabled) asSequence().filterIsInstance<ExtendedAudioPlaylist>().filter { it.type == type }
.toList() else emptyList()
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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 @@ -21,6 +22,7 @@ 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 @@ -78,7 +80,7 @@ public LavaSrcPlugin(LavaSrcConfig pluginConfig, SourcesConfig sourcesConfig, Sp
}
if (sourcesConfig.isYoutube()) {
log.info("Registering Youtube search manager...");
this.youtube = new YoutubeSearchManager();
this.youtube = new YoutubeSearchManager(() -> youtubeAudioSourceManager);
}
}

Expand All @@ -101,6 +103,9 @@ public AudioPlayerManager configure(@NotNull AudioPlayerManager manager) {
if (this.flowerytts != null) {
manager.registerSourceManager(this.flowerytts);
}
if (this.youtube != null) {
this.youtubeAudioSourceManager = manager.source(YoutubeAudioSourceManager.class);
}
return manager;
}

Expand All @@ -117,6 +122,9 @@ public AudioSearchManager configure(@NotNull AudioSearchManager manager) {
manager.registerSourceManager(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);
}
return manager;
Expand Down

0 comments on commit 7a9bddf

Please sign in to comment.