Skip to content

Commit

Permalink
make signatureTimestamp optional and report exception if it could n…
Browse files Browse the repository at this point in the history
…ot be parsed
  • Loading branch information
gechoto authored Jan 6, 2025
1 parent b8050ae commit 9696332
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
25 changes: 23 additions & 2 deletions app/src/main/java/com/zionhuang/music/utils/YTPlayerUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,16 @@ object YTPlayerUtils {
audioQuality: AudioQuality,
connectivityManager: ConnectivityManager,
): Result<PlaybackData> = runCatching {
/**
* This is required for some clients to get working streams however
* it should not be forced for the [MAIN_CLIENT] because the response of the [MAIN_CLIENT]
* is required even if the streams won't work from this client.
* This is why it is allowed to be null.
*/
val signatureTimestamp = getSignatureTimestampOrNull(videoId)

val mainPlayerResponse =
YouTube.player(videoId, playlistId, client = MAIN_CLIENT).getOrThrow()
YouTube.player(videoId, playlistId, MAIN_CLIENT, signatureTimestamp).getOrThrow()

val audioConfig = mainPlayerResponse.playerConfig?.audioConfig
val videoDetails = mainPlayerResponse.videoDetails
Expand All @@ -78,7 +86,7 @@ object YTPlayerUtils {
// after main client use fallback clients
val client = STREAM_FALLBACK_CLIENTS[clientIndex]
streamPlayerResponse =
YouTube.player(videoId, playlistId, client).getOrNull()
YouTube.player(videoId, playlistId, client, signatureTimestamp).getOrNull()
}

// process current client response
Expand Down Expand Up @@ -181,6 +189,19 @@ object YTPlayerUtils {
return false
}

/**
* Wrapper around the [NewPipeUtils.getSignatureTimestamp] function which reports exceptions
*/
private fun getSignatureTimestampOrNull(
videoId: String
): Int? {
return NewPipeUtils.getSignatureTimestamp(videoId)
.onFailure {
reportException(it)
}
.getOrNull()
}

/**
* Wrapper around the [NewPipeUtils.getStreamUrl] function which reports exceptions
*/
Expand Down
5 changes: 3 additions & 2 deletions innertube/src/main/java/com/zionhuang/innertube/InnerTube.kt
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class InnerTube {
client: YouTubeClient,
videoId: String,
playlistId: String?,
signatureTimestamp: Int?,
) = httpClient.post("player") {
ytClient(client, setLogin = true)
setBody(
Expand All @@ -138,9 +139,9 @@ class InnerTube {
videoId = videoId,
playlistId = playlistId,
playbackContext =
if (client.useSignatureTimestamp) {
if (client.useSignatureTimestamp && signatureTimestamp != null) {
PlayerBody.PlaybackContext(PlayerBody.PlaybackContext.ContentPlaybackContext(
signatureTimestamp = NewPipeUtils.getSignatureTimestamp(videoId).getOrThrow()
signatureTimestamp
))
} else null
)
Expand Down
4 changes: 2 additions & 2 deletions innertube/src/main/java/com/zionhuang/innertube/YouTube.kt
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,8 @@ object YouTube {
}
}

suspend fun player(videoId: String, playlistId: String? = null, client: YouTubeClient): Result<PlayerResponse> = runCatching {
innerTube.player(client, videoId, playlistId).body<PlayerResponse>()
suspend fun player(videoId: String, playlistId: String? = null, client: YouTubeClient, signatureTimestamp: Int? = null): Result<PlayerResponse> = runCatching {
innerTube.player(client, videoId, playlistId, signatureTimestamp).body<PlayerResponse>()
}

suspend fun next(endpoint: WatchEndpoint, continuation: String? = null): Result<NextResult> = runCatching {
Expand Down

0 comments on commit 9696332

Please sign in to comment.