Skip to content

Commit

Permalink
Support for VKMusic
Browse files Browse the repository at this point in the history
  • Loading branch information
Walkyst committed Sep 3, 2022
1 parent 35f9131 commit 83b30ec
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main/java/lavalink/client/LavalinkUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/
package lavalink.client

//import lavalink.client.source.invidious.InvidiousSourceManager
import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager
import com.sedmelluq.discord.lavaplayer.source.bandcamp.BandcampAudioSourceManager
import com.sedmelluq.discord.lavaplayer.source.http.HttpAudioSourceManager
Expand All @@ -33,8 +34,8 @@ import com.sedmelluq.discord.lavaplayer.tools.io.MessageInput
import com.sedmelluq.discord.lavaplayer.tools.io.MessageOutput
import com.sedmelluq.discord.lavaplayer.track.AudioTrack
import lavalink.client.player.LavalinkPlayer
//import lavalink.client.source.invidious.InvidiousSourceManager
import lavalink.client.source.spotify.SpotifySourceManager
import lavalink.client.source.vk.VkSourceManager
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.IOException
Expand All @@ -45,6 +46,7 @@ object LavalinkUtil {

init {
/* These are only to encode/decode messages */
playerManager.registerSourceManager(VkSourceManager(""))
playerManager.registerSourceManager(SpotifySourceManager(playerManager))
//playerManager.registerSourceManager(InvidiousSourceManager.createDefault())
playerManager.registerSourceManager(YoutubeAudioSourceManager())
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/lavalink/client/source/vk/VkAudioTrack.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package lavalink.client.source.vk

import com.sedmelluq.discord.lavaplayer.container.mp3.Mp3AudioTrack
import com.sedmelluq.discord.lavaplayer.source.AudioSourceManager
import com.sedmelluq.discord.lavaplayer.tools.JsonBrowser
import com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface
import com.sedmelluq.discord.lavaplayer.tools.io.PersistentHttpStream
import com.sedmelluq.discord.lavaplayer.track.AudioTrack
import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo
import com.sedmelluq.discord.lavaplayer.track.DelegatedAudioTrack
import com.sedmelluq.discord.lavaplayer.track.playback.LocalAudioTrackExecutor
import org.apache.http.client.methods.HttpGet
import org.apache.http.util.EntityUtils
import java.net.URI

class VkAudioTrack(
trackInfo: AudioTrackInfo,
private val sourceManager: VkSourceManager
) : DelegatedAudioTrack(trackInfo) {

override fun process(executor: LocalAudioTrackExecutor) {
sourceManager.httpInterfaceManager.`interface`.use { httpInterface ->
processStatic(executor, httpInterface)
}
}

private fun processStatic(executor: LocalAudioTrackExecutor, httpInterface: HttpInterface) {
httpInterface.execute(HttpGet(VkUtils.getTrackUrl(trackInfo.identifier, sourceManager.accessToken))).use {
processDelegate(Mp3AudioTrack(
trackInfo,
PersistentHttpStream(
httpInterface,
URI(JsonBrowser.parse(EntityUtils.toString(it.entity)).get("response").index(0).get("url").text()),
null
)
), executor)
}
}

override fun makeClone(): AudioTrack {
return VkAudioTrack(trackInfo, sourceManager)
}

override fun getSourceManager(): AudioSourceManager {
return sourceManager
}
}
42 changes: 42 additions & 0 deletions src/main/java/lavalink/client/source/vk/VkSourceManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package lavalink.client.source.vk

import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager
import com.sedmelluq.discord.lavaplayer.source.AudioSourceManager
import com.sedmelluq.discord.lavaplayer.tools.ExceptionTools
import com.sedmelluq.discord.lavaplayer.tools.io.HttpClientTools
import com.sedmelluq.discord.lavaplayer.tools.io.HttpInterfaceManager
import com.sedmelluq.discord.lavaplayer.track.AudioItem
import com.sedmelluq.discord.lavaplayer.track.AudioReference
import com.sedmelluq.discord.lavaplayer.track.AudioTrack
import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo
import java.io.DataInput
import java.io.DataOutput

class VkSourceManager(
val accessToken: String
) : AudioSourceManager {

val httpInterfaceManager: HttpInterfaceManager = HttpClientTools.createDefaultThreadLocalManager()

override fun getSourceName(): String {
return "vk"
}

override fun loadItem(manager: AudioPlayerManager, reference: AudioReference): AudioItem? {
return null
}

override fun isTrackEncodable(track: AudioTrack): Boolean = false

override fun encodeTrack(track: AudioTrack, output: DataOutput) {
// Nothing
}

override fun decodeTrack(trackInfo: AudioTrackInfo, input: DataInput): AudioTrack {
return VkAudioTrack(trackInfo, this)
}

override fun shutdown() {
ExceptionTools.closeWithWarnings(httpInterfaceManager)
}
}
19 changes: 19 additions & 0 deletions src/main/java/lavalink/client/source/vk/VkUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package lavalink.client.source.vk

import org.apache.http.client.utils.URIBuilder
import java.net.URI

object VkUtils {

private const val VK_ORIGIN = "https://api.vk.com/method/"
private const val AUDIO_GET_BY_ID = "audio.getById"
private const val API_VERSION = "5.131"

fun getTrackUrl(trackId: String, accessToken: String): URI {
return URIBuilder("$VK_ORIGIN$AUDIO_GET_BY_ID")
.addParameter("access_token", accessToken)
.addParameter("v", API_VERSION)
.addParameter("audios", trackId)
.build()
}
}

0 comments on commit 83b30ec

Please sign in to comment.