forked from LuanRT/YouTube.js
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNavigationEndpoint.ts
More file actions
153 lines (125 loc) · 5.11 KB
/
NavigationEndpoint.ts
File metadata and controls
153 lines (125 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { YTNode } from '../helpers.js';
import { Parser, type IEndpoint, type RawNode } from '../index.js';
import OpenPopupAction from './actions/OpenPopupAction.js';
import CreatePlaylistDialog from './CreatePlaylistDialog.js';
import CommandExecutorCommand from './commands/CommandExecutorCommand.js';
import type Actions from '../../core/Actions.js';
import type ModalWithTitleAndButton from './ModalWithTitleAndButton.js';
import type { ApiResponse } from '../../core/Actions.js';
import type { IParsedResponse } from '../types/index.js';
export type Metadata = {
url?: string;
api_url?: string;
page_type?: string;
send_post?: boolean;
};
export default class NavigationEndpoint extends YTNode {
static type = 'NavigationEndpoint';
public name?: string;
public payload: any;
public dialog?: CreatePlaylistDialog | YTNode | null;
public modal?: ModalWithTitleAndButton | YTNode | null;
public open_popup?: OpenPopupAction | null;
public next_endpoint?: NavigationEndpoint;
public metadata: Metadata;
public command?: YTNode | YTNode & IEndpoint;
public commands?: NavigationEndpoint[];
constructor(data: RawNode) {
super();
if (data) {
if (data.serialCommand || data.parallelCommand) {
const raw_command = data.serialCommand || data.parallelCommand;
this.commands = raw_command.commands.map((command: RawNode) => new NavigationEndpoint(command));
}
if (data.innertubeCommand || data.command || data.performOnceCommand) {
data = data.innertubeCommand || data.command || data.performOnceCommand;
}
}
this.command = Parser.parseCommand(data);
if (Reflect.has(data || {}, 'openPopupAction'))
this.open_popup = new OpenPopupAction(data.openPopupAction);
this.name = Object.keys(data || {})
.find((item) =>
item.endsWith('Endpoint') ||
item.endsWith('Command')
);
this.payload = this.name ? Reflect.get(data, this.name) : {};
if (Reflect.has(this.payload, 'dialog') || Reflect.has(this.payload, 'content')) {
this.dialog = Parser.parseItem(this.payload.dialog || this.payload.content);
}
if (Reflect.has(this.payload, 'modal')) {
this.modal = Parser.parseItem(this.payload.modal);
}
if (Reflect.has(this.payload, 'nextEndpoint')) {
this.next_endpoint = new NavigationEndpoint(this.payload.nextEndpoint);
}
if (data?.serviceEndpoint) {
data = data.serviceEndpoint;
}
this.metadata = {};
if (data?.commandMetadata?.webCommandMetadata?.url) {
this.metadata.url = data.commandMetadata.webCommandMetadata.url;
}
if (data?.commandMetadata?.webCommandMetadata?.webPageType) {
this.metadata.page_type = data.commandMetadata.webCommandMetadata.webPageType;
}
if (data?.commandMetadata?.webCommandMetadata?.apiUrl) {
this.metadata.api_url = data.commandMetadata.webCommandMetadata.apiUrl.replace('/youtubei/v1/', '');
} else if (this.name) {
this.metadata.api_url = this.getPath(this.name);
}
if (data?.commandMetadata?.webCommandMetadata?.sendPost) {
this.metadata.send_post = data.commandMetadata.webCommandMetadata.sendPost;
}
if (data?.createPlaylistEndpoint) {
if (data?.createPlaylistEndpoint.createPlaylistDialog) {
this.dialog = Parser.parseItem(data?.createPlaylistEndpoint.createPlaylistDialog, CreatePlaylistDialog);
}
}
}
/**
* Sometimes InnerTube does not return an API url, in that case the library should set it based on the name of the payload object.
* @deprecated This should be removed in the future.
*/
getPath(name: string) {
switch (name) {
case 'browseEndpoint':
return '/browse';
case 'watchEndpoint':
case 'reelWatchEndpoint':
return '/player';
case 'searchEndpoint':
return '/search';
case 'watchPlaylistEndpoint':
return '/next';
case 'liveChatItemContextMenuEndpoint':
return '/live_chat/get_item_context_menu';
}
}
call<T extends IParsedResponse>(actions: Actions, args: { [key: string]: any; parse: true }): Promise<T>;
call(actions: Actions, args?: { [key: string]: any; parse?: false }): Promise<ApiResponse>;
call(actions: Actions, args?: { [key: string]: any; parse?: boolean }): Promise<IParsedResponse | ApiResponse> {
if (!actions)
throw new Error('An API caller must be provided');
if (this.command) {
let command = this.command as (YTNode & IEndpoint);
if (command.is(CommandExecutorCommand)) {
command = command.commands.at(-1) as (YTNode & IEndpoint);
}
return actions.execute(command.getApiPath(), { ...command.buildRequest(), ...args });
}
if (!this.metadata.api_url)
throw new Error('Expected an api_url, but none was found.');
return actions.execute(this.metadata.api_url, { ...this.payload, ...args });
}
toURL(): string | undefined {
if (!this.metadata.url)
return undefined;
if (!this.metadata.page_type)
return undefined;
return (
this.metadata.page_type === 'WEB_PAGE_TYPE_UNKNOWN' ?
this.metadata.url : `https://www.youtube.com${this.metadata.url}`
);
}
}