From 83f867a21ccead731180b4de088f2765544b7645 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Thu, 26 Dec 2024 13:14:59 -0500 Subject: [PATCH] Watch history has video info (#524) * Watch history has video info * Lint fix * Fix upcoming in watch history --------- Co-authored-by: github-action linter --- CHANGELOG.md | 6 ++ .../Services/Innertube/InnertubeService.bs | 56 ++++++++++++++++- .../Services/Innertube/NextEndpoint.bs | 28 ++++++++- .../Services/Innertube/NodesParser.bs | 60 +++++++++++++++++++ .../Services/Innertube/protos/params.proto | 5 ++ .../Innertube/protos/params.proto.gen.bs | 19 +++++- .../Innertube/tests/InnertubeTestNode.xml | 8 +++ .../Innertube/tests/SearchFiltersTestNode.xml | 6 -- .../Services/Invidious/InvidiousService.bs | 42 +++++++++---- .../Invidious/InvidiousToContentNode.bs | 6 +- .../components/tests/NextParams.spec.bs | 27 +++++++++ .../components/tests/SearchFilters.spec.bs | 2 +- 12 files changed, 239 insertions(+), 26 deletions(-) create mode 100644 playlet-lib/src/components/Services/Innertube/tests/InnertubeTestNode.xml delete mode 100644 playlet-lib/src/components/Services/Innertube/tests/SearchFiltersTestNode.xml create mode 100644 playlet-lib/src/source/components/tests/NextParams.spec.bs diff --git a/CHANGELOG.md b/CHANGELOG.md index dba9a952..8e56e11c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Fixed + +- Watch history now has more info (title, channel, duration) + ## [0.35.1] - 2024-12-26 ### Added diff --git a/playlet-lib/src/components/Services/Innertube/InnertubeService.bs b/playlet-lib/src/components/Services/Innertube/InnertubeService.bs index 0ae4829d..9a173170 100644 --- a/playlet-lib/src/components/Services/Innertube/InnertubeService.bs +++ b/playlet-lib/src/components/Services/Innertube/InnertubeService.bs @@ -38,7 +38,7 @@ namespace InnertubeService nextRequest = invalid nextResponse = invalid if fetchNext - nextRequest = Innertube.CreateNextRequest(videoId, ctt) + nextRequest = Innertube.CreateNextRequest(videoId, "", ctt) nextRequest.Cancellation(cancellation) nextRequest.Send() end if @@ -74,6 +74,60 @@ namespace InnertubeService return playerResponse end function + function GetVideoInfoBatched(videoIds as object, options = invalid as object) as object + if not IsArray(videoIds) or videoIds.Count() = 0 + return [] + end if + + cancellation = invalid + retry = true + + if options <> invalid + if options.DoesExist("cancellation") + cancellation = options.cancellation + end if + if options.DoesExist("retry") + retry = options.retry + end if + end if + + params = Innertube.GetEncodedNextParams(videoIds) + request = Innertube.CreateNextRequest("", params, "") + request.Cancellation(cancellation) + + response = request.Await() + if not response.IsSuccess() + return [] + end if + + responseData = ObjectUtils.Dig(response.Json(), ["contents", "twoColumnWatchNextResults", "playlist", "playlist"]) + if responseData = invalid + ' Sometimes the response doesn't contain a playlist. + ' The theory here is that Innertube creates a playlist for the videoIds, and sometimes + ' it chooses to respond without the playlist (perhaps to avoid slow responses). + ' A retry after a short sleep usually fixes this. + if retry + LogWarn("Response data missing playlist. Retrying in a bit...") + newOptions = {} + if options <> invalid + newOptions.Append(options) + end if + newOptions["retry"] = false + sleep(200 + Rnd(500)) + return GetVideoInfoBatched(videoIds, newOptions) + else + LogWarn("Response data missing playlist. Not retrying.") + end if + end if + + feeds = Innertube.ParseResponse(responseData) + if feeds.Count() > 0 + return ValidArray(feeds[0].items) + end if + + return [] + end function + function SearchSuggestions(q as string, cancellation = invalid as object) as object suggestions = Innertube.SearchSuggestions(q, cancellation) if suggestions = invalid diff --git a/playlet-lib/src/components/Services/Innertube/NextEndpoint.bs b/playlet-lib/src/components/Services/Innertube/NextEndpoint.bs index b5ee6fec..defda88b 100644 --- a/playlet-lib/src/components/Services/Innertube/NextEndpoint.bs +++ b/playlet-lib/src/components/Services/Innertube/NextEndpoint.bs @@ -1,13 +1,27 @@ +import "pkg:/components/Services/Innertube/Context.bs" +import "pkg:/components/Services/Innertube/protos/params.proto.gen.bs" +import "pkg:/source/services/HttpClient.bs" +import "pkg:/source/utils/Logging.bs" +import "pkg:/source/utils/ObjectUtils.bs" +import "pkg:/source/utils/Types.bs" + namespace Innertube - function CreateNextRequest(videoId as string, ctt as string) as object + function CreateNextRequest(videoId as string, params as string, ctt as string) as object deviceInfo = CreateObject("roDeviceInfo") payload = { - "videoId": videoId "context": Innertube.CreateContext(Innertube.ClientType.Web, deviceInfo, ctt) } + if videoId <> "" + payload["videoId"] = videoId + end if + + if params <> "" + payload["params"] = params + end if + request = HttpClient.PostJson("https://www.youtube.com/youtubei/v1/next?prettyPrint=false&alt=json", payload) request.Headers({ "accept": "*/*" @@ -20,6 +34,16 @@ namespace Innertube return request end function + function GetEncodedNextParams(videoIds as object) as string + if not IsArray(videoIds) or videoIds.Count() = 0 + return "" + end if + + return Protobuf.Generated.misc.encodeNextParams({ + "video_id": videoIds + }) + end function + function ParseInnertubeNextResponse(payload as object, parsedResponse as object) as object if not parsedResponse.Success LogWarn("Invalid parsed response", parsedResponse) diff --git a/playlet-lib/src/components/Services/Innertube/NodesParser.bs b/playlet-lib/src/components/Services/Innertube/NodesParser.bs index e5424386..fe3260fb 100644 --- a/playlet-lib/src/components/Services/Innertube/NodesParser.bs +++ b/playlet-lib/src/components/Services/Innertube/NodesParser.bs @@ -45,6 +45,7 @@ namespace Innertube "gridVideoRenderer": ParseGridVideoRenderer "channelVideoPlayerRenderer": ParseChannelVideoPlayerRenderer "playlistVideoRenderer": ParsePlaylistVideoRenderer + "playlistPanelVideoRenderer": ParsePlaylistPanelVideoRenderer "channelRenderer": ParseChannelRenderer "gridChannelRenderer": ParseGridChannelRenderer "playlistRenderer": ParsePlaylistRenderer @@ -69,6 +70,9 @@ namespace Innertube "horizontalCardListRenderer": ParseNotImplemented ' expandableTabRenderer contains the search tab with a channel. Ignored. "expandableTabRenderer": ParseNotImplemented + ' Usually contains a "X unavailable video(s) are hidden" message. Ignored. + "messageRenderer": ParseNotImplemented + ' Obviously ignored. "clickTrackingParams": ParseNotImplemented } end function @@ -380,6 +384,62 @@ namespace Innertube return invalid end function + function ParsePlaylistPanelVideoRenderer(nodeData as object, context as object) as object + videoId = nodeData["videoId"] + if not IsString(videoId) + LogWarn("Invalid video ID", nodeData) + return invalid + end if + + author = invalid + authorId = invalid + authorPossibleNodes = ["shortBylineText", "ownerText", "longBylineText"] + for each authorPossibleNode in authorPossibleNodes + node = nodeData[authorPossibleNode] + author = ParseText(node) + authorId = ObjectUtils.Dig(node, ["runs", 0, "navigationEndpoint", "browseEndpoint", "browseId"]) + if author <> "" and authorId <> invalid + exit for + end if + end for + + if context.generateVideoThumbnails = true + videoThumbnails = GenerateVideoThumbnails(videoId) + else + videoThumbnails = ObjectUtils.Dig(nodeData, ["thumbnail", "thumbnails"]) + if videoThumbnails = invalid + videoThumbnails = GenerateVideoThumbnails(videoId) + end if + end if + + isUpcoming = false + liveNow = ValidString(ObjectUtils.Dig(nodeData, ["badges", 0, "metadataBadgeRenderer", "icon", "iconType"])) = "LIVE" + thumbnailOverlays = nodeData["thumbnailOverlays"] + if IsArray(thumbnailOverlays) + for each overlay in thumbnailOverlays + liveNow = liveNow or (ValidString(ObjectUtils.Dig(overlay, ["thumbnailOverlayTimeStatusRenderer", "style"])) = "LIVE") + isUpcoming = isUpcoming or (ValidString(ObjectUtils.Dig(overlay, ["thumbnailOverlayTimeStatusRenderer", "style"])) = "UPCOMING") + end for + end if + + lengthText = ParseText(nodeData["lengthText"]) + + video = { + "type": "video" + "videoId": videoId + "author": author + "authorId": authorId + "title": ParseText(nodeData["title"]) + "videoThumbnails": videoThumbnails + "lengthText": lengthText + "liveNow": liveNow + "isUpcoming": isUpcoming + } + + PushFeedItem(video, context) + return invalid + end function + function ParseChannelRenderer(nodeData as object, context as object) as object channelId = nodeData["channelId"] if not IsString(channelId) diff --git a/playlet-lib/src/components/Services/Innertube/protos/params.proto b/playlet-lib/src/components/Services/Innertube/protos/params.proto index 41e30e18..b0e7e13f 100644 --- a/playlet-lib/src/components/Services/Innertube/protos/params.proto +++ b/playlet-lib/src/components/Services/Innertube/protos/params.proto @@ -56,3 +56,8 @@ message SearchFilter { optional Filters filters = 2; } + +// https://github.com/LuanRT/YouTube.js/blob/5f83a7416d5dc3167e6af2767e9b90f4bf89f3dc/protos/misc/params.proto#L229-L231 +message NextParams { + repeated string video_id = 5; +} \ No newline at end of file diff --git a/playlet-lib/src/components/Services/Innertube/protos/params.proto.gen.bs b/playlet-lib/src/components/Services/Innertube/protos/params.proto.gen.bs index c02fec65..5a0bc4ac 100644 --- a/playlet-lib/src/components/Services/Innertube/protos/params.proto.gen.bs +++ b/playlet-lib/src/components/Services/Innertube/protos/params.proto.gen.bs @@ -1,5 +1,5 @@ ' Auto-generated file - do not modify manually -' md5:dfefe3075a9c6332b6072e7c02fdccf8 +' md5:ab48a52d517ca819a9720973f690d0e5 import "pkg:/source/Protobuf/BinaryReader.bs" import "pkg:/source/Protobuf/BinaryWriter.bs" @@ -131,6 +131,23 @@ namespace Protobuf end if end function + function encodeNextParams(message as dynamic, asByteArray = false as boolean) as dynamic + buffer = CreateObject("roByteArray") + writer = new Protobuf.BinaryWriter() + writer.SetBuffer(buffer) + encoder = new Protobuf.Encoder(writer) + if message.DoesExist("video_id") + for each value in message["video_id"] + encoder.EncodeString(5, value) + end for + end if + if asByteArray + return buffer + else + return buffer.ToBase64String().EncodeUriComponent() + end if + end function + end namespace diff --git a/playlet-lib/src/components/Services/Innertube/tests/InnertubeTestNode.xml b/playlet-lib/src/components/Services/Innertube/tests/InnertubeTestNode.xml new file mode 100644 index 00000000..62ca5036 --- /dev/null +++ b/playlet-lib/src/components/Services/Innertube/tests/InnertubeTestNode.xml @@ -0,0 +1,8 @@ + + + + + +