Files
YouTube.js/lib/parser/contents/classes/NavigationEndpoint.js
Daniel Wykerd 4a102878d8 refactor!: feature complete contents parser
* feat: allow setting search params to custom value

This is useful for getting results other than videos, like playlists and
channels.

* feat: add initial parsers for common renderers

* feat: artist search renderers

Added common renderers used when searching artists

* refactor: snake_case

* feat: channel home page renderers

* feat: parsers for more channel tabs

These are needed for channel tabs: Videos, Playlists, Community,
Channels

Additionally, do not merely return text as string, since they may
include links which may be navigated to

* feat: channel full metadata

* feat: renderers for playlists

* refactor!: Actions.browse

Channels should be viewable when not logged in, also added 'navigation'
type for use in NagivationEndpoint in the future.

* feat: home feed parsers

* feat: watch page renderers

* feat: start implementing HomeFeed API

The HomeFeed class remains compatible with the existing API

* feat: generate types using tsc and jsdoc

* feat: browse continuations from navigationEndpoint

* fix: Actions moved to session

This follows commit 1bfe2676d8

* fix: add more typescript config

* chore: use correct spaces and quotes

* feat: Trending API

* feat: reimplement existing channel API

* feat: add base video feed class

* feat: get channel videos

* feat: channel playlists

* feat: get channel community posts

* feat: get channels from channel

* feat: get channel about page data

* feat: add missing channel parsers

this commit also adds regenerated types I've neglected to push

* feat: initial playlist reimplementation

* feat: complete playlist reimplementation

* refactor: change InnertubeError to ES6 class

* fix: some unresolved types

* chore: update types

* feat: wip video details

* feat: get music tracks in video

Possibly an implementation for issue #48

* refactor:  merge parsers (wip)

This is a work in progress.

* fix: add pnpm to ignore

* fix: merge issues

* fix: merge Video and VideoInfo

VideoInfo should be working again.
Also remove the old parsers.

* feat: set matching in Simplify

Still looking into removing Simplify

* fix: ContinuationItem

This `call` method allows for traversal of continuations with the Simplify API
but may be removed in the future

* fix: optionally returned data

* revert: replace ContinuationItem with main

* feat(parser): contents memoization by classname

* feat(channel): working without Simplify

* feat(feed): working continuations

* fix: liniting issues

* feat(feed): filterable feed for home

* feat(feed): tabbed feed for trending & channel

* refactor: remove Simplify completely

* chore: lint

* refactor: alias `items` with `contents`

* refactor: `Search` to extend `Feed`

* fix: Search working

Also added MenuServiceItemDownload

* refactor: move `Channel` and `Playlist`

* fix: pass all tests

* fix: linting errors
2022-06-15 18:31:34 -03:00

184 lines
5.6 KiB
JavaScript

