diff --git a/app/src/main/java/com/maxrave/simpmusic/ui/screen/player/FullscreenPlayer.kt b/app/src/main/java/com/maxrave/simpmusic/ui/screen/player/FullscreenPlayer.kt index c01e8b7b..65db6d7d 100644 --- a/app/src/main/java/com/maxrave/simpmusic/ui/screen/player/FullscreenPlayer.kt +++ b/app/src/main/java/com/maxrave/simpmusic/ui/screen/player/FullscreenPlayer.kt @@ -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 { diff --git a/kotlinYtmusicScraper/src/main/java/com/maxrave/kotlinytmusicscraper/YouTube.kt b/kotlinYtmusicScraper/src/main/java/com/maxrave/kotlinytmusicscraper/YouTube.kt index 719d630c..6f7dbf64 100644 --- a/kotlinYtmusicScraper/src/main/java/com/maxrave/kotlinytmusicscraper/YouTube.kt +++ b/kotlinYtmusicScraper/src/main/java/com/maxrave/kotlinytmusicscraper/YouTube.kt @@ -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() + } catch (e: Exception) { + ytMusic.noLogInPlayer(videoId).body() + } println("Player Response " + playerResponse) // val ytScrapeInitial: YouTubeInitialPage = ytMusic.player(WEB, videoId, playlistId, cpn).body() println("Thumbnails " + playerResponse.videoDetails?.thumbnail) diff --git a/kotlinYtmusicScraper/src/main/java/com/maxrave/kotlinytmusicscraper/Ytmusic.kt b/kotlinYtmusicScraper/src/main/java/com/maxrave/kotlinytmusicscraper/Ytmusic.kt index 11739351..345cf711 100644 --- a/kotlinYtmusicScraper/src/main/java/com/maxrave/kotlinytmusicscraper/Ytmusic.kt +++ b/kotlinYtmusicScraper/src/main/java/com/maxrave/kotlinytmusicscraper/Ytmusic.kt @@ -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 @@ -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 @@ -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 { @@ -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( diff --git a/kotlinYtmusicScraper/src/main/java/com/maxrave/kotlinytmusicscraper/models/YouTubeClient.kt b/kotlinYtmusicScraper/src/main/java/com/maxrave/kotlinytmusicscraper/models/YouTubeClient.kt index e4682616..325dbc57 100644 --- a/kotlinYtmusicScraper/src/main/java/com/maxrave/kotlinytmusicscraper/models/YouTubeClient.kt +++ b/kotlinYtmusicScraper/src/main/java/com/maxrave/kotlinytmusicscraper/models/YouTubeClient.kt @@ -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, @@ -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/" @@ -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, @@ -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, ) } } \ No newline at end of file diff --git a/kotlinYtmusicScraper/src/main/java/com/maxrave/kotlinytmusicscraper/test/main.kt b/kotlinYtmusicScraper/src/main/java/com/maxrave/kotlinytmusicscraper/test/main.kt index c3182162..23bb57b6 100644 --- a/kotlinYtmusicScraper/src/main/java/com/maxrave/kotlinytmusicscraper/test/main.kt +++ b/kotlinYtmusicScraper/src/main/java/com/maxrave/kotlinytmusicscraper/test/main.kt @@ -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() } } @@ -25,9 +28,8 @@ fun testPlayer() { Ytmusic() .apply { locale = YouTubeLocale("VN", "vi") - cookie = "" }.player( - YouTubeClient.TVHTML5, + YouTubeClient.IOS, "ctiKD8jtvV8", null, (1..16) @@ -40,7 +42,8 @@ fun testPlayer() { ] }.joinToString(""), ).apply { - println(bodyAsText()) + println(toString()) + println(body()) } } }