Skip to content

Commit

Permalink
Fix issues with innertube API
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed Jan 8, 2023
1 parent 5651179 commit 0b7b2f7
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 26 deletions.
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ krontab = { group = "dev.inmo", name = "krontab", version = "0.8.2" }
ksp-api = { group = "com.google.devtools.ksp", name = "symbol-processing-api", version = "1.7.22-1.0.8" }
rhino = { group = "org.mozilla", name = "rhino", version = "1.7.14" }
ktor-client-okhttp = { group = "io.ktor", name = "ktor-client-okhttp", version.ref = "ktor" }
ktor-client-logging = { group = "io.ktor", name = "ktor-client-logging", version.ref = "ktor" }
ktor-serialization-kotlinx-json = { group = "io.ktor", name = "ktor-serialization-kotlinx-json", version.ref = "ktor" }
ktor-client-content-negotiation = { group = "io.ktor", name = "ktor-client-content-negotiation", version.ref = "ktor" }
ktor-server-content-negotiation = { group = "io.ktor", name = "ktor-server-content-negotiation", version.ref = "ktor" }
Expand Down
4 changes: 3 additions & 1 deletion music/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = "dev.schlaubi.mikbot"
version = "2.13.0"
version = "2.14.0"

dependencies {
api(libs.lavakord.kord)
Expand All @@ -32,6 +32,8 @@ dependencies {
// Image Color Client
api(projects.utils.imageColorClient)
api(projects.utils.imageColorClientKord)

implementation(libs.ktor.client.logging)
}

tasks {
Expand Down
2 changes: 1 addition & 1 deletion music/src/main/kotlin/dev/schlaubi/mikmusic/core/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ object Config : EnvironmentConfig("") {
val YOUTUBE_API_KEY by environment
val SPOTIFY_CLIENT_ID by getEnv("")
val SPOTIFY_CLIENT_SECRET by getEnv("")
val IMAGE_COLOR_SERVICE_URL by environment
val IMAGE_COLOR_SERVICE_URL by environment.optional()
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ class MusicPlugin(wrapper: PluginWrapper) : Plugin(wrapper) {
override suspend fun ExtensibleBotBuilder.apply() {
hooks {
beforeKoinSetup {
loadModule {
single { ImageColorClient(Config.IMAGE_COLOR_SERVICE_URL) }
val imageColorServiceUrl = Config.IMAGE_COLOR_SERVICE_URL
if (imageColorServiceUrl != null) {
loadModule {
single { ImageColorClient(imageColorServiceUrl) }
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import dev.schlaubi.mikbot.plugin.api.util.convertToISO
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.plugins.logging.*
import io.ktor.client.request.*
import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
import kotlinx.serialization.json.Json
import mu.KotlinLogging

private val youtubeMusic = Url("https://music.youtube.com")
private val youtube = Url("https://www.youtube.com")
Expand All @@ -22,13 +24,14 @@ private fun musicContext(locale: Locale): InnerTubeContext {
return InnerTubeContext(
InnerTubeContext.Client(
"WEB_REMIX",
"1.20220502.01.00",
"1.20230102.01.00",
isoLocale.language,
isoLocale.country!!
)
)
}

private val LOG = KotlinLogging.logger { }

object InnerTubeClient {
private val client = HttpClient {
Expand All @@ -40,6 +43,14 @@ object InnerTubeClient {

json(json)
}

install(Logging) {
logger = object : Logger {
override fun log(message: String) = LOG.debug(message)
}

level = if (LOG.isDebugEnabled) LogLevel.ALL else LogLevel.NONE
}
}

suspend fun requestNextSongs(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,24 @@ data class MusicQueueRenderer(
}

@Serializable
data class PlaylistPanelRenderer(
val title: String,
val contents: List<PlaylistPanelVideoRendererContent>,
)
data class PlaylistPanelRenderer(val contents: List<PlaylistPanelVideoRendererContent>)

@Serializable
data class PlaylistPanelVideoRendererContent(
val playlistPanelVideoRenderer: PlaylistPanelVideoRenderer,
val playlistPanelVideoWrapperRenderer: PlaylistPanelVideoWrapperRenderer? = null
)

@Serializable
data class PlaylistPanelVideoWrapperRenderer(val primaryRenderer: PlaylistPanelVideoRenderer)

@Serializable
data class PlaylistPanelVideoRenderer(
val title: Runs<Text>,
val selected: Boolean,
val navigationEndpoints: NavigationEndpoints? = null,
val videoId: String,
@SerialName("longBylineText")
val longByLineText: Runs<Text>? = null,
val playlistSetVideoId: String,
val longByLineText: Runs<Text>? = null
) {
@Serializable
data class NavigationEndpoints(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.schlaubi.mikmusic.musicchannel

import com.kotlindiscord.kord.extensions.i18n.TranslationsProvider
import com.kotlindiscord.kord.extensions.koin.KordExContext
import dev.kord.common.entity.ButtonStyle
import dev.kord.common.entity.DiscordPartialEmoji
import dev.kord.common.entity.Snowflake
Expand All @@ -16,7 +15,6 @@ import dev.kord.rest.builder.message.modify.actionRow
import dev.kord.rest.builder.message.modify.embed
import dev.kord.x.emoji.DiscordEmoji
import dev.kord.x.emoji.Emojis
import dev.nycode.imagecolor.ImageColorClient
import dev.schlaubi.mikbot.plugin.api.util.convertToISO
import dev.schlaubi.mikmusic.core.settings.MusicSettingsDatabase
import dev.schlaubi.mikmusic.player.ChapterQueuedTrack
Expand Down Expand Up @@ -123,12 +121,13 @@ suspend fun updateMessage(
additionalCondition = !playingQueueTrack.isOnLast
)
}
musicButton(
musicPlayer,
autoPlay,
Emojis.blueCar,
enabled = musicPlayer.autoPlay != null
)
// Disabled due to YouTube changing the underlying API
// musicButton(
// musicPlayer,
// autoPlay,
// Emojis.blueCar,
// enabled = musicPlayer.autoPlay != null
// )
}
actionRow {
musicButton(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import dev.schlaubi.lavakord.rest.TrackResponse
import dev.schlaubi.lavakord.rest.loadItem
import dev.schlaubi.mikbot.plugin.api.util.Translator
import dev.schlaubi.mikmusic.innerttube.InnerTubeClient
import dev.schlaubi.mikmusic.innerttube.PlaylistPanelVideoRendererContent
import dev.schlaubi.mikmusic.innerttube.Text
import dev.schlaubi.mikmusic.util.LinkedListSerializer
import dev.schlaubi.mikmusic.util.youtubeId
Expand Down Expand Up @@ -54,7 +53,7 @@ private suspend fun MusicPlayer.fetchAutoPlay(songId: String, playlistId: String
.content
.playlistPanelRenderer
.contents
.map(PlaylistPanelVideoRendererContent::playlistPanelVideoRenderer)
.mapNotNull { it.playlistPanelVideoWrapperRenderer?.primaryRenderer }
val newPlayListId = songRenderers.firstOrNull()?.navigationEndpoints?.watchEndpoint?.playlistId
val songs = songRenderers
.drop(1) // First song is requested song
Expand All @@ -68,7 +67,7 @@ private suspend fun MusicPlayer.fetchAutoPlay(songId: String, playlistId: String
}

context(EmbedBuilder)
suspend fun MusicPlayer.addAutoPlaySongs(translate: Translator) {
suspend fun MusicPlayer.addAutoPlaySongs(translate: Translator) {
val songs = autoPlay?.songs?.take(5)
if (!songs.isNullOrEmpty()) {
field {
Expand Down Expand Up @@ -97,7 +96,7 @@ suspend fun MusicPlayer.findNextAutoPlayedSong(lastSong: Track?): Track? {
}

context(MusicPlayer)
private suspend fun AutoPlayContext.Track.fetchTrack(): Track? {
private suspend fun AutoPlayContext.Track.fetchTrack(): Track? {
val response = link.loadItem("https://www.youtube.com/watch?v=$id")
return if (response.loadType == TrackResponse.LoadType.TRACK_LOADED) {
response.track.toTrack()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import dev.kord.rest.builder.message.EmbedBuilder
import dev.nycode.imagecolor.ImageColorClient
import dev.schlaubi.lavakord.audio.player.Track
import dev.schlaubi.mikbot.plugin.api.util.Translator
import dev.schlaubi.mikmusic.core.Config

private val imageColorClient by KordExContext.get().inject<ImageColorClient>()

Expand Down Expand Up @@ -42,8 +43,10 @@ suspend fun EmbedBuilder.addSong(translate: Translator, track: Track) {
url = info.thumbnails.high.url
}

imageColorClient.fetchImageColorOrNull(info.thumbnails.high.url)?.let { imageColor ->
color = Color(imageColor)
if (Config.IMAGE_COLOR_SERVICE_URL != null) {
imageColorClient.fetchImageColorOrNull(info.thumbnails.high.url)?.let { imageColor ->
color = Color(imageColor)
}
}

author {
Expand Down
24 changes: 24 additions & 0 deletions test.http
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,27 @@ Referer: https://music.youtube.com/
}
}
}

###

POST https://music.youtube.com/youtubei/v1/next?key=AIzaSyC9XL3ZjWddXya6X74dJoCTL-WEYFDNX30&prettyPrint=false
origin: https://music.youtube.com
Content-Type: application/json

{
"videoId": "Yx6GdcG5XdM",
"playlistId": "RDAMVMYx6GdcG5XdM",
"params": "8gEAmgMDCNgE",
"context": {
"client": {
"hl": "de",
"gl": "DE",
"clientName": "WEB_REMIX",
"clientVersion": "1.20230102.01.00"
}
}
}

###


0 comments on commit 0b7b2f7

Please sign in to comment.