'use strict';
const Parser = require('..');
class NavigationEndpoint {
type = 'NavigationEndpoint';
constructor(data) {
data?.serviceEndpoint &&
(data = data.serviceEndpoint);
this.metadata = {};
data?.commandMetadata?.webCommandMetadata?.url &&
(this.metadata.url = data.commandMetadata.webCommandMetadata.url);
data?.commandMetadata?.webCommandMetadata?.webPageType &&
(this.metadata.page_type = data.commandMetadata.webCommandMetadata.webPageType);
data?.commandMetadata?.webCommandMetadata?.apiUrl &&
(this.metadata.api_url = data.commandMetadata.webCommandMetadata.apiUrl.replace('/youtubei/v1/', ''));
data?.commandMetadata?.webCommandMetadata?.sendPost &&
(this.metadata.send_post = data.commandMetadata.webCommandMetadata.sendPost);
if (data?.browseEndpoint) {
const configs = data?.browseEndpoint?.browseEndpointContextSupportedConfigs?.browseEndpointContextMusicConfig;
this.browse = {
id: data?.browseEndpoint?.browseId || null,
params: data?.browseEndpoint.params || null,
base_url: data?.browseEndpoint?.canonicalBaseUrl || null,
page_type: configs?.pageType || null
};
}
if (data?.watchEndpoint) {
const configs = data?.watchEndpoint?.watchEndpointMusicSupportedConfigs?.watchEndpointMusicConfig;
this.watch = {
video_id: data?.watchEndpoint?.videoId,
playlist_id: data?.watchEndpoint.playlistId || null,
params: data?.watchEndpoint.params || null,
index: data?.watchEndpoint.index || null,
supported_onesie_config: data?.watchEndpoint?.watchEndpointSupportedOnesieConfig,
music_video_type: configs?.musicVideoType || null
};
}
if (data?.searchEndpoint) {
this.search = {
query: data.searchEndpoint.query,
params: data.searchEndpoint.params
}
}
if (data?.subscribeEndpoint) {
this.subscribe = {
channel_ids: data.subscribeEndpoint.channelIds,
params: data.subscribeEndpoint.params
}
}
if (data?.unsubscribeEndpoint) {
this.unsubscribe = {
channel_ids: data.unsubscribeEndpoint.channelIds,
params: data.unsubscribeEndpoint.params
}
}
if (data?.likeEndpoint) {
this.like = {
status: data.likeEndpoint.status,
target: {
video_id: data.likeEndpoint.target.videoId,
playlist_id: data.likeEndpoint.target.playlistId
},
params:
data.likeEndpoint?.removeLikeParams ||
data.likeEndpoint?.likeParams ||
data.likeEndpoint?.dislikeParams
}
}
if (data?.offlineVideoEndpoint) {
this.offline_video = {
video_id: data.offlineVideoEndpoint.videoId,
on_add_command: {
get_download_action: {
video_id: data.offlineVideoEndpoint.videoId,
params: data.offlineVideoEndpoint.onAddCommand.getDownloadActionCommand.params,
}
}
}
}
if (data?.continuationCommand) {
this.continuation = {
request: data?.continuationCommand?.request || null,
token: data?.continuationCommand?.token || null
};
}
if (data?.feedbackEndpoint) {
this.feedback = {
token: data.feedbackEndpoint.feedbackToken
}
}
if (data?.watchPlaylistEndpoint) {
this.watch_playlist = {
playlist_id: data.watchPlaylistEndpoint?.playlistId
}
}
if (data?.playlistEditEndpoint) {
this.playlist_edit = {
playlist_id: data.playlistEditEndpoint.playlistId,
actions: data.playlistEditEndpoint.actions.map(() => ({
action: data.action,
removed_video_id: data.removedVideoId
}))
}
}
if (data?.addToPlaylistServiceEndpoint) {
this.add_to_playlist = {
video_id: data.addToPlaylistServiceEndpoint.videoId
}
}
if (data?.getReportFormEndpoint) {
this.get_report_form = {
params: data.getReportFormEndpoint.params
}
}
}
async call(actions, client) {
if (!actions)
throw new Error('An active caller must be provided');
if (this.continuation) {
switch (this.continuation.request) {
case 'CONTINUATION_REQUEST_TYPE_BROWSE': {
const response = await actions.browse(this.continuation.token, { is_ctoken: true });
return Parser.parseResponse(response.data);
}
case 'CONTINUATION_REQUEST_TYPE_SEARCH': {
const response = await actions.search({ ctoken: this.continuation.token });
return Parser.parseResponse(response.data);
}
case 'CONTINUATION_REQUEST_TYPE_WATCH_NEXT': {
const response = await actions.next({ ctoken: this.continuation.token });
return Parser.parseResponse(response.data);
}
default:
throw new Error(this.continuation.request + ' not implemented');
}
}
if (this.search) {
const response = await actions.search({ query: this.search.query, params: this.search.params, client });
return Parser.parseResponse(response.data);
}
if (this.browse) {
const args = { client };
this.browse.params &&
(args.params = this.browse.params);
const response = await actions.browse(this.browse.id, args);
return Parser.parseResponse(response.data);
}
if (this.like) {
const response = await actions.engage(this.metadata.api_url, { video_id: this.like.target.video_id, params: this.like.params });
return response;
}
}
}
module.exports = NavigationEndpoint;