Skip to content

Commit

Permalink
Fixed YouTube Changed API temporary
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrave-dev committed Dec 18, 2024
1 parent 45bba1d commit c77f927
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ fun FullscreenPlayer(
val context = LocalContext.current

DisposableEffect(true) {
val activity = context.findActivity() ?: return@DisposableEffect onDispose {}
val activity = context.findActivity()
val originalOrientation = activity.requestedOrientation
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
val window = context.findActivity()?.window ?: return@DisposableEffect onDispose {}
val window = context.findActivity().window
val insetsController = WindowCompat.getInsetsController(window, window.decorView)

onDispose {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1272,8 +1272,11 @@ class YouTube {
),
]
}.joinToString("")
val playerResponse =
val playerResponse = try {
ytMusic.player(if (cookie != null) ANDROID_MUSIC else IOS, videoId, playlistId, cpn).body<PlayerResponse>()
} catch (e: Exception) {
ytMusic.noLogInPlayer(videoId).body<PlayerResponse>()
}
println("Player Response " + playerResponse)
// val ytScrapeInitial: YouTubeInitialPage = ytMusic.player(WEB, videoId, playlistId, cpn).body<YouTubeInitialPage>()
println("Thumbnails " + playerResponse.videoDetails?.thumbnail)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import io.ktor.client.plugins.cookies.AcceptAllCookiesStorage
import io.ktor.client.plugins.cookies.HttpCookies
import io.ktor.client.plugins.defaultRequest
import io.ktor.client.request.HttpRequestBuilder
import io.ktor.client.request.accept
import io.ktor.client.request.get
import io.ktor.client.request.header
import io.ktor.client.request.headers
Expand Down Expand Up @@ -99,7 +100,7 @@ class Ytmusic {
gl = Locale.getDefault().country,
hl = Locale.getDefault().toLanguageTag(),
)
var visitorData: String = "Cgt6SUNYVzB2VkJDbyjGrrSmBg%3D%3D"
var visitorData: String = "CgtMN0FkbDFaWERfdyi8t4u7BjIKCgJWThIEGgAgWQ%3D%3D"
var cookie: String? = null
set(value) {
field = value
Expand Down Expand Up @@ -273,22 +274,22 @@ class Ytmusic {
private fun createClient() =
HttpClient(OkHttp) {
expectSuccess = true
if (cachePath != null) {
engine {
config {
cache(
okhttp3.Cache(cachePath!!, 50L * 1024 * 1024),
)
}
if (cacheControlInterceptor != null) {
addNetworkInterceptor(cacheControlInterceptor!!)
}
if (forceCacheInterceptor != null) {
addInterceptor(forceCacheInterceptor!!)
}
}
}
install(HttpCache)
// if (cachePath != null) {
// engine {
// config {
// cache(
// okhttp3.Cache(cachePath!!, 50L * 1024 * 1024),
// )
// }
// if (cacheControlInterceptor != null) {
// addNetworkInterceptor(cacheControlInterceptor!!)
// }
// if (forceCacheInterceptor != null) {
// addInterceptor(forceCacheInterceptor!!)
// }
// }
// }
// install(HttpCache)
install(ContentNegotiation) {
json(
Json {
Expand Down Expand Up @@ -396,12 +397,29 @@ class Ytmusic {
contentType(ContentType.Application.Json)
}

suspend fun noLogInPlayer(
videoId: String
) = httpClient.post("player") {
accept(ContentType.Application.Json)
contentType(ContentType.Application.Json)
header("Host", "music.youtube.com")
setBody(
PlayerBody(
context = YouTubeClient.IOS.toContext(locale, visitorData),
playlistId = null,
cpn = null,
param = null,
videoId = videoId,
)
)
}

suspend fun player(
client: YouTubeClient,
videoId: String,
playlistId: String?,
cpn: String?,
) = httpClient.post("player") {
) = createClient().post("player") {
ytClient(client, setLogin = true)
setBody(
PlayerBody(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ data class YouTubeClient(
val userAgent: String,
val referer: String? = null,
val osVersion: String? = null,
val deviceMake: String? = null,
val deviceModel: String? = null,
val acceptHeader: String? = null,
val timeZone: String? = null,
val utcOffsetMinutes: Int? = null,
) {
fun toContext(
locale: YouTubeLocale,
Expand All @@ -27,9 +32,36 @@ data class YouTubeClient(
hl = locale.hl,
visitorData = visitorData,
osVersion = osVersion,
// userAgent = userAgent,
// deviceMake = deviceMake,
// deviceModel = deviceModel,
// acceptHeader = acceptHeader,
// timeZone = timeZone,
// utcOffsetMinutes = utcOffsetMinutes,
),
)

// {
// "context": {
// "client": {
// "clientName": "IOS",
// "clientVersion": "19.29.1",
// "hl": "en",
// "gl": "US",
// "visitorData": "CgtsZG1ySnZiQWtSbyiMjuGSBg%3D%3D",
// "userAgent": "com.google.ios.youtube/19.29.1 (iPhone16,2; U; CPU iOS 17_5_1 like Mac OS X;)",
// "deviceMake": "Apple",
// "deviceModel": "iPhone16,2",
// "osName": "iOS",
// "osVersion": "17.5.1.21F90",
// "acceptHeader": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
// "timeZone": "UTC",
// "utcOffsetMinutes": 0
// }
// },
// "videoId": "rPnIcUgrdnU"
// }

companion object {
private const val REFERER_YOUTUBE_MUSIC = "https://music.youtube.com/"
private const val REFERER_YOUTUBE = "https://www.youtube.com/"
Expand All @@ -41,7 +73,6 @@ data class YouTubeClient(
val ANDROID_MUSIC =
YouTubeClient(
clientName = "ANDROID_MUSIC",
// clientVersion = "5.01",
clientVersion = "6.33.52",
api_key = "AIzaSyAOghZGza2MQSZkY_zfZ370N-PUdXEo8AI",
userAgent = USER_AGENT_ANDROID,
Expand Down Expand Up @@ -111,6 +142,11 @@ data class YouTubeClient(
api_key = "AIzaSyB-63vPrdThhKuerbB2N_l7Kwwcxj6yUAc",
userAgent = USER_AGENT_IOS,
osVersion = "17.5.1.21F90",
// deviceMake = "Apple",
// deviceModel = "iPhone16,2",
// acceptHeader = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
// timeZone = "UTC",
// utcOffsetMinutes = 0,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ import com.maxrave.kotlinytmusicscraper.models.SectionListRenderer
import com.maxrave.kotlinytmusicscraper.models.Thumbnail
import com.maxrave.kotlinytmusicscraper.models.YouTubeClient
import com.maxrave.kotlinytmusicscraper.models.YouTubeLocale
import com.maxrave.kotlinytmusicscraper.models.response.PlayerResponse
import io.ktor.client.call.body
import io.ktor.client.statement.bodyAsText
import kotlinx.coroutines.runBlocking
import kotlin.random.Random

fun main() {
runBlocking {
testPlayer()
}
}

Expand All @@ -25,9 +28,8 @@ fun testPlayer() {
Ytmusic()
.apply {
locale = YouTubeLocale("VN", "vi")
cookie = ""
}.player(
YouTubeClient.TVHTML5,
YouTubeClient.IOS,
"ctiKD8jtvV8",
null,
(1..16)
Expand All @@ -40,7 +42,8 @@ fun testPlayer() {
]
}.joinToString(""),
).apply {
println(bodyAsText())
println(toString())
println(body<PlayerResponse>())
}
}
}
Expand Down

0 comments on commit c77f927

Please sign in to comment.