From 1e161e8a77d82e950fa82b87251f76a95094fb7c Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Mon, 9 Dec 2024 20:48:56 -0500 Subject: [PATCH] Use credentialTransferToken to play age restricted videos (#498) * Use credentialTransferToken to play age restricted videos * changelog --- CHANGELOG.md | 1 + .../Services/Innertube/InnertubeService.bs | 26 ++++++++++++++++--- .../Services/LoungeService/LoungeApi.bs | 11 ++++++++ .../Services/LoungeService/LoungeService.xml | 1 + .../VideoPlayer/VideoContentTask.bs | 6 ++++- .../src/components/VideoPlayer/VideoPlayer.bs | 1 + 6 files changed, 41 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 635bd886..69882ed6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Search suggestions to Playlet backend +- Support for age restrcited videos and private videos when cast from YouTube app (logged in) ## [0.32.1] - 2024-12-03 diff --git a/playlet-lib/src/components/Services/Innertube/InnertubeService.bs b/playlet-lib/src/components/Services/Innertube/InnertubeService.bs index 4e68b57e..a3a4d330 100644 --- a/playlet-lib/src/components/Services/Innertube/InnertubeService.bs +++ b/playlet-lib/src/components/Services/Innertube/InnertubeService.bs @@ -18,6 +18,7 @@ namespace InnertubeService function GetVideoMetadata(videoId as string, options = invalid as object) as object cancellation = invalid fetchNext = true + ctt = "" if options <> invalid if options.DoesExist("cancellation") @@ -26,16 +27,19 @@ namespace InnertubeService if options.DoesExist("fetchNext") fetchNext = options.fetchNext end if + if options.DoesExist("ctt") + ctt = options.ctt + end if end if - playerRequest = CreatePlayerRequest(videoId) + playerRequest = CreatePlayerRequest(videoId, ctt) playerRequest.Cancellation(cancellation) playerRequest.Send() nextRequest = invalid nextResponse = invalid if fetchNext - nextRequest = CreateNextRequest(videoId) + nextRequest = CreateNextRequest(videoId, ctt) nextRequest.Cancellation(cancellation) nextRequest.Send() end if @@ -67,7 +71,7 @@ namespace InnertubeService return playerResponse end function - function CreatePlayerRequest(videoId as string) as object + function CreatePlayerRequest(videoId as string, ctt as string) as object deviceInfo = CreateObject("roDeviceInfo") payload = { @@ -124,6 +128,13 @@ namespace InnertubeService } } + if not StringUtils.IsNullOrEmpty(ctt) + payload["context"]["user"]["credentialTransferTokens"] = [{ + "token": ctt + "scope": "VIDEO" + }] + end if + request = HttpClient.Post("https://www.youtube.com/youtubei/v1/player?prettyPrint=false&alt=json", FormatJson(payload)) request.Headers({ "accept": "*/*" @@ -137,7 +148,7 @@ namespace InnertubeService return request end function - function CreateNextRequest(videoId as string) as object + function CreateNextRequest(videoId as string, ctt as string) as object deviceInfo = CreateObject("roDeviceInfo") payload = { @@ -185,6 +196,13 @@ namespace InnertubeService } } + if not StringUtils.IsNullOrEmpty(ctt) + payload["context"]["user"]["credentialTransferTokens"] = [{ + "token": ctt + "scope": "VIDEO" + }] + end if + request = HttpClient.Post("https://www.youtube.com/youtubei/v1/next?prettyPrint=false&alt=json", FormatJson(payload)) request.Headers({ "accept": "*/*" diff --git a/playlet-lib/src/components/Services/LoungeService/LoungeApi.bs b/playlet-lib/src/components/Services/LoungeService/LoungeApi.bs index 68301714..fc9444c8 100644 --- a/playlet-lib/src/components/Services/LoungeService/LoungeApi.bs +++ b/playlet-lib/src/components/Services/LoungeService/LoungeApi.bs @@ -404,6 +404,17 @@ class LoungeApi function HandleSetPlaylist(_commandId as integer, commandType as string, commandArgs as object, _messagesToSend as object) LogDebug(commandType, commandArgs) + ' TODO:P2 ctt: credentialTransferToken + ' We receive credentialTransferTokens here that can be attached to innertube requests. + ' The tokens belong to the user logged in on the mobile app. + ' These allow us to watch age restricted videos and private videos. + ' They are scoped by video. + ' Right now we just store only the last ctt, which works ok if we're casting one video at a time. + ' But if the age restrcited video is added to the queue, we don't capture a token and playing the video would fail. + ' These tokens need to be managed better, based on the video they belong to. + if not StringUtils.IsNullOrEmpty(commandArgs.ctt) + m.loungeServiceNode.ctt = commandArgs.ctt + end if LoungeVideoQueue.SetPlaylist(m.videoQueueNode, m.invService, commandArgs) end function diff --git a/playlet-lib/src/components/Services/LoungeService/LoungeService.xml b/playlet-lib/src/components/Services/LoungeService/LoungeService.xml index e31a1b1f..dfee9dce 100644 --- a/playlet-lib/src/components/Services/LoungeService/LoungeService.xml +++ b/playlet-lib/src/components/Services/LoungeService/LoungeService.xml @@ -5,6 +5,7 @@ + diff --git a/playlet-lib/src/components/VideoPlayer/VideoContentTask.bs b/playlet-lib/src/components/VideoPlayer/VideoContentTask.bs index 5b2ee25a..c260de09 100644 --- a/playlet-lib/src/components/VideoPlayer/VideoContentTask.bs +++ b/playlet-lib/src/components/VideoPlayer/VideoContentTask.bs @@ -6,6 +6,7 @@ function VideoContentTask(input as object) as object contentNode = input.content invidiousNode = input.invidious preferencesNode = input.preferences + loungeServiceNode = input.loungeService playletServerPort = input.playletServerPort if m.top.cancel @@ -20,7 +21,10 @@ function VideoContentTask(input as object) as object backend = preferencesNode["playback.backend"] if backend = "playlet" - response = InnertubeService.GetVideoMetadata(contentNode.videoId, { cancellation: m.top.cancellation }) + response = InnertubeService.GetVideoMetadata(contentNode.videoId, { + cancellation: m.top.cancellation + ctt: loungeServiceNode.ctt + }) else response = service.GetVideoMetadata(contentNode.videoId, { cancellation: m.top.cancellation }) end if diff --git a/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs b/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs index c183179a..db62aa27 100644 --- a/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs +++ b/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs @@ -152,6 +152,7 @@ function StartVideoContentTask(videoContentNode as object) content: videoContentNode invidious: m.top.invidious preferences: m.top.preferences + loungeService: m.top.loungeService playletServerPort: m.top.webServer.port }, OnVideoContentTaskResults) end function