From 8b123b18f5c8743969f256d224d03ba8d178bd3c Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Mon, 19 May 2025 17:35:35 -0400 Subject: [PATCH] Watching progress (#609) * Watching progress * watch time endpoint * Lint fix * Changelog * options mandatory * tweak * tweak --------- Co-authored-by: github-action linter --- CHANGELOG.md | 5 + .../ContentNode/VideoContentNode.xml | 1 + .../Services/Innertube/InnertubeService.bs | 94 ++++++++++---- .../Services/Innertube/NodesParser.bs | 6 + .../Services/Invidious/InvidiousService.bs | 3 +- .../Invidious/InvidiousToContentNode.bs | 1 + .../VideoFeed/VideoRowCell/VideoRowCell.bs | 12 ++ .../VideoFeed/VideoRowCell/VideoRowCell.xml | 9 ++ .../VideoPlayer/MarkVideoWatchedTask.bs | 66 +++++++++- .../components/VideoPlayer/SponsorBlock.bs | 1 - .../src/components/VideoPlayer/VideoPlayer.bs | 118 ++++++++++++++---- .../PlayletInvidiousBackendRouter.bs | 47 +------ .../src/source/utils/MessagePortUtils.bs | 2 +- tools/bs-plugins/type-gen-plugin.ts | 3 +- 14 files changed, 273 insertions(+), 95 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac902a66..facff970 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Watch progress for YouTube accounts: Progress is saved as you watch. Progress bar is shown on video thumbnails (only on user feeds, such as recommendations, subscriptions, and watch history) + - Known bug: Watch progress doesn't update for all videos. + ### Fixed - A few issues related to Playlet backend feeds and caching diff --git a/playlet-lib/src/components/ContentNode/VideoContentNode.xml b/playlet-lib/src/components/ContentNode/VideoContentNode.xml index d891e9de..07c1c2be 100644 --- a/playlet-lib/src/components/ContentNode/VideoContentNode.xml +++ b/playlet-lib/src/components/ContentNode/VideoContentNode.xml @@ -23,5 +23,6 @@ + \ No newline at end of file diff --git a/playlet-lib/src/components/Services/Innertube/InnertubeService.bs b/playlet-lib/src/components/Services/Innertube/InnertubeService.bs index 69e5fb9d..8bc9e97a 100644 --- a/playlet-lib/src/components/Services/Innertube/InnertubeService.bs +++ b/playlet-lib/src/components/Services/Innertube/InnertubeService.bs @@ -326,7 +326,7 @@ namespace InnertubeService return result end function - function AuthMarkVideoWatched(videoId as string, options as object) as boolean + function AuthGetPlaybackTrackingUrls(videoId as string, options as object) as object cancellation = invalid accessToken = options.accessToken @@ -340,43 +340,95 @@ namespace InnertubeService playerResponse = playerRequest.Await() if not playerResponse.IsSuccess() - LogError("Error marking video as watched:", playerResponse.ErrorMessage()) - return false - end if - - if CancellationUtils.IsCancelled(cancellation) - return false + return { + error: playerResponse.ErrorMessage() + } end if json = playerResponse.Json() + videostatsPlaybackUrl = ObjectUtils.Dig(json, ["playbackTracking", "videostatsPlaybackUrl", "baseUrl"]) - if StringUtils.IsNullOrEmpty(videostatsPlaybackUrl) - LogError("No videostatsPlaybackUrl found in response") - return false + videostatsWatchtimeUrl = ObjectUtils.Dig(json, ["playbackTracking", "videostatsWatchtimeUrl", "baseUrl"]) + + return { + videostatsPlaybackUrl: videostatsPlaybackUrl + videostatsWatchtimeUrl: videostatsWatchtimeUrl + } + end function + + function AuthMarkVideoWatched(videostatsPlaybackUrl as string, options as object) as boolean + cancellation = invalid + accessToken = options.accessToken + + if options.DoesExist("cancellation") + cancellation = options.cancellation end if - videostatsPlaybackUrl = videostatsPlaybackUrl.replace("https://s.youtube.com", "https://www.youtube.com") + videostatsPlaybackUrl = videostatsPlaybackUrl.replace("https://s.", "https://www.") queryParams = UrlUtils.ParseQueryComponents(videostatsPlaybackUrl) queryParams["c"] = "TVHTML5" queryParams["cbrver"] = INNERTUBE_TV_CLIENT_VERSION queryParams["cver"] = INNERTUBE_TV_CLIENT_VERSION - queryParams["cpn"] = InnertubeService.CreateClientPlaybackNonce() - queryParams["fmt"] = "251" - queryParams["rtn"] = "0" - queryParams["rt"] = "0" queryParams["ver"] = "2" + queryParams["cpn"] = InnertubeService.CreateClientPlaybackNonce() + queryParams["cmt"] = 0 + queryParams["final"] = "1" videostatsPlaybackUrl = UrlUtils.SetQueryParams(videostatsPlaybackUrl, queryParams) - trackingRequest = HttpClient.Get(videostatsPlaybackUrl) - trackingRequest.Header("Authorization", "Bearer " + accessToken) + request = HttpClient.Get(videostatsPlaybackUrl) + request.Cancellation(cancellation) + request.NoCache() + request.Headers(Innertube.CreateHeaders(Innertube.ClientType.Tv)) + request.Header("Authorization", "Bearer " + accessToken) - trackingResponse = trackingRequest.Await() - if not trackingResponse.IsSuccess() - LogError("Error marking video as watched:", trackingResponse.ErrorMessage()) + response = request.Await() + if not response.IsSuccess() + LogError("Error marking video as watched:", response.ErrorMessage()) return false end if - LogDebug("Marked video as watched:", videoId) + LogDebug("Marked video as watched") + return true + end function + + function AuthMarkVideoWatchTime(time as float, videostatsWatchtimeUrl as string, options = invalid as object) as boolean + cancellation = invalid + accessToken = options.accessToken + + if options <> invalid + if options.DoesExist("cancellation") + cancellation = options.cancellation + end if + end if + + timeStr = time.ToStr("%.3f") + + videostatsWatchtimeUrl = videostatsWatchtimeUrl.replace("https://s.", "https://www.") + queryParams = UrlUtils.ParseQueryComponents(videostatsWatchtimeUrl) + queryParams["c"] = "TVHTML5" + queryParams["cbrver"] = INNERTUBE_TV_CLIENT_VERSION + queryParams["cver"] = INNERTUBE_TV_CLIENT_VERSION + queryParams["ver"] = "2" + queryParams["cpn"] = InnertubeService.CreateClientPlaybackNonce() + queryParams["cmt"] = 0 + queryParams["final"] = "1" + queryParams["st"] = timeStr + queryParams["et"] = timeStr + videostatsWatchtimeUrl = UrlUtils.SetQueryParams(videostatsWatchtimeUrl, queryParams) + + request = HttpClient.Get(videostatsWatchtimeUrl) + request.Cancellation(cancellation) + request.NoCache() + request.Headers(Innertube.CreateHeaders(Innertube.ClientType.Tv)) + request.Header("Authorization", "Bearer " + accessToken) + + response = request.Await() + if not response.IsSuccess() + LogError("Error marking video watch time:", response.ErrorMessage()) + return false + end if + + LogDebug("Marked video watch time:", timeStr) return true end function diff --git a/playlet-lib/src/components/Services/Innertube/NodesParser.bs b/playlet-lib/src/components/Services/Innertube/NodesParser.bs index 2da62d63..04ddecf9 100644 --- a/playlet-lib/src/components/Services/Innertube/NodesParser.bs +++ b/playlet-lib/src/components/Services/Innertube/NodesParser.bs @@ -970,6 +970,7 @@ namespace Innertube lengthText = "" isUpcoming = false liveNow = false + percentDurationWatched = 0 thumbnailOverlays = ObjectUtils.Dig(nodeData, ["header", "tileHeaderRenderer", "thumbnailOverlays"]) if IsArray(thumbnailOverlays) for each overlay in thumbnailOverlays @@ -977,6 +978,10 @@ namespace Innertube if maybeLengthText <> "" lengthText = maybeLengthText end if + maybePercentDurationWatched = ObjectUtils.Dig(overlay, ["thumbnailOverlayResumePlaybackRenderer", "percentDurationWatched"]) + if maybePercentDurationWatched <> invalid + percentDurationWatched = maybePercentDurationWatched + end if liveNow = liveNow or (ValidString(ObjectUtils.Dig(overlay, ["thumbnailOverlayTimeStatusRenderer", "style"])) = "LIVE") isUpcoming = isUpcoming or (ValidString(ObjectUtils.Dig(overlay, ["thumbnailOverlayTimeStatusRenderer", "style"])) = "UPCOMING") end for @@ -1005,6 +1010,7 @@ namespace Innertube "liveNow": liveNow "isUpcoming": isUpcoming "lines": renderedLines + "percentDurationWatched": percentDurationWatched } PushFeedItem(video, context) diff --git a/playlet-lib/src/components/Services/Invidious/InvidiousService.bs b/playlet-lib/src/components/Services/Invidious/InvidiousService.bs index 30f727a6..3d77dac7 100644 --- a/playlet-lib/src/components/Services/Invidious/InvidiousService.bs +++ b/playlet-lib/src/components/Services/Invidious/InvidiousService.bs @@ -85,7 +85,7 @@ namespace Invidious return instance end function - function MarkVideoWatched(videoId as dynamic) as void + function MarkVideoWatched(videoId as dynamic, cancellation = invalid as object) as void if StringUtils.IsNullOrEmpty(videoId) return end if @@ -98,6 +98,7 @@ namespace Invidious url = `${authToken.instance}${Invidious.HISTORY_ENDPOINT}/${videoId}` request = HttpClient.Post(url, "") request.Headers(m.GetAuthenticationHeaders(authToken.token)) + request.Cancellation(cancellation) request.Await() end function diff --git a/playlet-lib/src/components/Services/Invidious/InvidiousToContentNode.bs b/playlet-lib/src/components/Services/Invidious/InvidiousToContentNode.bs index 03d2a37f..a1f6f2af 100644 --- a/playlet-lib/src/components/Services/Invidious/InvidiousToContentNode.bs +++ b/playlet-lib/src/components/Services/Invidious/InvidiousToContentNode.bs @@ -52,6 +52,7 @@ namespace InvidiousContent node.viewCountText = VideoGetViewCountText(item) SetIfExists(node, "index", item, "index") SetIfExists(node, "lines", item, "lines") + SetIfExists(node, "percentDurationWatched", item, "percentDurationWatched") return node end function diff --git a/playlet-lib/src/components/VideoFeed/VideoRowCell/VideoRowCell.bs b/playlet-lib/src/components/VideoFeed/VideoRowCell/VideoRowCell.bs index f7b8ec4c..b05b8b21 100644 --- a/playlet-lib/src/components/VideoFeed/VideoRowCell/VideoRowCell.bs +++ b/playlet-lib/src/components/VideoFeed/VideoRowCell/VideoRowCell.bs @@ -1,4 +1,5 @@ import "pkg:/source/utils/Locale.bs" +import "pkg:/source/utils/MathUtils.bs" import "pkg:/source/utils/StringUtils.bs" import "pkg:/source/utils/TimeUtils.bs" import "pkg:/source/utils/Types.bs" @@ -8,6 +9,7 @@ function Init() m.durationLabel = m.top.FindNode("durationLabel") m.smallestSystemFont = m.top.FindNode("smallestSystemFont") m.smallestBoldSystemFont = m.top.FindNode("smallestBoldSystemFont") + m.progressRect = m.top.FindNode("progressRect") end function function OnContentSet() as void @@ -53,6 +55,8 @@ function OnContentSet() as void m.top.thumbnailUri = content.thumbnail + m.top.percentDurationWatched = content.percentDurationWatched + if not StringUtils.IsNullOrEmpty(content.lengthText) SetDurationLabel(content.lengthText, "#161616FF", 0.8, m.smallestSystemFont) m.top.durationRectVisible = true @@ -90,3 +94,11 @@ function SetDurationLabel(text as string, bgColor as string, bgOpacity as float, rect.opacity = bgOpacity end function +function OnPercentDurationWatched() as void + if m.progressRect = invalid + return + end if + + percent = MathUtils.Clamp(m.top.percentDurationWatched, 0, 100) / 100.0 + m.progressRect.scale = [percent, 1] +end function diff --git a/playlet-lib/src/components/VideoFeed/VideoRowCell/VideoRowCell.xml b/playlet-lib/src/components/VideoFeed/VideoRowCell/VideoRowCell.xml index d4cd2671..26de15a5 100644 --- a/playlet-lib/src/components/VideoFeed/VideoRowCell/VideoRowCell.xml +++ b/playlet-lib/src/components/VideoFeed/VideoRowCell/VideoRowCell.xml @@ -8,6 +8,7 @@ + @@ -36,6 +37,14 @@ font="font:SmallestSystemFont" translation="[8, 0]" /> + +