Skip to content

Commit 6fc2b51

Browse files
committed
feat: enhance YouTube service error handling and improve URL signature processing
1 parent 1925c0f commit 6fc2b51

3 files changed

Lines changed: 12 additions & 7 deletions

File tree

app/src/main/java/com/maxrave/simpmusic/service/SimpleMediaServiceHandler.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import com.maxrave.simpmusic.extension.toListName
5656
import com.maxrave.simpmusic.extension.toMediaItem
5757
import com.maxrave.simpmusic.extension.toSongEntity
5858
import com.maxrave.simpmusic.extension.toTrack
59+
import com.maxrave.simpmusic.pushYouTubeError
5960
import com.maxrave.simpmusic.service.test.source.MergingMediaSourceFactory
6061
import com.maxrave.simpmusic.ui.widget.BasicWidget
6162
import com.maxrave.simpmusic.utils.Resource
@@ -845,6 +846,7 @@ class SimpleMediaServiceHandler(
845846
}
846847

847848
override fun onPlayerError(error: PlaybackException) {
849+
pushYouTubeError(error)
848850
when (error.errorCode) {
849851
PlaybackException.ERROR_CODE_TIMEOUT -> {
850852
Log.e("Player Error", "onPlayerError (${error.errorCode}): ${error.message}")

kotlinYtmusicScraper/src/main/java/com/maxrave/kotlinytmusicscraper/YouTube.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import com.maxrave.kotlinytmusicscraper.models.SongItem
3232
import com.maxrave.kotlinytmusicscraper.models.VideoItem
3333
import com.maxrave.kotlinytmusicscraper.models.WatchEndpoint
3434
import com.maxrave.kotlinytmusicscraper.models.YTItemType
35+
import com.maxrave.kotlinytmusicscraper.models.YouTubeClient
3536
import com.maxrave.kotlinytmusicscraper.models.YouTubeClient.Companion.TVHTML5
3637
import com.maxrave.kotlinytmusicscraper.models.YouTubeClient.Companion.WEB
3738
import com.maxrave.kotlinytmusicscraper.models.YouTubeClient.Companion.WEB_REMIX
@@ -1239,8 +1240,8 @@ class YouTube(
12391240
val listUrlSig = mutableListOf<String>()
12401241
var decodedSigResponse: PlayerResponse? = null
12411242
val listClients = listOf(WEB_REMIX, TVHTML5)
1242-
var sigResponse: PlayerResponse? = null
1243-
var currentClient = listClients.first()
1243+
var sigResponse: PlayerResponse?
1244+
var currentClient: YouTubeClient
12441245
for (client in listClients) {
12451246
listUrlSig.removeAll(listUrlSig)
12461247
decodedSigResponse = null
@@ -1315,7 +1316,7 @@ class YouTube(
13151316
decodedSigResponse
13161317
.streamingData
13171318
?.formats
1318-
?.mapNotNull { Pair(it.itag, it.url) }
1319+
?.map { Pair(it.itag, it.url) }
13191320
?.toMutableList() ?: mutableListOf()
13201321
).apply {
13211322
addAll(
@@ -1327,7 +1328,8 @@ class YouTube(
13271328
listFormat.forEach {
13281329
println("YouTube Format ${it.first} ${it.second}")
13291330
}
1330-
if (listUrlSig.isNotEmpty() && !is403Url(listUrlSig.first())) {
1331+
if (listUrlSig.isNotEmpty() && !is403Url(listUrlSig.last())) {
1332+
println("YouTube SmartTube Found URL ${listUrlSig.last()}")
13311333
break
13321334
} else {
13331335
listUrlSig.clear()
@@ -1378,7 +1380,8 @@ class YouTube(
13781380
?.let { addAll(it) }
13791381
},
13801382
)
1381-
if (listUrlSig.isNotEmpty() && !is403Url(listUrlSig.first())) {
1383+
if (listUrlSig.isNotEmpty() && !is403Url(listUrlSig.last())) {
1384+
println("YouTube NewPipe Found URL ${listUrlSig.last()}")
13821385
break
13831386
}
13841387
}
@@ -1396,7 +1399,7 @@ class YouTube(
13961399
.bodyAsText()
13971400
.let { challenge ->
13981401
val listChallenge = poTokenJsonDeserializer.decodeFromString<List<String?>>(challenge)
1399-
listChallenge.filterIsInstance<String>().firstOrNull()
1402+
listChallenge.filterNotNull().firstOrNull()
14001403
}?.let { poTokenChallenge ->
14011404
ytMusic.generatePoToken(poTokenChallenge).bodyAsText().getPoToken().also { poToken ->
14021405
if (poToken != null) {

0 commit comments

Comments
 (0)