feat(MediaInfo): Add updateWatchTime (#874)

* feat: add watch time update function for VideoInfos classes

* chore: Improve naming

* chore: fix ts error

---------

Co-authored-by: absidue <48293849+absidue@users.noreply.github.com>
Co-authored-by: Luan <luan.lrt4@gmail.com>
This commit is contained in:
Konstantin
2025-06-08 13:25:54 +02:00
committed by GitHub
parent bb022e8285
commit 065e9a4e7c
3 changed files with 37 additions and 6 deletions

View File

@@ -139,7 +139,7 @@ export default class MediaInfo {
manifest_options
);
}
/**
* Get a cleaned up representation of the adaptive_formats
*/
@@ -207,10 +207,7 @@ export default class MediaInfo {
return new TranscriptInfo(this.actions, response);
}
/**
* Adds video to the watch history.
*/
async addToWatchHistory(client_name: string = Constants.CLIENTS.WEB.NAME, client_version: string = Constants.CLIENTS.WEB.VERSION, replacement = 'https://www.'): Promise<Response> {
async addToWatchHistory(client_name?: string, client_version?: string, replacement = 'https://www.'): Promise<Response> {
if (!this.#playback_tracking)
throw new InnertubeError('Playback tracking not available');
@@ -223,6 +220,26 @@ export default class MediaInfo {
const url = this.#playback_tracking.videostats_playback_url.replace('https://s.', replacement);
return await this.#actions.stats(url, {
client_name: client_name || Constants.CLIENTS.WEB.NAME,
client_version: client_version || Constants.CLIENTS.WEB.VERSION
}, url_params);
}
async updateWatchTime(startTime: number, client_name: string = Constants.CLIENTS.WEB.NAME, client_version: string = Constants.CLIENTS.WEB.VERSION, replacement = 'https://www.'): Promise<Response> {
if (!this.#playback_tracking)
throw new InnertubeError('Playback tracking not available');
const url_params = {
cpn: this.#cpn,
st: startTime.toFixed(3),
et: startTime.toFixed(3),
cmt: startTime.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
@@ -243,7 +260,7 @@ export default class MediaInfo {
/**
* Parsed InnerTube response.
*/
get page(): [ IPlayerResponse, INextResponse? ] {
get page(): [IPlayerResponse, INextResponse?] {
return this.#page;
}
}

View File

@@ -192,6 +192,13 @@ export default class VideoInfo extends MediaInfo {
return super.addToWatchHistory();
}
/**
* Updates watch time for the video.
*/
async updateWatchTime(startTime: number): Promise<Response> {
return super.updateWatchTime(startTime);
}
/**
* Retrieves watch next feed continuation.
*/

View File

@@ -144,6 +144,13 @@ class TrackInfo extends MediaInfo {
async addToWatchHistory(): Promise<Response> {
return super.addToWatchHistory(Constants.CLIENTS.YTMUSIC.NAME, Constants.CLIENTS.YTMUSIC.VERSION, 'https://music.');
}
/**
* Updates the watch time of the song.
*/
async updateWatchTime(startTime: number): Promise<Response> {
return super.updateWatchTime(startTime, Constants.CLIENTS.YTMUSIC.NAME, Constants.CLIENTS.YTMUSIC.VERSION, 'https://music.');
}
get available_tabs(): string[] {
return this.tabs ? this.tabs.map((tab) => tab.title) : [];