From 32de36c6dc60a1c0776d1437399bd8154a2c2323 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Spa=CC=88th?= Date: Thu, 16 Jan 2025 16:09:57 +0100 Subject: [PATCH 1/3] feat: add watch time update function for VideoInfos classes --- src/core/mixins/MediaInfo.ts | 23 +++++++++++++++++++++++ src/parser/youtube/VideoInfo.ts | 7 +++++++ src/parser/ytmusic/TrackInfo.ts | 7 +++++++ 3 files changed, 37 insertions(+) diff --git a/src/core/mixins/MediaInfo.ts b/src/core/mixins/MediaInfo.ts index d18411c2a..4d8c19f70 100644 --- a/src/core/mixins/MediaInfo.ts +++ b/src/core/mixins/MediaInfo.ts @@ -224,6 +224,29 @@ export default class MediaInfo { }, url_params); } + /** + * Adds video to the watch history. + */ + async updateWatchHistoryTime(client_name = Constants.CLIENTS.WEB.NAME, client_version = Constants.CLIENTS.WEB.VERSION, replacement = 'https://www.', playedSeconds = 0): Promise { + if (!this.#playback_tracking) + throw new InnertubeError('Playback tracking not available'); + + const url_params = { + cpn: this.#cpn, + st: playedSeconds.toFixed(3), + et: playedSeconds.toFixed(3), + cmt: playedSeconds.toFixed(3), + final: '1' + }; + + const url = this.#playback_tracking.videostats_watchtime_url.replace('https://s.', replacement); + + return await this.#actions.stats(url, { + client_name, + client_version + }, url_params); + } + get actions(): Actions { return this.#actions; } diff --git a/src/parser/youtube/VideoInfo.ts b/src/parser/youtube/VideoInfo.ts index 21efafe45..cfa46762a 100644 --- a/src/parser/youtube/VideoInfo.ts +++ b/src/parser/youtube/VideoInfo.ts @@ -175,6 +175,13 @@ export default class VideoInfo extends MediaInfo { return super.addToWatchHistory(); } + /** + * Updates video in the watch history with specific point (after being added to watch history). + */ + async updateWatchTime(playedSeconds = 0): Promise { + return super.updateWatchHistoryTime(undefined, undefined, undefined, playedSeconds); + } + /** * Retrieves watch next feed continuation. */ diff --git a/src/parser/ytmusic/TrackInfo.ts b/src/parser/ytmusic/TrackInfo.ts index b7a22de0b..3dd9c6e78 100644 --- a/src/parser/ytmusic/TrackInfo.ts +++ b/src/parser/ytmusic/TrackInfo.ts @@ -145,6 +145,13 @@ class TrackInfo extends MediaInfo { return super.addToWatchHistory(Constants.CLIENTS.YTMUSIC.NAME, Constants.CLIENTS.YTMUSIC.VERSION, 'https://music.'); } + /** + * Updates video in the watch history with specific point (after being added to watch history). + */ + async updateWatchTime(playedSeconds = 0): Promise { + return super.updateWatchHistoryTime(Constants.CLIENTS.YTMUSIC.NAME, Constants.CLIENTS.YTMUSIC.VERSION, 'https://music.', playedSeconds); + } + get available_tabs(): string[] { return this.tabs ? this.tabs.map((tab) => tab.title) : []; } From 58569dfcb91471e18557ac889018619a1cc6cf4d Mon Sep 17 00:00:00 2001 From: Konstantin Date: Fri, 17 Jan 2025 00:16:05 +0100 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: absidue <48293849+absidue@users.noreply.github.com> --- src/core/mixins/MediaInfo.ts | 2 +- src/parser/youtube/VideoInfo.ts | 4 ++-- src/parser/ytmusic/TrackInfo.ts | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/core/mixins/MediaInfo.ts b/src/core/mixins/MediaInfo.ts index 4d8c19f70..3d9f73afd 100644 --- a/src/core/mixins/MediaInfo.ts +++ b/src/core/mixins/MediaInfo.ts @@ -227,7 +227,7 @@ export default class MediaInfo { /** * Adds video to the watch history. */ - async updateWatchHistoryTime(client_name = Constants.CLIENTS.WEB.NAME, client_version = Constants.CLIENTS.WEB.VERSION, replacement = 'https://www.', playedSeconds = 0): Promise { + async updateWatchHistoryTime(playedSeconds: number, client_name = Constants.CLIENTS.WEB.NAME, client_version = Constants.CLIENTS.WEB.VERSION, replacement = 'https://www.'): Promise { if (!this.#playback_tracking) throw new InnertubeError('Playback tracking not available'); diff --git a/src/parser/youtube/VideoInfo.ts b/src/parser/youtube/VideoInfo.ts index cfa46762a..4f8eb48fa 100644 --- a/src/parser/youtube/VideoInfo.ts +++ b/src/parser/youtube/VideoInfo.ts @@ -178,8 +178,8 @@ export default class VideoInfo extends MediaInfo { /** * Updates video in the watch history with specific point (after being added to watch history). */ - async updateWatchTime(playedSeconds = 0): Promise { - return super.updateWatchHistoryTime(undefined, undefined, undefined, playedSeconds); + async updateWatchTime(playedSeconds: number): Promise { + return super.updateWatchHistoryTime(playedSeconds); } /** diff --git a/src/parser/ytmusic/TrackInfo.ts b/src/parser/ytmusic/TrackInfo.ts index 3dd9c6e78..46ec400c1 100644 --- a/src/parser/ytmusic/TrackInfo.ts +++ b/src/parser/ytmusic/TrackInfo.ts @@ -148,8 +148,8 @@ class TrackInfo extends MediaInfo { /** * Updates video in the watch history with specific point (after being added to watch history). */ - async updateWatchTime(playedSeconds = 0): Promise { - return super.updateWatchHistoryTime(Constants.CLIENTS.YTMUSIC.NAME, Constants.CLIENTS.YTMUSIC.VERSION, 'https://music.', playedSeconds); + async updateWatchTime(playedSeconds: number): Promise { + return super.updateWatchHistoryTime(playedSeconds, Constants.CLIENTS.YTMUSIC.NAME, Constants.CLIENTS.YTMUSIC.VERSION, 'https://music.'); } get available_tabs(): string[] { From 827624a8877215e1879329239e5f10fe69d66ce0 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Tue, 21 Jan 2025 16:23:09 +0100 Subject: [PATCH 3/3] update src/core/mixins/MediaInfo.ts Co-authored-by: absidue <48293849+absidue@users.noreply.github.com> --- src/core/mixins/MediaInfo.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/mixins/MediaInfo.ts b/src/core/mixins/MediaInfo.ts index 3d9f73afd..7441d6066 100644 --- a/src/core/mixins/MediaInfo.ts +++ b/src/core/mixins/MediaInfo.ts @@ -227,7 +227,7 @@ export default class MediaInfo { /** * Adds video to the watch history. */ - async updateWatchHistoryTime(playedSeconds: number, client_name = Constants.CLIENTS.WEB.NAME, client_version = Constants.CLIENTS.WEB.VERSION, replacement = 'https://www.'): Promise { + async updateWatchHistoryTime(playedSeconds: number, client_name: string = Constants.CLIENTS.WEB.NAME, client_version: string = Constants.CLIENTS.WEB.VERSION, replacement = 'https://www.'): Promise { if (!this.#playback_tracking) throw new InnertubeError('Playback tracking not available');