Skip to content

Commit

Permalink
PlayerResponse: move findUrl function because we don't need the videoId
Browse files Browse the repository at this point in the history
  • Loading branch information
gechoto authored Dec 24, 2024
1 parent 8f8331f commit c5e5474
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class DownloadUtil @Inject constructor(
)
}

val streamUrl = playerResponse.findUrl(format.itag)
val streamUrl = format.findUrl()

songUrlCache[mediaId] = streamUrl!! to playerResponse.streamingData!!.expiresInSeconds * 1000L
dataSpec.withUri(streamUrl.toUri())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ class MusicService : MediaLibraryService(),
}
scope.launch(Dispatchers.IO) { recoverSong(mediaId, playerResponse) }

val streamUrl = playerResponse.findUrl(format.itag)
val streamUrl = format.findUrl()

songUrlCache[mediaId] = streamUrl!! to playerResponse.streamingData!!.expiresInSeconds * 1000L
dataSpec.withUri(streamUrl.toUri()).subrange(dataSpec.uriPositionOffset, CHUNK_LENGTH)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ data class PlayerResponse(
) {
val isAudio: Boolean
get() = width == null

fun findUrl(): String? {
this.url?.let {
return it
}
this.signatureCipher?.let { signatureCipher ->
val params = parseQueryString(signatureCipher)
val obfuscatedSignature = params["s"] ?: return null
val signatureParam = params["sp"] ?: return null
val url = params["url"]?.let { URLBuilder(it) } ?: return null
url.parameters[signatureParam] = YoutubeJavaScriptPlayerManager.deobfuscateSignature("", obfuscatedSignature)
val streamUrl = YoutubeJavaScriptPlayerManager.getUrlWithThrottlingParameterDeobfuscated("", url.toString())
return streamUrl
}
return null
}
}
}

Expand All @@ -78,22 +94,4 @@ data class PlayerResponse(
val viewCount: String,
val thumbnail: Thumbnails,
)

fun findUrl(itag: Int): String? {
this.streamingData?.adaptiveFormats?.find { it.itag == itag }?.let { format ->
format.url?.let {
return it
}
format.signatureCipher?.let { signatureCipher ->
val params = parseQueryString(signatureCipher)
val obfuscatedSignature = params["s"] ?: return null
val signatureParam = params["sp"] ?: return null
val url = params["url"]?.let { URLBuilder(it) } ?: return null
url.parameters[signatureParam] = YoutubeJavaScriptPlayerManager.deobfuscateSignature("", obfuscatedSignature)
val streamUrl = YoutubeJavaScriptPlayerManager.getUrlWithThrottlingParameterDeobfuscated("", url.toString())
return streamUrl
}
}
return null
}
}

0 comments on commit c5e5474

Please sign in to comment.