diff --git a/CHANGELOG.md b/CHANGELOG.md index e29800ed..cf1f6d9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ 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.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] +### Added +- Add watched videos to Invidious watch history + ## [0.8.0] - 2023-03-02 ### Added - SponsorBlock notification diff --git a/playlet-lib/src/components/VideoPlayer/MarkVideoWatchedTask.bs b/playlet-lib/src/components/VideoPlayer/MarkVideoWatchedTask.bs new file mode 100644 index 00000000..21cdfa73 --- /dev/null +++ b/playlet-lib/src/components/VideoPlayer/MarkVideoWatchedTask.bs @@ -0,0 +1,11 @@ +import "pkg:/source/services/Invidious.bs" + +@asynctask +function MarkVideoWatchedTask(input as object) as object + videoId = input.videoId + + success = Invidious.MarkVideoWatched(videoId) + return { + success: success + } +end function diff --git a/playlet-lib/src/components/VideoPlayer/Video.bs b/playlet-lib/src/components/VideoPlayer/Video.bs index fff546ef..fceed8fc 100644 --- a/playlet-lib/src/components/VideoPlayer/Video.bs +++ b/playlet-lib/src/components/VideoPlayer/Video.bs @@ -1,6 +1,8 @@ +import "pkg:/source/asyncTask/asyncTask.bs" import "pkg:/source/services/Invidious.bs" import "pkg:/components/Dialog/DialogUtils.bs" import "pkg:/components/SponsorBlockNotification/SponsorBlockNotificationManager.bs" +import "pkg:/components/VideoPlayer/MarkVideoWatchedTask.bs" function PlayVideo(videoId as string, args = invalid as object) sender = args?.sender @@ -35,6 +37,8 @@ function PlayVideo(videoId as string, args = invalid as object) if videoContainer.fullscreen videoContainer.videoPlayer.SetFocus(true) end if + + StartAsyncTask(MarkVideoWatchedTask, { videoId: videoId }) end function function CloseVideo(setFocus = true as boolean) diff --git a/playlet-lib/src/source/services/Invidious.bs b/playlet-lib/src/source/services/Invidious.bs index d34637a3..d72bd16f 100644 --- a/playlet-lib/src/source/services/Invidious.bs +++ b/playlet-lib/src/source/services/Invidious.bs @@ -13,11 +13,12 @@ namespace Invidious const STATS_ENDPOINT = "/api/v1/stats" const VIDEOS_ENDPOINT = "/api/v1/videos" + const HISTORY_ENDPOINT = "/api/v1/auth/history" const SEARCH_SUGGEST_ENDPOINT = "/api/v1/search/suggestions" const SEARCH_ENDPOINT = "/api/v1/search" - const AUTH_SCOPE = "GET:feed,GET:playlists,POST:tokens/unregister" + const AUTH_SCOPE = "GET:feed,GET:playlists,POST:tokens/unregister,POST:history*" const AUTH_FEED = "/api/v1/auth/feed" const AUTH_PLAYLISTS = "/api/v1/auth/playlists" const AUTH_TOKENS_UNREGISTER = "/api/v1/auth/tokens/unregister" @@ -66,6 +67,23 @@ namespace Invidious } end function + function MarkVideoWatched(videoId as string) as boolean + authToken = InvidiousSettings.GetAuthToken() + if authToken = invalid + return false + end if + + url = `${authToken.instance}${Invidious.HISTORY_ENDPOINT}/${videoId}` + headers = Invidious.GetAuthenticationHeaders(authToken.token) + args = { + headers: headers, + parseJson: false + } + + response = Requests().post(url, args) + return response.ok + end function + function SearchSuggestions(q as string) as object 'SearchSuggestionResult instance = InvidiousSettings.GetSelectedInstance() url = `${instance}${Invidious.SEARCH_SUGGEST_ENDPOINT}` @@ -127,7 +145,8 @@ namespace Invidious expire = TimeUtils.Now().AsSeconds() + 60 * 60 * 24 * 365 * 2 '2 years from now callbackUrl = `http://${localIp}:${webServerPort}${Invidious.TOKEN_CALLBACK}?ref=${instance}`.EncodeUriComponent() - return `${invidiousHost}/authorize_token?scopes=${Invidious.AUTH_SCOPE}&callback_url=${callbackUrl}&expire=${expire}` + scope = `${Invidious.AUTH_SCOPE}`.EncodeUriComponent() + return `${invidiousHost}/authorize_token?scopes=${scope}&callback_url=${callbackUrl}&expire=${expire}` end function function GetAuthenticationHeaders(token as string) as object