Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.
Kotlin alternativeandroidandroid-applicationclientglassmorphisminnertubelibrespotliquid-glassspotifyspotify-connectspotifyapiyoutube-musicyoutube-playerytdlnis
Play and download YouTube songs as the account when YouTube asks for a bot check
Lelonio256a700846be
diff --git a/app/src/main/java/dev/lelonio/square/backend/youtube/SignedInStreams.kt b/app/src/main/java/dev/lelonio/square/backend/youtube/SignedInStreams.ktindex a946bce..c964b25 100644--- a/app/src/main/java/dev/lelonio/square/backend/youtube/SignedInStreams.kt+++ b/app/src/main/java/dev/lelonio/square/backend/youtube/SignedInStreams.kt@@ -50,11 +50,19 @@ object SignedInStreams {/*** Whether [error], from asking without an account, is YouTube asking for- * one: an age check, or a song it serves to accounts alone.+ * one: an age check, a song it serves to accounts alone, or its "confirm+ * you're not a bot", which it puts in front of every song for a network it+ * has stopped trusting.+ *+ * That last one is not a ContentNotAvailableException, so it was not taken+ * for a request for an account: a listener signed in, on such a network,+ * had every song fail and none tried as the account, which YouTube would+ * have played.*/fun needsAccount(error: Throwable): Boolean =generateSequence(error) { it.cause }.any {it is AgeRestrictedContentException ||+ it is org.schabi.newpipe.extractor.exceptions.SignInConfirmNotBotException ||(it is ContentNotAvailableException && it.message.orEmpty().contains("LOGIN_REQUIRED"))}diff --git a/app/src/main/java/dev/lelonio/square/download/YouTubeDownloads.kt b/app/src/main/java/dev/lelonio/square/download/YouTubeDownloads.ktindex 08cd53b..9a9b430 100644--- a/app/src/main/java/dev/lelonio/square/download/YouTubeDownloads.kt+++ b/app/src/main/java/dev/lelonio/square/download/YouTubeDownloads.kt@@ -102,12 +102,25 @@ object YouTubeDownloads {cancelled.remove(trackUri)YouTubeStreams.ensureNewPipe()val id = YouTubeBackend.videoIdOfUri(trackUri)- val info = StreamInfo.getInfo(ServiceList.YouTube, YouTubeStreams.WATCH_URL + id)- val stream = info.audioStreams- .filter { !it.content.isNullOrEmpty() }- .maxByOrNull { it.averageBitrate }- ?: error("no audio for $id")- val extension = stream.format?.suffix ?: "m4a"+ // Asked without an account first, and as the account where YouTube+ // wants one, as the player does. Without the second, a network+ // YouTube had stopped trusting ("confirm you're not a bot") could not+ // download anything, signed in or not (#81).+ val source = try {+ val info = StreamInfo.getInfo(ServiceList.YouTube, YouTubeStreams.WATCH_URL + id)+ val stream = info.audioStreams+ .filter { !it.content.isNullOrEmpty() }+ .maxByOrNull { it.averageBitrate }+ ?: error("no audio for $id")+ Source(stream.content, stream.format?.suffix ?: "m4a", stream.averageBitrate.takeIf { it > 0 } ?: 0)+ } catch (error: Exception) {+ if (!dev.lelonio.square.backend.youtube.SignedInStreams.needsAccount(error)) throw error+ val url = dev.lelonio.square.backend.youtube.SignedInStreams.url(+ context, id, dev.lelonio.square.backend.youtube.SignedInStreams.Kind.AUDIO, lowRate = false,+ )+ Source(url, if ("mime=audio%2Fwebm" in url) "webm" else "m4a", 0)+ }+ val extension = source.extensionval dir = directory(context)val part = File(dir, "$id.$extension.$PARTIAL")@@ -119,7 +132,7 @@ object YouTubeDownloads {while (total < 0 || offset < total) {if (trackUri in cancelled) throw kotlinx.coroutines.CancellationException("removed")val request = Request.Builder()- .url(stream.content)+ .url(source.url).header("Range", "bytes=$offset-${offset + CHUNK_BYTES - 1}").build()http.newCall(request).execute().use { response ->@@ -165,11 +178,14 @@ object YouTubeDownloads {trackId = id,format = extension,bytes = done.length(),- kbps = stream.averageBitrate.takeIf { it > 0 } ?: 0,+ kbps = source.kbps,downloadedAt = System.currentTimeMillis(),)}+ /** Where a song's audio is fetched from, and what it is. */+ private class Source(val url: String, val extension: String, val kbps: Int)+private const val PARTIAL = "part"private const val READ_TIMEOUT_SECONDS = 30Ldiff --git a/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt b/app/src/main/java/dev/lelonio/square/ui/MainViewModel.ktindex e39c578..13fdeb6 100644--- a/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt+++ b/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt@@ -2192,7 +2192,14 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {if (container.activeBackend.id != BackendId.SPOTIFY) {// No engine to authenticate, so nothing to send the service.- refresh()+ //+ // Once this object is whole, not from inside its construction.+ // The view model's scope runs on the main thread straight away, so+ // a load that did not have to wait for anything (nobody signed in,+ // a library already at hand) went on into the saved albums and the+ // followed artists, whose fields are declared further down this+ // class and were still null: the app closed as it opened.+ viewModelScope.launch(Dispatchers.Main) { refresh() }} else if (container.spotifySignedIn) {PlaybackService.connect(app)refresh()