forked from freyacodes/Lavalink-Client
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
src/main/java/lavalink/client/source/vk/VkSourceManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |