Merge pull request #230 from play-dl/developer

1.7.1
This commit is contained in:
Killer069 2022-01-04 15:21:26 +05:30 committed by GitHub
commit b746646604
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 275 additions and 164 deletions

View File

@ -491,6 +491,21 @@ export class DeezerAlbum {
return this; return this;
} }
/**
* Fetches all the tracks in the album and returns them
*
* ```ts
* const album = await play.deezer('album url')
*
* const tracks = await album.all_tracks()
* ```
* @returns An array of {@link DeezerTrack}
*/
async all_tracks(): Promise<DeezerTrack[]> {
await this.fetch();
return this.tracks as DeezerTrack[];
}
/** /**
* Converts instances of this class to JSON data * Converts instances of this class to JSON data
* @returns JSON data. * @returns JSON data.
@ -746,6 +761,21 @@ export class DeezerPlaylist {
return this; return this;
} }
/**
* Fetches all the tracks in the playlist and returns them
*
* ```ts
* const playlist = await play.deezer('playlist url')
*
* const tracks = await playlist.all_tracks()
* ```
* @returns An array of {@link DeezerTrack}
*/
async all_tracks(): Promise<DeezerTrack[]> {
await this.fetch();
return this.tracks as DeezerTrack[];
}
/** /**
* Converts instances of this class to JSON data * Converts instances of this class to JSON data
* @returns JSON data. * @returns JSON data.

View File

@ -314,6 +314,7 @@ export class SoundCloudPlaylist {
} }
/** /**
* Get total no. of fetched tracks * Get total no. of fetched tracks
* @see {@link SoundCloudPlaylist.all_tracks}
*/ */
get total_tracks(): number { get total_tracks(): number {
let count = 0; let count = 0;
@ -324,25 +325,19 @@ export class SoundCloudPlaylist {
return count; return count;
} }
/** /**
* Get all fetched tracks as a array. * Fetches all the tracks in the playlist and returns them
*
* For getting all feetched tracks
* *
* ```ts * ```ts
* const playlist = await play.soundcloud("playlist url") * const playlist = await play.soundcloud('playlist url')
* *
* await playlist.fetch() * const tracks = await playlist.all_tracks()
*
* const result = playlist.fetched_tracks
* ``` * ```
* @returns An array of {@link SoundCloudTrack}
*/ */
get fetched_tracks(): SoundCloudTrack[] { async all_tracks(): Promise<SoundCloudTrack[]> {
let result: SoundCloudTrack[] = []; await this.fetch();
this.tracks.forEach((track) => {
if (track instanceof SoundCloudTrack) result.push(track); return this.tracks as SoundCloudTrack[];
else return;
});
return result;
} }
/** /**
* Converts Class to JSON data * Converts Class to JSON data

View File

@ -309,21 +309,8 @@ export class SpotifyPlaylist {
return this.fetched_tracks.get(`${num}`) as SpotifyTrack[]; return this.fetched_tracks.get(`${num}`) as SpotifyTrack[];
} }
/** /**
* Spotify Playlist total no of pages in a playlist * Gets total number of pages in that playlist class.
* * @see {@link SpotifyPlaylist.all_tracks}
* For getting all songs in a playlist,
*
* ```ts
* const playlist = await play.spotify('playlist url')
*
* await playlist.fetch()
*
* const result = []
*
* for (let i = 0; i <= playlist.tota_pages; i++) {
* result.push(playlist.page(i))
* }
* ```
*/ */
get total_pages() { get total_pages() {
return this.fetched_tracks.size; return this.fetched_tracks.size;
@ -336,6 +323,25 @@ export class SpotifyPlaylist {
const page_number: number = this.total_pages; const page_number: number = this.total_pages;
return (page_number - 1) * 100 + (this.fetched_tracks.get(`${page_number}`) as SpotifyTrack[]).length; return (page_number - 1) * 100 + (this.fetched_tracks.get(`${page_number}`) as SpotifyTrack[]).length;
} }
/**
* Fetches all the tracks in the playlist and returns them
*
* ```ts
* const playlist = await play.spotify('playlist url')
*
* const tracks = await playlist.all_tracks()
* ```
* @returns An array of {@link SpotifyTrack}
*/
async all_tracks(): Promise<SpotifyTrack[]> {
await this.fetch();
const tracks: SpotifyTrack[] = [];
for (const page of this.fetched_tracks.values()) tracks.push(...page);
return tracks;
}
/** /**
* Converts Class to JSON * Converts Class to JSON
* @returns JSON data * @returns JSON data
@ -508,21 +514,8 @@ export class SpotifyAlbum {
return this.fetched_tracks.get(`${num}`); return this.fetched_tracks.get(`${num}`);
} }
/** /**
* Spotify Album total no of pages in a album * Gets total number of pages in that album class.
* * @see {@link SpotifyAlbum.all_tracks}
* For getting all songs in a album,
*
* ```ts
* const album = await play.spotify('album url')
*
* await album.fetch()
*
* const result = []
*
* for (let i = 0; i <= album.tota_pages; i++) {
* result.push(album.page(i))
* }
* ```
*/ */
get total_pages() { get total_pages() {
return this.fetched_tracks.size; return this.fetched_tracks.size;
@ -535,7 +528,29 @@ export class SpotifyAlbum {
const page_number: number = this.total_pages; const page_number: number = this.total_pages;
return (page_number - 1) * 100 + (this.fetched_tracks.get(`${page_number}`) as SpotifyTrack[]).length; return (page_number - 1) * 100 + (this.fetched_tracks.get(`${page_number}`) as SpotifyTrack[]).length;
} }
/**
* Fetches all the tracks in the album and returns them
*
* ```ts
* const album = await play.spotify('album url')
*
* const tracks = await album.all_tracks()
* ```
* @returns An array of {@link SpotifyTrack}
*/
async all_tracks(): Promise<SpotifyTrack[]> {
await this.fetch();
const tracks: SpotifyTrack[] = [];
for (const page of this.fetched_tracks.values()) tracks.push(...page);
return tracks;
}
/**
* Converts Class to JSON
* @returns JSON data
*/
toJSON(): AlbumJSON { toJSON(): AlbumJSON {
return { return {
name: this.name, name: this.name,

View File

@ -237,10 +237,10 @@ export async function refreshToken(): Promise<boolean> {
return true; return true;
} }
export function setSpotifyToken(options: SpotifyDataOptions) { export async function setSpotifyToken(options: SpotifyDataOptions) {
spotifyData = options; spotifyData = options;
spotifyData.file = false; spotifyData.file = false;
refreshToken(); await refreshToken();
} }
export { SpotifyTrack, SpotifyAlbum, SpotifyPlaylist }; export { SpotifyTrack, SpotifyAlbum, SpotifyPlaylist };

View File

@ -3,12 +3,8 @@ import { IncomingMessage } from 'node:http';
import { parseAudioFormats, StreamOptions, StreamType } from '../stream'; import { parseAudioFormats, StreamOptions, StreamType } from '../stream';
import { request, request_stream } from '../../Request'; import { request, request_stream } from '../../Request';
import { video_stream_info } from '../utils/extractor'; import { video_stream_info } from '../utils/extractor';
import { URL } from 'node:url';
export interface FormatInterface {
url: string;
targetDurationSec: number;
maxDvrDurationSec: number;
}
/** /**
* YouTube Live Stream class for playing audio from Live Stream videos. * YouTube Live Stream class for playing audio from Live Stream videos.
*/ */
@ -22,29 +18,16 @@ export class LiveStream {
*/ */
type: StreamType; type: StreamType;
/** /**
* Base URL in dash manifest file. * Incoming message that we recieve.
*
* Storing this is essential.
* This helps to destroy the TCP connection completely if you stopped player in between the stream
*/ */
private base_url: string; private request?: IncomingMessage;
/**
* Given Dash URL.
*/
private url: string;
/**
* Interval to fetch data again to dash url.
*/
private interval: number;
/**
* Sequence count of urls in dash file.
*/
private packet_count: number;
/** /**
* Timer that creates loop from interval time provided. * Timer that creates loop from interval time provided.
*/ */
private timer: Timer; private normal_timer?: Timer;
/**
* Live Stream Video url.
*/
private video_url: string;
/** /**
* Timer used to update dash url so as to avoid 404 errors after long hours of streaming. * Timer used to update dash url so as to avoid 404 errors after long hours of streaming.
* *
@ -52,45 +35,69 @@ export class LiveStream {
*/ */
private dash_timer: Timer; private dash_timer: Timer;
/** /**
* Segments of url that we recieve in dash file. * Given Dash URL.
*
* base_url + segment_urls[0] = One complete url for one segment.
*/ */
private segments_urls: string[]; private dash_url: string;
/** /**
* Incoming message that we recieve. * Base URL in dash manifest file.
*
* Storing this is essential.
* This helps to destroy the TCP connection completely if you stopped player in between the stream
*/ */
private request: IncomingMessage | null; private base_url: string;
/**
* Interval to fetch data again to dash url.
*/
private interval: number;
/**
* Timer used to update dash url so as to avoid 404 errors after long hours of streaming.
*
* It updates dash_url every 30 minutes.
*/
private video_url: string;
/**
* No of segments of data to add in stream before starting to loop
*/
private precache: number;
/**
* Segment sequence number
*/
private sequence: number;
/** /**
* Live Stream Class Constructor * Live Stream Class Constructor
* @param dash_url dash manifest URL * @param dash_url dash manifest URL
* @param target_interval interval time for fetching dash data again * @param target_interval interval time for fetching dash data again
* @param video_url Live Stream video url. * @param video_url Live Stream video url.
*/ */
constructor(dash_url: string, target_interval: number, video_url: string) { constructor(dash_url: string, interval: number, video_url: string, precache?: number) {
this.stream = new Readable({ highWaterMark: 5 * 1000 * 1000, read() {} }); this.stream = new Readable({ highWaterMark: 5 * 1000 * 1000, read() {} });
this.type = StreamType.Arbitrary; this.type = StreamType.Arbitrary;
this.url = dash_url; this.sequence = 0;
this.dash_url = dash_url;
this.base_url = ''; this.base_url = '';
this.segments_urls = []; this.interval = interval;
this.packet_count = 0;
this.request = null;
this.video_url = video_url; this.video_url = video_url;
this.interval = target_interval || 0; this.precache = precache || 3;
this.timer = new Timer(() => {
this.start();
}, this.interval);
this.dash_timer = new Timer(() => { this.dash_timer = new Timer(() => {
this.dash_timer.reuse();
this.dash_updater(); this.dash_updater();
this.dash_timer.reuse();
}, 1800); }, 1800);
this.stream.on('close', () => { this.stream.on('close', () => {
this.cleanup(); this.cleanup();
}); });
this.start(); this.initialize_dash();
}
/**
* This cleans every used variable in class.
*
* This is used to prevent re-use of this class and helping garbage collector to collect it.
*/
private cleanup() {
this.normal_timer?.destroy();
this.dash_timer.destroy();
this.request?.destroy();
this.video_url = '';
this.request = undefined;
this.dash_url = '';
this.base_url = '';
this.interval = 0;
} }
/** /**
* Updates dash url. * Updates dash url.
@ -99,68 +106,43 @@ export class LiveStream {
*/ */
private async dash_updater() { private async dash_updater() {
const info = await video_stream_info(this.video_url); const info = await video_stream_info(this.video_url);
if ( if (info.LiveStreamData.dashManifestUrl) this.dash_url = info.LiveStreamData.dashManifestUrl;
info.LiveStreamData.isLive === true && return this.initialize_dash();
info.LiveStreamData.hlsManifestUrl !== null &&
info.video_details.durationInSec === 0
) {
this.url = info.LiveStreamData.dashManifestUrl as string;
}
} }
/** /**
* Parses data recieved from dash_url. * Initializes dash after getting dash url.
* *
* Updates base_url , segments_urls array. * Start if it is first time of initialishing dash function.
*/ */
private async dash_getter() { private async initialize_dash() {
const response = await request(this.url); const response = await request(this.dash_url);
const audioFormat = response const audioFormat = response
.split('<AdaptationSet id="0"')[1] .split('<AdaptationSet id="0"')[1]
.split('</AdaptationSet>')[0] .split('</AdaptationSet>')[0]
.split('</Representation>'); .split('</Representation>');
if (audioFormat[audioFormat.length - 1] === '') audioFormat.pop(); if (audioFormat[audioFormat.length - 1] === '') audioFormat.pop();
this.base_url = audioFormat[audioFormat.length - 1].split('<BaseURL>')[1].split('</BaseURL>')[0]; this.base_url = audioFormat[audioFormat.length - 1].split('<BaseURL>')[1].split('</BaseURL>')[0];
const list = audioFormat[audioFormat.length - 1].split('<SegmentList>')[1].split('</SegmentList>')[0]; await request_stream(`https://${new URL(this.base_url).host}/generate_204`);
this.segments_urls = list.replace(new RegExp('<SegmentURL media="', 'g'), '').split('"/>'); if (this.sequence === 0) {
if (this.segments_urls[this.segments_urls.length - 1] === '') this.segments_urls.pop(); const list = audioFormat[audioFormat.length - 1]
} .split('<SegmentList>')[1]
/** .split('</SegmentList>')[0]
* This cleans every used variable in class. .replaceAll('<SegmentURL media="', '')
* .split('"/>');
* This is used to prevent re-use of this class and helping garbage collector to collect it. if (list[list.length - 1] === '') list.pop();
*/ if (list.length > this.precache) list.splice(0, list.length - this.precache);
private cleanup() { this.sequence = Number(list[0].split('sq/')[1].split('/')[0]);
this.timer.destroy(); this.first_data(list.length);
this.dash_timer.destroy();
this.request?.destroy();
this.video_url = '';
this.request = null;
this.url = '';
this.base_url = '';
this.segments_urls = [];
this.packet_count = 0;
this.interval = 0;
}
/**
* This starts function in Live Stream Class.
*
* Gets data from dash url and pass it to dash getter function.
* Get data from complete segment url and pass data to Stream.
*/
private async start() {
if (this.stream.destroyed) {
this.cleanup();
return;
} }
await this.dash_getter(); }
if (this.segments_urls.length > 3) this.segments_urls.splice(0, this.segments_urls.length - 3); /**
if (this.packet_count === 0) this.packet_count = Number(this.segments_urls[0].split('sq/')[1].split('/')[0]); * Used only after initializing dash function first time.
for await (const segment of this.segments_urls) { * @param len Length of data that you want to
if (Number(segment.split('sq/')[1].split('/')[0]) !== this.packet_count) { */
continue; private async first_data(len: number) {
} for (let i = 1; i <= len; i++) {
await new Promise(async (resolve, reject) => { await new Promise(async (resolve) => {
const stream = await request_stream(this.base_url + segment).catch((err: Error) => err); const stream = await request_stream(this.base_url + 'sq/' + this.sequence).catch((err: Error) => err);
if (stream instanceof Error) { if (stream instanceof Error) {
this.stream.emit('error', stream); this.stream.emit('error', stream);
return; return;
@ -170,7 +152,7 @@ export class LiveStream {
this.stream.push(c); this.stream.push(c);
}); });
stream.on('end', () => { stream.on('end', () => {
this.packet_count++; this.sequence++;
resolve(''); resolve('');
}); });
stream.once('error', (err) => { stream.once('error', (err) => {
@ -178,8 +160,35 @@ export class LiveStream {
}); });
}); });
} }
this.normal_timer = new Timer(() => {
this.timer.reuse(); this.loop();
this.normal_timer?.reuse();
}, this.interval);
}
/**
* This loops function in Live Stream Class.
*
* Gets next segment and push it.
*/
private loop() {
return new Promise(async (resolve) => {
const stream = await request_stream(this.base_url + 'sq/' + this.sequence).catch((err: Error) => err);
if (stream instanceof Error) {
this.stream.emit('error', stream);
return;
}
this.request = stream;
stream.on('data', (c) => {
this.stream.push(c);
});
stream.on('end', () => {
this.sequence++;
resolve('');
});
stream.once('error', (err) => {
this.stream.emit('error', err);
});
});
} }
/** /**
* Deprecated Functions * Deprecated Functions

View File

@ -218,6 +218,12 @@ export class YouTubePlayList {
} }
/** /**
* Fetches all the videos in the playlist and returns them * Fetches all the videos in the playlist and returns them
*
* ```ts
* const playlist = await play.playlist_info('playlist url')
*
* const videos = await playlist.all_videos()
* ```
* @returns An array of {@link YouTubeVideo} objects * @returns An array of {@link YouTubeVideo} objects
* @see {@link YouTubePlayList.fetch} * @see {@link YouTubePlayList.fetch}
*/ */
@ -226,9 +232,7 @@ export class YouTubePlayList {
const videos = []; const videos = [];
for (const [, page] of this.fetched_videos.entries()) { for (const page of this.fetched_videos.values()) videos.push(...page);
videos.push(...page);
}
return videos; return videos;
} }

View File

@ -97,7 +97,7 @@ export class SeekStream {
if (!this.stream.headerparsed) { if (!this.stream.headerparsed) {
const stream = await request_stream(this.url, { const stream = await request_stream(this.url, {
headers: { headers: {
range: `bytes=0-1000` range: `bytes=0-`
} }
}).catch((err: Error) => err); }).catch((err: Error) => err);
@ -112,10 +112,12 @@ export class SeekStream {
this.request = stream; this.request = stream;
stream.pipe(this.stream, { end: false }); stream.pipe(this.stream, { end: false });
stream.once('end', () => { this.stream.once("headComplete", () => {
this.stream.state = WebmSeekerState.READING_DATA; stream.unpipe(this.stream)
res(''); stream.destroy()
}); this.stream.state = WebmSeekerState.READING_DATA
res('')
})
} else res(''); } else res('');
}).catch((err) => err); }).catch((err) => err);
if (parse instanceof Error) { if (parse instanceof Error) {

View File

@ -1,6 +1,14 @@
import { YouTubeChannel } from './Channel'; import { YouTubeChannel } from './Channel';
import { YouTubeThumbnail } from './Thumbnail'; import { YouTubeThumbnail } from './Thumbnail';
interface VideoMusic {
song?: string;
artist?: string;
album?: string;
writers?: string;
license?: string;
}
interface VideoOptions { interface VideoOptions {
/** /**
* YouTube Video ID * YouTube Video ID
@ -66,6 +74,10 @@ interface VideoOptions {
* `true` if the video has been identified by the YouTube community as inappropriate or offensive to some audiences and viewer discretion is advised * `true` if the video has been identified by the YouTube community as inappropriate or offensive to some audiences and viewer discretion is advised
*/ */
discretionAdvised?: boolean; discretionAdvised?: boolean;
/**
* Gives info about music content in that video.
*/
music?: VideoMusic[];
} }
/** /**
* Class for YouTube Video url * Class for YouTube Video url
@ -135,6 +147,10 @@ export class YouTubeVideo {
* `true` if the video has been identified by the YouTube community as inappropriate or offensive to some audiences and viewer discretion is advised * `true` if the video has been identified by the YouTube community as inappropriate or offensive to some audiences and viewer discretion is advised
*/ */
discretionAdvised?: boolean; discretionAdvised?: boolean;
/**
* Gives info about music content in that video.
*/
music?: VideoMusic[];
/** /**
* Constructor for YouTube Video Class * Constructor for YouTube Video Class
* @param data JSON parsed data. * @param data JSON parsed data.
@ -161,7 +177,8 @@ export class YouTubeVideo {
this.live = !!data.live; this.live = !!data.live;
this.private = !!data.private; this.private = !!data.private;
this.tags = data.tags || []; this.tags = data.tags || [];
this.discretionAdvised = data.discretionAdvised === undefined ? undefined : data.discretionAdvised; this.discretionAdvised = data.discretionAdvised ?? undefined;
this.music = data.music || [];
} }
/** /**
* Converts class to title name of video. * Converts class to title name of video.
@ -190,7 +207,8 @@ export class YouTubeVideo {
likes: this.likes, likes: this.likes,
live: this.live, live: this.live,
private: this.private, private: this.private,
discretionAdvised: this.discretionAdvised discretionAdvised: this.discretionAdvised,
music: this.music
}; };
} }
} }

View File

@ -131,6 +131,11 @@ export class WebmSeeker extends Duplex {
if (ebmlID.name === 'ebml') this.headfound = true; if (ebmlID.name === 'ebml') this.headfound = true;
else return new Error('Failed to find EBML ID at start of stream.'); else return new Error('Failed to find EBML ID at start of stream.');
} }
if(ebmlID.name === "cluster") {
this.emit("headComplete")
this.cursor = this.chunk.length
break;
}
const data = this.chunk.slice( const data = this.chunk.slice(
this.cursor + this.data_size, this.cursor + this.data_size,
this.cursor + this.data_size + this.data_length this.cursor + this.data_size + this.data_length

View File

@ -4,3 +4,4 @@ export { YouTube } from './search';
export { YouTubeVideo } from './classes/Video'; export { YouTubeVideo } from './classes/Video';
export { YouTubePlayList } from './classes/Playlist'; export { YouTubePlayList } from './classes/Playlist';
export { YouTubeChannel } from './classes/Channel'; export { YouTubeChannel } from './classes/Channel';
export { InfoData } from './utils/constants';

View File

@ -19,6 +19,7 @@ export interface StreamOptions {
quality?: number; quality?: number;
language?: string; language?: string;
htmldata?: boolean; htmldata?: boolean;
precache?: number;
} }
/** /**
@ -71,7 +72,8 @@ export async function stream_from_info(
return new LiveStream( return new LiveStream(
info.LiveStreamData.dashManifestUrl, info.LiveStreamData.dashManifestUrl,
info.format[info.format.length - 1].targetDurationSec as number, info.format[info.format.length - 1].targetDurationSec as number,
info.video_details.url info.video_details.url,
options.precache
); );
} }
@ -83,7 +85,7 @@ export async function stream_from_info(
else final.push(info.format[info.format.length - 1]); else final.push(info.format[info.format.length - 1]);
let type: StreamType = let type: StreamType =
final[0].codec === 'opus' && final[0].container === 'webm' ? StreamType.WebmOpus : StreamType.Arbitrary; final[0].codec === 'opus' && final[0].container === 'webm' ? StreamType.WebmOpus : StreamType.Arbitrary;
await request_stream(`https://${new URL(final[0].url).host}/generate_204`) await request_stream(`https://${new URL(final[0].url).host}/generate_204`);
if (options.seek) { if (options.seek) {
if (type === StreamType.WebmOpus) { if (type === StreamType.WebmOpus) {
if (options.seek >= info.video_details.durationInSec || options.seek <= 0) if (options.seek >= info.video_details.durationInSec || options.seek <= 0)

View File

@ -200,6 +200,25 @@ export async function video_basic_info(url: string, options: InfoOptions = {}):
} }
); );
const microformat = player_response.microformat.playerMicroformatRenderer; const microformat = player_response.microformat.playerMicroformatRenderer;
const musicInfo =
initial_response.contents.twoColumnWatchNextResults.results.results.contents?.[1]?.videoSecondaryInfoRenderer
?.metadataRowContainer?.metadataRowContainerRenderer?.rows;
const music: any[] = [];
if (musicInfo) {
let incompleteInfo: any = {};
musicInfo.forEach((x: any) => {
if (!x.metadataRowRenderer) return;
if (x.metadataRowRenderer.title.simpleText.toLowerCase() === 'song') {
music.push(incompleteInfo);
incompleteInfo = {};
incompleteInfo.song =
x.metadataRowRenderer.contents[0].simpleText ?? x.metadataRowRenderer.contents[0]?.runs?.[0]?.text;
} else
incompleteInfo[x.metadataRowRenderer.title.simpleText.toLowerCase()] =
x.metadataRowRenderer.contents[0].simpleText ?? x.metadataRowRenderer.contents[0]?.runs?.[0]?.text;
});
}
music.shift();
const video_details = new YouTubeVideo({ const video_details = new YouTubeVideo({
id: vid.videoId, id: vid.videoId,
title: vid.title, title: vid.title,
@ -228,7 +247,8 @@ export async function video_basic_info(url: string, options: InfoOptions = {}):
), ),
live: vid.isLiveContent, live: vid.isLiveContent,
private: vid.isPrivate, private: vid.isPrivate,
discretionAdvised discretionAdvised,
music
}); });
const format = player_response.streamingData.formats ?? []; const format = player_response.streamingData.formats ?? [];
format.push(...(player_response.streamingData.adaptiveFormats ?? [])); format.push(...(player_response.streamingData.adaptiveFormats ?? []));

View File

@ -9,7 +9,8 @@ import {
YouTubeStream, YouTubeStream,
YouTubeChannel, YouTubeChannel,
YouTubePlayList, YouTubePlayList,
YouTubeVideo YouTubeVideo,
InfoData
} from './YouTube'; } from './YouTube';
import { import {
spotify, spotify,
@ -73,7 +74,6 @@ import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
import { stream as yt_stream, StreamOptions, stream_from_info as yt_stream_info } from './YouTube/stream'; import { stream as yt_stream, StreamOptions, stream_from_info as yt_stream_info } from './YouTube/stream';
import { yt_search } from './YouTube/search'; import { yt_search } from './YouTube/search';
import { EventEmitter } from 'stream'; import { EventEmitter } from 'stream';
import { InfoData } from './YouTube/utils/constants';
async function stream( async function stream(
url: string, url: string,
@ -252,6 +252,7 @@ async function stream_from_info(info: InfoData, options?: StreamOptions): Promis
* - `string` language : Sets language of searched content [ YouTube search only. ], e.g. "en-US" * - `string` language : Sets language of searched content [ YouTube search only. ], e.g. "en-US"
* - `number` quality : Quality number. [ 0 = Lowest, 1 = Medium, 2 = Highest ] * - `number` quality : Quality number. [ 0 = Lowest, 1 = Medium, 2 = Highest ]
* - `boolean` htmldata : given data is html data or not * - `boolean` htmldata : given data is html data or not
* - `number` precache : No of segments of data to store before looping [YouTube Live Stream only]. [ Defaults to 3 ]
* @returns A {@link YouTubeStream} or {@link SoundCloudStream} Stream to play * @returns A {@link YouTubeStream} or {@link SoundCloudStream} Stream to play
*/ */
async function stream_from_info( async function stream_from_info(
@ -477,7 +478,6 @@ export {
SpotifyAlbum, SpotifyAlbum,
SpotifyPlaylist, SpotifyPlaylist,
SpotifyTrack, SpotifyTrack,
YouTubeStream,
YouTubeChannel, YouTubeChannel,
YouTubePlayList, YouTubePlayList,
YouTubeVideo, YouTubeVideo,
@ -503,11 +503,12 @@ export {
validate, validate,
video_basic_info, video_basic_info,
video_info, video_info,
yt_validate yt_validate,
InfoData
}; };
// Export Types // Export Types
export { Deezer, YouTube, SoundCloud, Spotify }; export { Deezer, YouTube, SoundCloud, Spotify, YouTubeStream };
// Export Default // Export Default
export default { export default {

View File

@ -39,14 +39,23 @@ interface tokenOptions {
* } * }
* }) // YouTube Cookies * }) // YouTube Cookies
* *
* await play.setToken({
* spotify : {
* client_id: 'ID',
client_secret: 'secret',
refresh_token: 'token',
market: 'US'
* }
* }) // Await this only when setting data for spotify
*
* play.setToken({ * play.setToken({
* useragent: ['Your User-agent'] * useragent: ['Your User-agent']
* }) // Use this to avoid 429 errors. * }) // Use this to avoid 429 errors.
* ``` * ```
* @param options {@link tokenOptions} * @param options {@link tokenOptions}
*/ */
export function setToken(options: tokenOptions) { export async function setToken(options: tokenOptions) {
if (options.spotify) setSpotifyToken(options.spotify); if (options.spotify) await setSpotifyToken(options.spotify);
if (options.soundcloud) setSoundCloudToken(options.soundcloud); if (options.soundcloud) setSoundCloudToken(options.soundcloud);
if (options.youtube) setCookieToken(options.youtube); if (options.youtube) setCookieToken(options.youtube);
if (options.useragent) setUserAgent(options.useragent); if (options.useragent) setUserAgent(options.useragent);