@ziplayer/plugin 0.0.4 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/SoundCloudPlugin.d.ts +149 -0
- package/dist/SoundCloudPlugin.d.ts.map +1 -1
- package/dist/SoundCloudPlugin.js +149 -0
- package/dist/SoundCloudPlugin.js.map +1 -1
- package/dist/SpotifyPlugin.d.ts +127 -5
- package/dist/SpotifyPlugin.d.ts.map +1 -1
- package/dist/SpotifyPlugin.js +127 -5
- package/dist/SpotifyPlugin.js.map +1 -1
- package/dist/TTSPlugin.d.ts +123 -2
- package/dist/TTSPlugin.d.ts.map +1 -1
- package/dist/TTSPlugin.js +114 -0
- package/dist/TTSPlugin.js.map +1 -1
- package/dist/YouTubePlugin.d.ts +148 -0
- package/dist/YouTubePlugin.d.ts.map +1 -1
- package/dist/YouTubePlugin.js +148 -2
- package/dist/YouTubePlugin.js.map +1 -1
- package/dist/index.d.ts +78 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +78 -0
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/SoundCloudPlugin.ts +149 -0
- package/src/SpotifyPlugin.ts +312 -190
- package/src/TTSPlugin.ts +130 -3
- package/src/YouTubePlugin.ts +528 -382
- package/src/index.ts +82 -0
package/src/YouTubePlugin.ts
CHANGED
|
@@ -1,382 +1,528 @@
|
|
|
1
|
-
import { BasePlugin, Track, SearchResult, StreamInfo } from "ziplayer";
|
|
2
|
-
|
|
3
|
-
import { Innertube, Log } from "youtubei.js";
|
|
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
|
-
raw?.
|
|
75
|
-
raw?.
|
|
76
|
-
raw?.
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
raw?.
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
raw?.
|
|
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
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
const
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
} catch {
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
1
|
+
import { BasePlugin, Track, SearchResult, StreamInfo } from "ziplayer";
|
|
2
|
+
|
|
3
|
+
import { Innertube, Log } from "youtubei.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A plugin for handling YouTube audio content including videos, playlists, and search functionality.
|
|
7
|
+
*
|
|
8
|
+
* This plugin provides comprehensive support for:
|
|
9
|
+
* - YouTube video URLs (youtube.com, youtu.be, music.youtube.com)
|
|
10
|
+
* - YouTube playlist URLs and dynamic mixes
|
|
11
|
+
* - YouTube search queries
|
|
12
|
+
* - Audio stream extraction from YouTube videos
|
|
13
|
+
* - Related track recommendations
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* const youtubePlugin = new YouTubePlugin();
|
|
17
|
+
*
|
|
18
|
+
* // Add to PlayerManager
|
|
19
|
+
* const manager = new PlayerManager({
|
|
20
|
+
* plugins: [youtubePlugin]
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* // Search for videos
|
|
24
|
+
* const result = await youtubePlugin.search("Never Gonna Give You Up", "user123");
|
|
25
|
+
*
|
|
26
|
+
* // Get audio stream
|
|
27
|
+
* const stream = await youtubePlugin.getStream(result.tracks[0]);
|
|
28
|
+
*
|
|
29
|
+
* @since 1.0.0
|
|
30
|
+
*/
|
|
31
|
+
export class YouTubePlugin extends BasePlugin {
|
|
32
|
+
name = "youtube";
|
|
33
|
+
version = "1.0.0";
|
|
34
|
+
|
|
35
|
+
private client!: Innertube;
|
|
36
|
+
private searchClient!: Innertube;
|
|
37
|
+
private ready: Promise<void>;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Creates a new YouTubePlugin instance.
|
|
41
|
+
*
|
|
42
|
+
* The plugin will automatically initialize YouTube clients for both video playback
|
|
43
|
+
* and search functionality. Initialization is asynchronous and handled internally.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* const plugin = new YouTubePlugin();
|
|
47
|
+
* // Plugin is ready to use after initialization completes
|
|
48
|
+
*/
|
|
49
|
+
constructor() {
|
|
50
|
+
super();
|
|
51
|
+
this.ready = this.init();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private async init(): Promise<void> {
|
|
55
|
+
this.client = await Innertube.create({
|
|
56
|
+
client_type: "ANDROID",
|
|
57
|
+
retrieve_player: false,
|
|
58
|
+
} as any);
|
|
59
|
+
|
|
60
|
+
// Use a separate web client for search to avoid mobile parser issues
|
|
61
|
+
this.searchClient = await Innertube.create({
|
|
62
|
+
client_type: "WEB",
|
|
63
|
+
retrieve_player: false,
|
|
64
|
+
} as any);
|
|
65
|
+
Log.setLevel(0);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Build a Track from various YouTube object shapes (search item, playlist item, watch_next feed, basic_info, info)
|
|
69
|
+
private buildTrack(raw: any, requestedBy: string, extra?: { playlist?: string }): Track {
|
|
70
|
+
const pickFirst = (...vals: any[]) => vals.find((v) => v !== undefined && v !== null && v !== "");
|
|
71
|
+
|
|
72
|
+
// Try to resolve from multiple common shapes
|
|
73
|
+
const id = pickFirst(
|
|
74
|
+
raw?.id,
|
|
75
|
+
raw?.video_id,
|
|
76
|
+
raw?.videoId,
|
|
77
|
+
raw?.content_id,
|
|
78
|
+
raw?.identifier,
|
|
79
|
+
raw?.basic_info?.id,
|
|
80
|
+
raw?.basic_info?.video_id,
|
|
81
|
+
raw?.basic_info?.videoId,
|
|
82
|
+
raw?.basic_info?.content_id,
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
const title = pickFirst(
|
|
86
|
+
raw?.metadata?.title?.text,
|
|
87
|
+
raw?.title?.text,
|
|
88
|
+
raw?.title,
|
|
89
|
+
raw?.headline,
|
|
90
|
+
raw?.basic_info?.title,
|
|
91
|
+
"Unknown title",
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
const durationValue = pickFirst(
|
|
95
|
+
raw?.length_seconds,
|
|
96
|
+
raw?.duration?.seconds,
|
|
97
|
+
raw?.duration?.text,
|
|
98
|
+
raw?.duration,
|
|
99
|
+
raw?.length_text,
|
|
100
|
+
raw?.basic_info?.duration,
|
|
101
|
+
);
|
|
102
|
+
const duration = Number(toSeconds(durationValue)) || 0;
|
|
103
|
+
|
|
104
|
+
const thumb = pickFirst(
|
|
105
|
+
raw?.thumbnails?.[0]?.url,
|
|
106
|
+
raw?.thumbnail?.[0]?.url,
|
|
107
|
+
raw?.thumbnail?.url,
|
|
108
|
+
raw?.thumbnail?.thumbnails?.[0]?.url,
|
|
109
|
+
raw?.content_image?.image?.[0]?.url,
|
|
110
|
+
raw?.basic_info?.thumbnail?.[0]?.url,
|
|
111
|
+
raw?.basic_info?.thumbnail?.[raw?.basic_info?.thumbnail?.length - 1]?.url,
|
|
112
|
+
raw?.thumbnails?.[raw?.thumbnails?.length - 1]?.url,
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
const author = pickFirst(raw?.author?.name, raw?.author, raw?.channel?.name, raw?.owner?.name, raw?.basic_info?.author);
|
|
116
|
+
|
|
117
|
+
const views = pickFirst(
|
|
118
|
+
raw?.view_count,
|
|
119
|
+
raw?.views,
|
|
120
|
+
raw?.short_view_count,
|
|
121
|
+
raw?.stats?.view_count,
|
|
122
|
+
raw?.basic_info?.view_count,
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
const url = pickFirst(raw?.url, id ? `https://www.youtube.com/watch?v=${id}` : undefined);
|
|
126
|
+
|
|
127
|
+
return {
|
|
128
|
+
id: String(id),
|
|
129
|
+
title: String(title),
|
|
130
|
+
url: String(url),
|
|
131
|
+
duration,
|
|
132
|
+
thumbnail: thumb,
|
|
133
|
+
requestedBy,
|
|
134
|
+
source: this.name,
|
|
135
|
+
metadata: {
|
|
136
|
+
author,
|
|
137
|
+
views,
|
|
138
|
+
...(extra?.playlist ? { playlist: extra.playlist } : {}),
|
|
139
|
+
},
|
|
140
|
+
} as Track;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Determines if this plugin can handle the given query.
|
|
145
|
+
*
|
|
146
|
+
* @param query - The search query or URL to check
|
|
147
|
+
* @returns `true` if the plugin can handle the query, `false` otherwise
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* plugin.canHandle("https://www.youtube.com/watch?v=dQw4w9WgXcQ"); // true
|
|
151
|
+
* plugin.canHandle("Never Gonna Give You Up"); // true
|
|
152
|
+
* plugin.canHandle("spotify:track:123"); // false
|
|
153
|
+
*/
|
|
154
|
+
canHandle(query: string): boolean {
|
|
155
|
+
const q = (query || "").trim().toLowerCase();
|
|
156
|
+
const isUrl = q.startsWith("http://") || q.startsWith("https://");
|
|
157
|
+
if (isUrl) {
|
|
158
|
+
try {
|
|
159
|
+
const parsed = new URL(query);
|
|
160
|
+
const allowedHosts = ["youtube.com", "www.youtube.com", "music.youtube.com", "youtu.be", "www.youtu.be"];
|
|
161
|
+
return allowedHosts.includes(parsed.hostname.toLowerCase());
|
|
162
|
+
} catch (e) {
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Avoid intercepting explicit patterns for other extractors
|
|
168
|
+
if (q.startsWith("tts:") || q.startsWith("say ")) return false;
|
|
169
|
+
if (q.startsWith("spotify:") || q.includes("open.spotify.com")) return false;
|
|
170
|
+
if (q.includes("soundcloud")) return false;
|
|
171
|
+
|
|
172
|
+
// Treat remaining non-URL free text as YouTube-searchable
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Validates if a URL is a valid YouTube URL.
|
|
178
|
+
*
|
|
179
|
+
* @param url - The URL to validate
|
|
180
|
+
* @returns `true` if the URL is a valid YouTube URL, `false` otherwise
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* plugin.validate("https://www.youtube.com/watch?v=dQw4w9WgXcQ"); // true
|
|
184
|
+
* plugin.validate("https://youtu.be/dQw4w9WgXcQ"); // true
|
|
185
|
+
* plugin.validate("https://spotify.com/track/123"); // false
|
|
186
|
+
*/
|
|
187
|
+
validate(url: string): boolean {
|
|
188
|
+
try {
|
|
189
|
+
const parsed = new URL(url);
|
|
190
|
+
const allowedHosts = ["youtube.com", "www.youtube.com", "music.youtube.com", "youtu.be", "www.youtu.be", "m.youtube.com"];
|
|
191
|
+
return allowedHosts.includes(parsed.hostname.toLowerCase());
|
|
192
|
+
} catch (e) {
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Searches for YouTube content based on the given query.
|
|
199
|
+
*
|
|
200
|
+
* This method handles both URL-based queries (direct video/playlist links) and
|
|
201
|
+
* text-based search queries. For URLs, it will extract video or playlist information.
|
|
202
|
+
* For text queries, it will perform a YouTube search and return up to 10 results.
|
|
203
|
+
*
|
|
204
|
+
* @param query - The search query (URL or text)
|
|
205
|
+
* @param requestedBy - The user ID who requested the search
|
|
206
|
+
* @returns A SearchResult containing tracks and optional playlist information
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* // Search by URL
|
|
210
|
+
* const result = await plugin.search("https://www.youtube.com/watch?v=dQw4w9WgXcQ", "user123");
|
|
211
|
+
*
|
|
212
|
+
* // Search by text
|
|
213
|
+
* const searchResult = await plugin.search("Never Gonna Give You Up", "user123");
|
|
214
|
+
* console.log(searchResult.tracks); // Array of Track objects
|
|
215
|
+
*/
|
|
216
|
+
async search(query: string, requestedBy: string): Promise<SearchResult> {
|
|
217
|
+
await this.ready;
|
|
218
|
+
|
|
219
|
+
if (this.validate(query)) {
|
|
220
|
+
const listId = this.extractListId(query);
|
|
221
|
+
if (listId) {
|
|
222
|
+
if (this.isMixListId(listId)) {
|
|
223
|
+
const anchorVideoId = this.extractVideoId(query);
|
|
224
|
+
if (anchorVideoId) {
|
|
225
|
+
try {
|
|
226
|
+
const info: any = await (this.searchClient as any).getInfo(anchorVideoId);
|
|
227
|
+
const feed: any[] = info?.watch_next_feed || [];
|
|
228
|
+
const tracks: Track[] = feed
|
|
229
|
+
.filter((tr: any) => tr?.content_type === "VIDEO")
|
|
230
|
+
.map((v: any) => this.buildTrack(v, requestedBy, { playlist: listId }));
|
|
231
|
+
const { basic_info } = info;
|
|
232
|
+
|
|
233
|
+
const currTrack = this.buildTrack(basic_info, requestedBy);
|
|
234
|
+
tracks.unshift(currTrack);
|
|
235
|
+
return {
|
|
236
|
+
tracks,
|
|
237
|
+
playlist: { name: "YouTube Mix", url: query, thumbnail: tracks[0]?.thumbnail },
|
|
238
|
+
};
|
|
239
|
+
} catch {
|
|
240
|
+
// ignore and fall back to normal playlist handling below
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
try {
|
|
245
|
+
const playlist: any = await (this.searchClient as any).getPlaylist(listId);
|
|
246
|
+
const videos: any[] = playlist?.videos || playlist?.items || [];
|
|
247
|
+
const tracks: Track[] = videos.map((v: any) => this.buildTrack(v, requestedBy, { playlist: listId }));
|
|
248
|
+
|
|
249
|
+
return {
|
|
250
|
+
tracks,
|
|
251
|
+
playlist: {
|
|
252
|
+
name: playlist?.title || playlist?.metadata?.title || `Playlist ${listId}`,
|
|
253
|
+
url: query,
|
|
254
|
+
thumbnail: playlist?.thumbnails?.[0]?.url || playlist?.thumbnail?.url,
|
|
255
|
+
},
|
|
256
|
+
};
|
|
257
|
+
} catch {
|
|
258
|
+
const withoutList = query.replace(/[?&]list=[^&]+/, "").replace(/[?&]$/, "");
|
|
259
|
+
return await this.search(withoutList, requestedBy);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const videoId = this.extractVideoId(query);
|
|
264
|
+
if (!videoId) throw new Error("Invalid YouTube URL");
|
|
265
|
+
|
|
266
|
+
const info = await this.client.getBasicInfo(videoId);
|
|
267
|
+
const track = this.buildTrack(info, requestedBy);
|
|
268
|
+
return { tracks: [track] };
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// Text search → return up to 10 video tracks
|
|
272
|
+
const res: any = await this.searchClient.search(query, {
|
|
273
|
+
type: "video" as any,
|
|
274
|
+
});
|
|
275
|
+
const items: any[] = res?.items || res?.videos || res?.results || [];
|
|
276
|
+
|
|
277
|
+
const tracks: Track[] = items.slice(0, 10).map((v: any) => this.buildTrack(v, requestedBy));
|
|
278
|
+
|
|
279
|
+
return { tracks };
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Extracts tracks from a YouTube playlist URL.
|
|
284
|
+
*
|
|
285
|
+
* @param url - The YouTube playlist URL
|
|
286
|
+
* @param requestedBy - The user ID who requested the extraction
|
|
287
|
+
* @returns An array of Track objects from the playlist
|
|
288
|
+
*
|
|
289
|
+
* @example
|
|
290
|
+
* const tracks = await plugin.extractPlaylist(
|
|
291
|
+
* "https://www.youtube.com/playlist?list=PLrAXtmRdnEQy6nuLMOV8uM0bMq3MUfHc1",
|
|
292
|
+
* "user123"
|
|
293
|
+
* );
|
|
294
|
+
* console.log(`Found ${tracks.length} tracks in playlist`);
|
|
295
|
+
*/
|
|
296
|
+
async extractPlaylist(url: string, requestedBy: string): Promise<Track[]> {
|
|
297
|
+
await this.ready;
|
|
298
|
+
|
|
299
|
+
const listId = this.extractListId(url);
|
|
300
|
+
if (!listId) return [];
|
|
301
|
+
|
|
302
|
+
try {
|
|
303
|
+
// Attempt to handle dynamic Mix playlists via watch_next feed
|
|
304
|
+
if (this.isMixListId(listId)) {
|
|
305
|
+
const anchorVideoId = this.extractVideoId(url);
|
|
306
|
+
if (anchorVideoId) {
|
|
307
|
+
try {
|
|
308
|
+
const info: any = await (this.searchClient as any).getInfo(anchorVideoId);
|
|
309
|
+
const feed: any[] = info?.watch_next_feed || [];
|
|
310
|
+
return feed
|
|
311
|
+
.filter((tr: any) => tr?.content_type === "VIDEO")
|
|
312
|
+
.map((v: any) => this.buildTrack(v, requestedBy, { playlist: listId }));
|
|
313
|
+
} catch {}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const playlist: any = await (this.client as any).getPlaylist(listId);
|
|
318
|
+
const videos: any[] = playlist?.videos || playlist?.items || [];
|
|
319
|
+
return videos.map((v: any) => {
|
|
320
|
+
return this.buildTrack(v, requestedBy, { playlist: listId }); //ack;
|
|
321
|
+
});
|
|
322
|
+
} catch {
|
|
323
|
+
return [];
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Retrieves the audio stream for a YouTube track.
|
|
329
|
+
*
|
|
330
|
+
* This method extracts the audio stream from a YouTube video using the YouTube client.
|
|
331
|
+
* It attempts to get the best quality audio stream available and handles various
|
|
332
|
+
* format fallbacks if the primary method fails.
|
|
333
|
+
*
|
|
334
|
+
* @param track - The Track object to get the stream for
|
|
335
|
+
* @returns A StreamInfo object containing the audio stream and metadata
|
|
336
|
+
* @throws {Error} If the track ID is invalid or stream extraction fails
|
|
337
|
+
*
|
|
338
|
+
* @example
|
|
339
|
+
* const track = { id: "dQw4w9WgXcQ", title: "Never Gonna Give You Up", ... };
|
|
340
|
+
* const streamInfo = await plugin.getStream(track);
|
|
341
|
+
* console.log(streamInfo.type); // "arbitrary"
|
|
342
|
+
* console.log(streamInfo.stream); // Readable stream
|
|
343
|
+
*/
|
|
344
|
+
async getStream(track: Track): Promise<StreamInfo> {
|
|
345
|
+
await this.ready;
|
|
346
|
+
|
|
347
|
+
const id = this.extractVideoId(track.url) || track.id;
|
|
348
|
+
|
|
349
|
+
if (!id) throw new Error("Invalid track id");
|
|
350
|
+
|
|
351
|
+
try {
|
|
352
|
+
const stream: any = await (this.client as any).download(id, {
|
|
353
|
+
type: "audio",
|
|
354
|
+
quality: "best",
|
|
355
|
+
});
|
|
356
|
+
return {
|
|
357
|
+
stream,
|
|
358
|
+
type: "arbitrary",
|
|
359
|
+
metadata: track.metadata,
|
|
360
|
+
};
|
|
361
|
+
} catch (e: any) {
|
|
362
|
+
try {
|
|
363
|
+
const info: any = await (this.client as any).getBasicInfo(id);
|
|
364
|
+
|
|
365
|
+
// Prefer m4a audio-only formats first
|
|
366
|
+
let format: any = info?.chooseFormat?.({
|
|
367
|
+
type: "audio",
|
|
368
|
+
quality: "best",
|
|
369
|
+
});
|
|
370
|
+
if (!format && info?.formats?.length) {
|
|
371
|
+
const audioOnly = info.formats.filter((f: any) => f.mime_type?.includes("audio"));
|
|
372
|
+
audioOnly.sort((a: any, b: any) => (b.bitrate || 0) - (a.bitrate || 0));
|
|
373
|
+
format = audioOnly[0];
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
if (!format) throw new Error("No audio format available");
|
|
377
|
+
|
|
378
|
+
let url: string | undefined = undefined;
|
|
379
|
+
if (typeof format.decipher === "function") {
|
|
380
|
+
url = format.decipher((this.client as any).session.player);
|
|
381
|
+
}
|
|
382
|
+
if (!url) url = format.url;
|
|
383
|
+
|
|
384
|
+
if (!url) throw new Error("No valid URL to decipher");
|
|
385
|
+
const res = await fetch(url);
|
|
386
|
+
|
|
387
|
+
if (!res.ok || !res.body) {
|
|
388
|
+
throw new Error(`HTTP ${res.status}`);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
return {
|
|
392
|
+
stream: res.body as any,
|
|
393
|
+
type: "arbitrary",
|
|
394
|
+
metadata: {
|
|
395
|
+
...track.metadata,
|
|
396
|
+
itag: format.itag,
|
|
397
|
+
mime: format.mime_type,
|
|
398
|
+
},
|
|
399
|
+
};
|
|
400
|
+
} catch (inner: any) {
|
|
401
|
+
throw new Error(`Failed to get YouTube stream: ${inner?.message || inner}`);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* Gets related tracks for a given YouTube video.
|
|
408
|
+
*
|
|
409
|
+
* This method fetches the "watch next" feed from YouTube to find related videos
|
|
410
|
+
* that are similar to the provided track. It can filter out tracks that are
|
|
411
|
+
* already in the history to avoid duplicates.
|
|
412
|
+
*
|
|
413
|
+
* @param trackURL - The YouTube video URL to get related tracks for
|
|
414
|
+
* @param opts - Options for filtering and limiting results
|
|
415
|
+
* @param opts.limit - Maximum number of related tracks to return (default: 5)
|
|
416
|
+
* @param opts.offset - Number of tracks to skip from the beginning (default: 0)
|
|
417
|
+
* @param opts.history - Array of tracks to exclude from results
|
|
418
|
+
* @returns An array of related Track objects
|
|
419
|
+
*
|
|
420
|
+
* @example
|
|
421
|
+
* const related = await plugin.getRelatedTracks(
|
|
422
|
+
* "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
|
|
423
|
+
* { limit: 3, history: [currentTrack] }
|
|
424
|
+
* );
|
|
425
|
+
* console.log(`Found ${related.length} related tracks`);
|
|
426
|
+
*/
|
|
427
|
+
async getRelatedTracks(trackURL: string, opts: { limit?: number; offset?: number; history?: Track[] } = {}): Promise<Track[]> {
|
|
428
|
+
await this.ready;
|
|
429
|
+
const videoId = this.extractVideoId(trackURL);
|
|
430
|
+
if (!videoId) {
|
|
431
|
+
// If the last track URL is not a direct video URL (e.g., playlist URL),
|
|
432
|
+
// we cannot fetch related videos reliably.
|
|
433
|
+
return [];
|
|
434
|
+
}
|
|
435
|
+
const info: any = await await (this.searchClient as any).getInfo(videoId);
|
|
436
|
+
const related: any[] = info?.watch_next_feed || [];
|
|
437
|
+
const offset = opts.offset ?? 0;
|
|
438
|
+
const limit = opts.limit ?? 5;
|
|
439
|
+
|
|
440
|
+
const relatedfilter = related.filter(
|
|
441
|
+
(tr: any) => tr.content_type === "VIDEO" && !(opts?.history ?? []).some((t) => t.url === tr.url),
|
|
442
|
+
);
|
|
443
|
+
|
|
444
|
+
return relatedfilter.slice(offset, offset + limit).map((v: any) => this.buildTrack(v, "auto"));
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* Provides a fallback stream by searching for the track title.
|
|
449
|
+
*
|
|
450
|
+
* This method is used when the primary stream extraction fails. It performs
|
|
451
|
+
* a search using the track's title and attempts to get a stream from the
|
|
452
|
+
* first search result.
|
|
453
|
+
*
|
|
454
|
+
* @param track - The Track object to get a fallback stream for
|
|
455
|
+
* @returns A StreamInfo object containing the fallback audio stream
|
|
456
|
+
* @throws {Error} If no fallback track is found or stream extraction fails
|
|
457
|
+
*
|
|
458
|
+
* @example
|
|
459
|
+
* try {
|
|
460
|
+
* const stream = await plugin.getStream(track);
|
|
461
|
+
* } catch (error) {
|
|
462
|
+
* // Try fallback
|
|
463
|
+
* const fallbackStream = await plugin.getFallback(track);
|
|
464
|
+
* }
|
|
465
|
+
*/
|
|
466
|
+
async getFallback(track: Track): Promise<StreamInfo> {
|
|
467
|
+
try {
|
|
468
|
+
const result = await this.search(track.title, track.requestedBy);
|
|
469
|
+
const first = result.tracks[0];
|
|
470
|
+
if (!first) throw new Error("No fallback track found");
|
|
471
|
+
return await this.getStream(first);
|
|
472
|
+
} catch (e: any) {
|
|
473
|
+
throw new Error(`YouTube fallback search failed: ${e?.message || e}`);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
private extractVideoId(input: string): string | null {
|
|
478
|
+
try {
|
|
479
|
+
const u = new URL(input);
|
|
480
|
+
const allowedShortHosts = ["youtu.be"];
|
|
481
|
+
const allowedLongHosts = ["youtube.com", "www.youtube.com", "music.youtube.com", "m.youtube.com"];
|
|
482
|
+
if (allowedShortHosts.includes(u.hostname)) {
|
|
483
|
+
return u.pathname.split("/").filter(Boolean)[0] || null;
|
|
484
|
+
}
|
|
485
|
+
if (allowedLongHosts.includes(u.hostname)) {
|
|
486
|
+
// watch?v=, shorts/, embed/
|
|
487
|
+
if (u.searchParams.get("v")) return u.searchParams.get("v");
|
|
488
|
+
const path = u.pathname;
|
|
489
|
+
if (path.startsWith("/shorts/")) return path.replace("/shorts/", "");
|
|
490
|
+
if (path.startsWith("/embed/")) return path.replace("/embed/", "");
|
|
491
|
+
}
|
|
492
|
+
return null;
|
|
493
|
+
} catch {
|
|
494
|
+
return null;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
private isMixListId(listId: string): boolean {
|
|
499
|
+
// YouTube dynamic mixes typically start with 'RD'
|
|
500
|
+
return typeof listId === "string" && listId.toUpperCase().startsWith("RD");
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
private extractListId(input: string): string | null {
|
|
504
|
+
try {
|
|
505
|
+
const u = new URL(input);
|
|
506
|
+
return u.searchParams.get("list");
|
|
507
|
+
} catch {
|
|
508
|
+
return null;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
function toSeconds(d: any): number | undefined {
|
|
513
|
+
if (typeof d === "number") return d;
|
|
514
|
+
if (typeof d === "string") {
|
|
515
|
+
// mm:ss or hh:mm:ss
|
|
516
|
+
const parts = d.split(":").map(Number);
|
|
517
|
+
if (parts.some((n) => Number.isNaN(n))) return undefined;
|
|
518
|
+
if (parts.length === 3) return parts[0] * 3600 + parts[1] * 60 + parts[2];
|
|
519
|
+
if (parts.length === 2) return parts[0] * 60 + parts[1];
|
|
520
|
+
const asNum = Number(d);
|
|
521
|
+
return Number.isFinite(asNum) ? asNum : undefined;
|
|
522
|
+
}
|
|
523
|
+
if (d && typeof d === "object") {
|
|
524
|
+
if (typeof (d as any).seconds === "number") return (d as any).seconds;
|
|
525
|
+
if (typeof (d as any).milliseconds === "number") return Math.floor((d as any).milliseconds / 1000);
|
|
526
|
+
}
|
|
527
|
+
return undefined;
|
|
528
|
+
}
|