Playlet innertube backend (#463)

* Playlet innertube backend

* Video queue view

* changelog

* Tweaks

* Lint fix

* no message

---------

Co-authored-by: github-action linter <githubaction@githubaction.com>
This commit is contained in:
Brahim Hadricheandgithub-action linter authored and GitHub committed 2024-10-04 15:57:16 -04:00
1 parent 8cb9d7187c
commit d0d45bc1c4
11 files changed
+232 -35

No files matched your search

+1
View File
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ### Added
- Experimental Playlet backend to play videos using Innertube
- Partial Spanish translations (Thanks to Kamborio and gallegonovato) - Partial Spanish translations (Thanks to Kamborio and gallegonovato)
## [0.26.0] - 2024-09-23 ## [0.26.0] - 2024-09-23
+5 -16
View File
@@ -43,22 +43,13 @@ function GetDeviceFriendlyName() as string
end function end function
function ShowAnnouncement() function ShowAnnouncement()
title = "Announcement #2 - web app hot fix" title = "Announcement #3 - Playlet innertube backend"
message = [ message = [
"Invidious servers continue being blocked by YouTube. https://github.com/iBicha/playlet/issues/400" "Currently, most public Invidious instances don't currently work."
"A temporary workaround has been implemented which relies on your Roku device instead of Invidious for streaming data. This workaround is limited, doesn't always work, and doesn't have captions.", "Because of that, a new backend to play videos has been added to the settings, and it is enabled by default."
"How to use:" "If you would like to use your Invidious instance for playback, you can change the backend in the settings."
] "Thank you."
bulletText = [
`Go to the "Remote" screen, and open Playlet Web App in your browser`
"In the web app, tap the video you want to play"
`Choose "Play on ${GetDeviceFriendlyName()} (HOT FIX)"`
]
bottomMessage = [
"We apologize for the inconvenience."
] ]
buttons = [Tr(Locale.Buttons.OK)] buttons = [Tr(Locale.Buttons.OK)]
@@ -66,8 +57,6 @@ function ShowAnnouncement()
dialog = CreateObject("roSGNode", "SimpleDialog") dialog = CreateObject("roSGNode", "SimpleDialog")
dialog.title = title dialog.title = title
dialog.message = message dialog.message = message
dialog.bulletText = bulletText
dialog.bottomMessage = bottomMessage
dialog.buttons = buttons dialog.buttons = buttons
deviceInfo = CreateObject("roDeviceInfo") deviceInfo = CreateObject("roDeviceInfo")
displaySize = deviceInfo.GetDisplaySize() displaySize = deviceInfo.GetDisplaySize()
@@ -42,19 +42,12 @@ function GetDeviceFriendlyName() as string
end function end function
function ShowAnnouncement() function ShowAnnouncement()
title = "Announcement #2 - web app hot fix" title = "Announcement #3 - Playlet innertube backend"
message = [ message = [
"Invidious servers continue being blocked by YouTube. https://github.com/iBicha/playlet/issues/400" "Currently, most public Invidious instances don't currently work."
"A temporary workaround has been implemented which relies on your Roku device instead of Invidious for streaming data. This workaround is limited, doesn't always work, and doesn't have captions." "Because of that, a new backend to play videos has been added to the settings, and it is enabled by default."
"How to use:" "If you would like to use your Invidious instance for playback, you can change the backend in the settings."
] "Thank you."
bulletText = [
"Go to the " + chr(34) + "Remote" + chr(34) + " screen, and open Playlet Web App in your browser"
"In the web app, tap the video you want to play"
("Choose " + chr(34) + "Play on " + bslib_toString(GetDeviceFriendlyName()) + " (HOT FIX)" + chr(34))
]
bottomMessage = [
"We apologize for the inconvenience."
] ]
buttons = [ buttons = [
Tr("OK") Tr("OK")
@@ -62,8 +55,6 @@ function ShowAnnouncement()
dialog = CreateObject("roSGNode", "SimpleDialog") dialog = CreateObject("roSGNode", "SimpleDialog")
dialog.title = title dialog.title = title
dialog.message = message dialog.message = message
dialog.bulletText = bulletText
dialog.bottomMessage = bottomMessage
dialog.buttons = buttons dialog.buttons = buttons
deviceInfo = CreateObject("roDeviceInfo") deviceInfo = CreateObject("roDeviceInfo")
displaySize = deviceInfo.GetDisplaySize() displaySize = deviceInfo.GetDisplaySize()
@@ -0,0 +1,172 @@
import "pkg:/source/services/HttpClient.bs"
import "pkg:/source/utils/TimeUtils.bs"
namespace InnertubeService
function GetVideoMetadata(videoId as string, options = invalid as object) as object
request = HttpClient.Post("https://www.youtube.com/youtubei/v1/player", FormatJson(MakePayload(videoId)))
request.Headers({
"accept": "*/*"
"accept-language": "*"
"content-type": "application/json"
"user-agent": "com.google.ios.youtube/18.06.35 (iPhone; CPU iPhone OS 14_4 like Mac OS X; en_US)"
"x-youtube-client-name": "5"
"x-youtube-client-version": "18.06.35"
})
cancellation = invalid
if options <> invalid
if options.DoesExist("cancellation")
cancellation = options.cancellation
end if
end if
request.Cancellation(cancellation)
response = request.Await()
if not response.IsSuccess()
return response
end if
parsedResponse = ParseInnertubeVideoResponse(response.Json())
if parsedResponse <> invalid
if parsedResponse.Success
response.OverrideJson(parsedResponse.Metadata)
else
response.OverrideStatusCode(500)
response.OverrideErrorMessage(parsedResponse.Error)
end if
end if
return response
end function
function MakePayload(videoId as string) as object
deviceInfo = CreateObject("roDeviceInfo")
return {
"playbackContext": {
"contentPlaybackContext": {
"vis": 0
"splay": false
"referer": "https://www.youtube.com/watch?v=" + videoId
"currentUrl": "/watch?v=" + videoId
"autonavState": "STATE_ON"
"autoCaptionsDefaultOn": false
"html5Preference": "HTML5_PREF_WANTS"
"lactMilliseconds": "-1"
}
}
"attestationRequest": {
"omitBotguardData": true
}
"racyCheckOk": true
"contentCheckOk": true
"videoId": videoId
"context": {
"client": {
"hl": "en"
"gl": "US"
"remoteHost": ""
"screenDensityFloat": 1
"screenHeightPoints": 1440
"screenPixelDensity": 1
"screenWidthPoints": 2560
"visitorData": ""
"clientName": "iOS"
"clientVersion": "18.06.35"
"osName": "iOS"
"osVersion": "10.0"
"platform": "MOBILE"
"clientFormFactor": "UNKNOWN_FORM_FACTOR"
"userInterfaceTheme": "USER_INTERFACE_THEME_LIGHT"
"timeZone": deviceInfo.GetTimeZone()
"originalUrl": "https://www.youtube.com"
"deviceMake": "Apple"
"deviceModel": "iPhone10,6"
"utcOffsetMinutes": -240
"memoryTotalKbytes": "8000000"
"mainAppWebInfo": {
"graftUrl": "https://www.youtube.com"
"pwaInstallabilityStatus": "PWA_INSTALLABILITY_STATUS_UNKNOWN"
"webDisplayMode": "WEB_DISPLAY_MODE_BROWSER"
"isWebNativeShareAvailable": true
}
}
"user": {
"enableSafetyMode": false
"lockedSafetyMode": false
}
"request": {
"useSsl": true
"internalExperimentFlags": []
}
}
}
end function
function ParseInnertubeVideoResponse(payload as object) as object
if not IsAssociativeArray(payload)
return {
Success: false
Error: "Invalid payload"
}
end if
playabilityStatus = payload["playabilityStatus"]
if not IsAssociativeArray(playabilityStatus)
return {
Success: false
Error: "Invalid playability status"
}
end if
if playabilityStatus["status"] <> "OK"
return {
Success: false
Error: playabilityStatus["reason"]
}
end if
videoDetails = payload["videoDetails"]
if not IsAssociativeArray(videoDetails)
return {
Success: false
Error: "Invalid video details"
}
end if
streamingData = payload["streamingData"]
if not IsAssociativeArray(streamingData)
return {
Success: false
Error: "Invalid streaming data"
}
end if
videoInfo = {
"type": "video"
"title": videoDetails["title"]
"videoId": videoDetails["videoId"]
"videoThumbnails": videoDetails["thumbnail"]["thumbnails"]
"storyboards": []
"viewCount": videoDetails["viewCount"].ToInt()
"author": videoDetails["author"]
"authorId": videoDetails["channelId"]
"lengthSeconds": videoDetails["lengthSeconds"].ToInt()
"liveNow": videoDetails["isLiveContent"]
"hlsUrl": streamingData["hlsManifestUrl"]
"adaptiveFormats": []
"formatStreams": []
"captions": []
"recommendedVideos": []
}
return {
Success: true
Metadata: videoInfo
}
end function
end namespace
@@ -1,3 +1,4 @@
import "pkg:/components/Services/Innertube/InnertubeService.bs"
import "pkg:/components/Services/Invidious/InvidiousService.bs" import "pkg:/components/Services/Invidious/InvidiousService.bs"
@asynctask @asynctask
@@ -17,7 +18,12 @@ function VideoContentTask(input as object) as object
service = new Invidious.InvidiousService(invidiousNode) service = new Invidious.InvidiousService(invidiousNode)
response = service.GetVideoMetadata(contentNode.videoId, { cancellation: m.top.cancellation }) backend = preferencesNode["playback.backend"]
if backend = "playlet"
response = InnertubeService.GetVideoMetadata(contentNode.videoId, { cancellation: m.top.cancellation })
else
response = service.GetVideoMetadata(contentNode.videoId, { cancellation: m.top.cancellation })
end if
if m.top.cancel if m.top.cancel
return invalid return invalid
@@ -144,6 +144,7 @@ function LoadVideoDetailsIfNeeded() as void
m.loadDetailsTask = AsyncTask.Start(Tasks.VideoQueueViewContentTask, { m.loadDetailsTask = AsyncTask.Start(Tasks.VideoQueueViewContentTask, {
videoNodes: m.videosDetailsToLoad videoNodes: m.videosDetailsToLoad
invidious: m.top.invidious invidious: m.top.invidious
preferences: m.top.preferences
index: indexInLoadingArray index: indexInLoadingArray
}) })
end function end function
@@ -5,6 +5,7 @@
<field id="videoQueue" type="node" /> <field id="videoQueue" type="node" />
<field id="appController" type="node" /> <field id="appController" type="node" />
<field id="invidious" type="node" /> <field id="invidious" type="node" />
<field id="preferences" type="node" />
<function name="Show" /> <function name="Show" />
</interface> </interface>
@@ -1,3 +1,4 @@
import "pkg:/components/Services/Innertube/InnertubeService.bs"
import "pkg:/components/Services/Invidious/InvidiousService.bs" import "pkg:/components/Services/Invidious/InvidiousService.bs"
import "pkg:/components/Services/Invidious/InvidiousToContentNode.bs" import "pkg:/components/Services/Invidious/InvidiousToContentNode.bs"
import "pkg:/source/services/HttpClient.bs" import "pkg:/source/services/HttpClient.bs"
@@ -7,12 +8,15 @@ import "pkg:/source/utils/MathUtils.bs"
function VideoQueueViewContentTask(input as object) as object function VideoQueueViewContentTask(input as object) as object
videoNodes = input.videoNodes videoNodes = input.videoNodes
invidiousNode = input.invidious invidiousNode = input.invidious
preferencesNode = input.preferences
service = new Invidious.InvidiousService(invidiousNode) service = new Invidious.InvidiousService(invidiousNode)
instance = service.GetInstance() instance = service.GetInstance()
cancellation = m.top.cancellation cancellation = m.top.cancellation
index = MathUtils.Clamp(input.index - 2, 0, videoNodes.Count() - 1) index = MathUtils.Clamp(input.index - 2, 0, videoNodes.Count() - 1)
backend = preferencesNode["playback.backend"]
requestOptions = { requestOptions = {
cacheSeconds: 8640000 ' 100 days cacheSeconds: 8640000 ' 100 days
cancellation: cancellation cancellation: cancellation
@@ -24,7 +28,7 @@ function VideoQueueViewContentTask(input as object) as object
' Need to change it to load on demand using render tracking ' Need to change it to load on demand using render tracking
for i = index to videoNodes.Count() - 1 for i = index to videoNodes.Count() - 1
videoNode = videoNodes[i] videoNode = videoNodes[i]
if not LoadVideoDetail(videoNode, service, instance, requestOptions) if not LoadVideoDetail(videoNode, service, instance, requestOptions, backend)
' Sleep for a bit to avoid creating too much traffic ' Sleep for a bit to avoid creating too much traffic
sleep(500) sleep(500)
end if end if
@@ -36,7 +40,7 @@ function VideoQueueViewContentTask(input as object) as object
for i = index - 1 to 0 step -1 for i = index - 1 to 0 step -1
videoNode = videoNodes[i] videoNode = videoNodes[i]
if not LoadVideoDetail(videoNode, service, instance, requestOptions) if not LoadVideoDetail(videoNode, service, instance, requestOptions, backend)
' Sleep for a bit to avoid creating too much traffic ' Sleep for a bit to avoid creating too much traffic
sleep(1000) sleep(1000)
end if end if
@@ -50,9 +54,15 @@ function VideoQueueViewContentTask(input as object) as object
end function end function
' Returns true if the video was loaded from cache. ' Returns true if the video was loaded from cache.
function LoadVideoDetail(videoNode as object, service as object, instance as string, requestOptions as object) as boolean function LoadVideoDetail(videoNode as object, service as object, instance as string, requestOptions as object, backend as string) as boolean
videoId = videoNode.videoId videoId = videoNode.videoId
response = service.GetVideoMetadata(videoId, requestOptions)
if backend = "playlet"
response = InnertubeService.GetVideoMetadata(videoId, requestOptions)
else
response = service.GetVideoMetadata(videoId, requestOptions)
end if
metadata = response.Json() metadata = response.Json()
if not response.IsSuccess() or metadata = invalid if not response.IsSuccess() or metadata = invalid
@@ -7,6 +7,7 @@ namespace VideoQueueViewUtils
videoQueueView.videoQueue = videoQueue videoQueueView.videoQueue = videoQueue
videoQueueView.appController = appController videoQueueView.appController = appController
videoQueueView.invidious = videoQueue.invidious videoQueueView.invidious = videoQueue.invidious
videoQueueView.preferences = videoQueue.preferences
appController@.PushScreen(videoQueueView) appController@.PushScreen(videoQueueView)
content = videoQueue.content content = videoQueue.content
+17
View File
@@ -32,6 +32,23 @@
}, },
], ],
}, },
{
displayText: "Backend",
key: "playback.backend",
description: "The backend used for fetching video data. The Playlet backend is experimental",
type: "radio",
defaultValue: "playlet",
options: [
{
displayText: "Invidious",
value: "invidious",
},
{
displayText: "Playlet (local)",
value: "playlet",
},
],
},
], ],
}, },
{ {
@@ -616,6 +616,14 @@ namespace HttpClient
m._text = text m._text = text
end function end function
function OverrideJson(json as object)
m._json = json
end function
function OverrideErrorMessage(errorMessage as string)
m._errorMessage = errorMessage
end function
function IsSuccess() as boolean function IsSuccess() as boolean
statusCode = m.StatusCode() statusCode = m.StatusCode()
return statusCode >= 200 and statusCode < 400 return statusCode >= 200 and statusCode < 400