Types Improved
This commit is contained in:
parent
842d704bf2
commit
70d1774508
@ -33,11 +33,12 @@ export async function soundcloud(url: string): Promise<SoundCloudTrack | SoundCl
|
|||||||
else return new SoundCloudPlaylist(json_data, soundData.client_id);
|
else return new SoundCloudPlaylist(json_data, soundData.client_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type SoundCloud = SoundCloudTrack | SoundCloudPlaylist;
|
||||||
export async function so_search(
|
export async function so_search(
|
||||||
query: string,
|
query: string,
|
||||||
type: 'tracks' | 'playlists' | 'albums',
|
type: 'tracks' | 'playlists' | 'albums',
|
||||||
limit: number = 10
|
limit: number = 10
|
||||||
): Promise<(SoundCloudPlaylist | SoundCloudTrack)[]> {
|
): Promise<SoundCloud[]> {
|
||||||
const response = await request(
|
const response = await request(
|
||||||
`https://api-v2.soundcloud.com/search/${type}?q=${query}&client_id=${soundData.client_id}&limit=${limit}`
|
`https://api-v2.soundcloud.com/search/${type}?q=${query}&client_id=${soundData.client_id}&limit=${limit}`
|
||||||
);
|
);
|
||||||
@ -66,8 +67,8 @@ export async function stream(url: string, quality?: number): Promise<Stream> {
|
|||||||
: StreamType.Arbitrary;
|
: StreamType.Arbitrary;
|
||||||
return new Stream(s_data.url, type);
|
return new Stream(s_data.url, type);
|
||||||
}
|
}
|
||||||
|
export type SoundCloudStream = Stream;
|
||||||
export async function stream_from_info(data: SoundCloudTrack, quality?: number): Promise<Stream> {
|
export async function stream_from_info(data: SoundCloudTrack, quality?: number): Promise<SoundCloudStream> {
|
||||||
const HLSformats = parseHlsFormats(data.formats);
|
const HLSformats = parseHlsFormats(data.formats);
|
||||||
if (typeof quality !== 'number') quality = HLSformats.length - 1;
|
if (typeof quality !== 'number') quality = HLSformats.length - 1;
|
||||||
else if (quality <= 0) quality = 0;
|
else if (quality <= 0) quality = 0;
|
||||||
|
|||||||
@ -110,11 +110,13 @@ export function is_expired(): boolean {
|
|||||||
else return false;
|
else return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type Spotify = SpotifyAlbum | SpotifyPlaylist | SpotifyVideo;
|
||||||
|
|
||||||
export async function sp_search(
|
export async function sp_search(
|
||||||
query: string,
|
query: string,
|
||||||
type: 'album' | 'playlist' | 'track',
|
type: 'album' | 'playlist' | 'track',
|
||||||
limit: number = 10
|
limit: number = 10
|
||||||
): Promise<(SpotifyAlbum | SpotifyPlaylist | SpotifyVideo)[]> {
|
): Promise<Spotify[]> {
|
||||||
const results: (SpotifyAlbum | SpotifyPlaylist | SpotifyVideo)[] = [];
|
const results: (SpotifyAlbum | SpotifyPlaylist | SpotifyVideo)[] = [];
|
||||||
if (!spotifyData) throw new Error('Spotify Data is missing\nDid you forgot to do authorization ?');
|
if (!spotifyData) throw new Error('Spotify Data is missing\nDid you forgot to do authorization ?');
|
||||||
if (query.length === 0) throw new Error('Pass some query to search.');
|
if (query.length === 0) throw new Error('Pass some query to search.');
|
||||||
|
|||||||
@ -4,17 +4,18 @@ export interface ChannelIconInterface {
|
|||||||
height: number;
|
height: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Channel {
|
export class YouTubeChannel {
|
||||||
name?: string;
|
name?: string;
|
||||||
verified?: boolean;
|
verified?: boolean;
|
||||||
id?: string;
|
id?: string;
|
||||||
|
type: 'video' | 'playlist' | 'channel';
|
||||||
url?: string;
|
url?: string;
|
||||||
icon?: ChannelIconInterface;
|
icon?: ChannelIconInterface;
|
||||||
subscribers?: string;
|
subscribers?: string;
|
||||||
|
|
||||||
constructor(data: any) {
|
constructor(data: any) {
|
||||||
if (!data) throw new Error(`Cannot instantiate the ${this.constructor.name} class without data!`);
|
if (!data) throw new Error(`Cannot instantiate the ${this.constructor.name} class without data!`);
|
||||||
|
this.type = 'channel';
|
||||||
this._patch(data);
|
this._patch(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,10 +42,6 @@ export class Channel {
|
|||||||
return this.icon.url.replace(`=s${def}-c`, `=s${options.size}-c`);
|
return this.icon.url.replace(`=s${def}-c`, `=s${options.size}-c`);
|
||||||
}
|
}
|
||||||
|
|
||||||
get type(): 'channel' {
|
|
||||||
return 'channel';
|
|
||||||
}
|
|
||||||
|
|
||||||
toString(): string {
|
toString(): string {
|
||||||
return this.name || '';
|
return this.name || '';
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,22 +1,27 @@
|
|||||||
import { getPlaylistVideos, getContinuationToken } from '../utils/extractor';
|
import { getPlaylistVideos, getContinuationToken } from '../utils/extractor';
|
||||||
import { request } from '../utils/request';
|
import { request } from '../utils/request';
|
||||||
import { Thumbnail } from './Thumbnail';
|
import { YouTubeChannel } from './Channel';
|
||||||
import { Channel } from './Channel';
|
import { YouTubeVideo } from './Video';
|
||||||
import { Video } from './Video';
|
|
||||||
const BASE_API = 'https://www.youtube.com/youtubei/v1/browse?key=';
|
const BASE_API = 'https://www.youtube.com/youtubei/v1/browse?key=';
|
||||||
|
|
||||||
export class PlayList {
|
export class YouTubePlayList {
|
||||||
id?: string;
|
id?: string;
|
||||||
title?: string;
|
title?: string;
|
||||||
|
type: 'video' | 'playlist' | 'channel';
|
||||||
videoCount?: number;
|
videoCount?: number;
|
||||||
lastUpdate?: string;
|
lastUpdate?: string;
|
||||||
views?: number;
|
views?: number;
|
||||||
url?: string;
|
url?: string;
|
||||||
link?: string;
|
link?: string;
|
||||||
channel?: Channel;
|
channel?: YouTubeChannel;
|
||||||
thumbnail?: Thumbnail;
|
thumbnail?: {
|
||||||
|
id: string | undefined;
|
||||||
|
width: number | undefined;
|
||||||
|
height: number | undefined;
|
||||||
|
url: string | undefined;
|
||||||
|
};
|
||||||
private videos?: [];
|
private videos?: [];
|
||||||
private fetched_videos: Map<string, Video[]>;
|
private fetched_videos: Map<string, YouTubeVideo[]>;
|
||||||
private _continuation: {
|
private _continuation: {
|
||||||
api?: string;
|
api?: string;
|
||||||
token?: string;
|
token?: string;
|
||||||
@ -28,6 +33,7 @@ export class PlayList {
|
|||||||
if (!data) throw new Error(`Cannot instantiate the ${this.constructor.name} class without data!`);
|
if (!data) throw new Error(`Cannot instantiate the ${this.constructor.name} class without data!`);
|
||||||
this.__count = 0;
|
this.__count = 0;
|
||||||
this.fetched_videos = new Map();
|
this.fetched_videos = new Map();
|
||||||
|
this.type = 'playlist';
|
||||||
if (searchResult) this.__patchSearch(data);
|
if (searchResult) this.__patchSearch(data);
|
||||||
else this.__patch(data);
|
else this.__patch(data);
|
||||||
}
|
}
|
||||||
@ -44,7 +50,7 @@ export class PlayList {
|
|||||||
this.thumbnail = data.thumbnail || undefined;
|
this.thumbnail = data.thumbnail || undefined;
|
||||||
this.videos = data.videos || [];
|
this.videos = data.videos || [];
|
||||||
this.__count++;
|
this.__count++;
|
||||||
this.fetched_videos.set(`${this.__count}`, this.videos as Video[]);
|
this.fetched_videos.set(`${this.__count}`, this.videos as YouTubeVideo[]);
|
||||||
this._continuation.api = data.continuation?.api ?? undefined;
|
this._continuation.api = data.continuation?.api ?? undefined;
|
||||||
this._continuation.token = data.continuation?.token ?? undefined;
|
this._continuation.token = data.continuation?.token ?? undefined;
|
||||||
this._continuation.clientVersion = data.continuation?.clientVersion ?? '<important data>';
|
this._continuation.clientVersion = data.continuation?.clientVersion ?? '<important data>';
|
||||||
@ -63,7 +69,7 @@ export class PlayList {
|
|||||||
this.views = 0;
|
this.views = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
async next(limit = Infinity): Promise<Video[]> {
|
async next(limit = Infinity): Promise<YouTubeVideo[]> {
|
||||||
if (!this._continuation || !this._continuation.token) return [];
|
if (!this._continuation || !this._continuation.token) return [];
|
||||||
|
|
||||||
const nextPage = await request(`${BASE_API}${this._continuation.api}`, {
|
const nextPage = await request(`${BASE_API}${this._continuation.api}`, {
|
||||||
@ -109,14 +115,10 @@ export class PlayList {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
get type(): 'playlist' {
|
page(number: number): YouTubeVideo[] {
|
||||||
return 'playlist';
|
|
||||||
}
|
|
||||||
|
|
||||||
page(number: number): Video[] {
|
|
||||||
if (!number) throw new Error('Page number is not provided');
|
if (!number) throw new Error('Page number is not provided');
|
||||||
if (!this.fetched_videos.has(`${number}`)) throw new Error('Given Page number is invalid');
|
if (!this.fetched_videos.has(`${number}`)) throw new Error('Given Page number is invalid');
|
||||||
return this.fetched_videos.get(`${number}`) as Video[];
|
return this.fetched_videos.get(`${number}`) as YouTubeVideo[];
|
||||||
}
|
}
|
||||||
|
|
||||||
get total_pages() {
|
get total_pages() {
|
||||||
@ -125,7 +127,7 @@ export class PlayList {
|
|||||||
|
|
||||||
get total_videos() {
|
get total_videos() {
|
||||||
const page_number: number = this.total_pages;
|
const page_number: number = this.total_pages;
|
||||||
return (page_number - 1) * 100 + (this.fetched_videos.get(`${page_number}`) as Video[]).length;
|
return (page_number - 1) * 100 + (this.fetched_videos.get(`${page_number}`) as YouTubeVideo[]).length;
|
||||||
}
|
}
|
||||||
|
|
||||||
toJSON() {
|
toJSON() {
|
||||||
|
|||||||
@ -1,49 +0,0 @@
|
|||||||
type ThumbnailType = 'default' | 'hqdefault' | 'mqdefault' | 'sddefault' | 'maxresdefault' | 'ultrares';
|
|
||||||
|
|
||||||
export class Thumbnail {
|
|
||||||
id?: string;
|
|
||||||
width?: number;
|
|
||||||
height?: number;
|
|
||||||
url?: string;
|
|
||||||
|
|
||||||
constructor(data: any) {
|
|
||||||
if (!data) throw new Error(`Cannot instantiate the ${this.constructor.name} class without data!`);
|
|
||||||
|
|
||||||
this._patch(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
private _patch(data: any) {
|
|
||||||
if (!data) data = {};
|
|
||||||
|
|
||||||
this.id = data.id || undefined;
|
|
||||||
this.width = data.width || 0;
|
|
||||||
this.height = data.height || 0;
|
|
||||||
this.url = data.url || undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
displayThumbnailURL(thumbnailType: ThumbnailType = 'maxresdefault'): string {
|
|
||||||
if (!['default', 'hqdefault', 'mqdefault', 'sddefault', 'maxresdefault', 'ultrares'].includes(thumbnailType))
|
|
||||||
throw new Error(`Invalid thumbnail type "${thumbnailType}"!`);
|
|
||||||
if (thumbnailType === 'ultrares') return this.url as string;
|
|
||||||
return `https://i3.ytimg.com/vi/${this.id}/${thumbnailType}.jpg`;
|
|
||||||
}
|
|
||||||
|
|
||||||
defaultThumbnailURL(id: '0' | '1' | '2' | '3' | '4'): string {
|
|
||||||
if (!id) id = '0';
|
|
||||||
if (!['0', '1', '2', '3', '4'].includes(id)) throw new Error(`Invalid thumbnail id "${id}"!`);
|
|
||||||
return `https://i3.ytimg.com/vi/${this.id}/${id}.jpg`;
|
|
||||||
}
|
|
||||||
|
|
||||||
toString(): string {
|
|
||||||
return this.url ? `${this.url}` : '';
|
|
||||||
}
|
|
||||||
|
|
||||||
toJSON() {
|
|
||||||
return {
|
|
||||||
id: this.id,
|
|
||||||
width: this.width,
|
|
||||||
height: this.height,
|
|
||||||
url: this.url
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,5 +1,4 @@
|
|||||||
import { Channel } from './Channel';
|
import { YouTubeChannel } from './Channel';
|
||||||
import { Thumbnail } from './Thumbnail';
|
|
||||||
|
|
||||||
interface VideoOptions {
|
interface VideoOptions {
|
||||||
id?: string;
|
id?: string;
|
||||||
@ -21,7 +20,6 @@ interface VideoOptions {
|
|||||||
id: string;
|
id: string;
|
||||||
icon: string;
|
icon: string;
|
||||||
};
|
};
|
||||||
videos?: Video[];
|
|
||||||
type: string;
|
type: string;
|
||||||
ratings: {
|
ratings: {
|
||||||
likes: number;
|
likes: number;
|
||||||
@ -32,9 +30,10 @@ interface VideoOptions {
|
|||||||
tags: string[];
|
tags: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Video {
|
export class YouTubeVideo {
|
||||||
id?: string;
|
id?: string;
|
||||||
url?: string;
|
url: string;
|
||||||
|
type: 'video' | 'playlist' | 'channel';
|
||||||
title?: string;
|
title?: string;
|
||||||
description?: string;
|
description?: string;
|
||||||
durationRaw: string;
|
durationRaw: string;
|
||||||
@ -47,8 +46,7 @@ export class Video {
|
|||||||
height: number | undefined;
|
height: number | undefined;
|
||||||
url: string | undefined;
|
url: string | undefined;
|
||||||
};
|
};
|
||||||
channel?: Channel;
|
channel?: YouTubeChannel;
|
||||||
videos?: Video[];
|
|
||||||
likes: number;
|
likes: number;
|
||||||
dislikes: number;
|
dislikes: number;
|
||||||
live: boolean;
|
live: boolean;
|
||||||
@ -60,6 +58,7 @@ export class Video {
|
|||||||
|
|
||||||
this.id = data.id || undefined;
|
this.id = data.id || undefined;
|
||||||
this.url = `https://www.youtube.com/watch?v=${this.id}`;
|
this.url = `https://www.youtube.com/watch?v=${this.id}`;
|
||||||
|
this.type = 'video';
|
||||||
this.title = data.title || undefined;
|
this.title = data.title || undefined;
|
||||||
this.description = data.description || undefined;
|
this.description = data.description || undefined;
|
||||||
this.durationRaw = data.duration_raw || '0:00';
|
this.durationRaw = data.duration_raw || '0:00';
|
||||||
@ -75,10 +74,6 @@ export class Video {
|
|||||||
this.tags = data.tags || [];
|
this.tags = data.tags || [];
|
||||||
}
|
}
|
||||||
|
|
||||||
get type(): 'video' {
|
|
||||||
return 'video';
|
|
||||||
}
|
|
||||||
|
|
||||||
get toString(): string {
|
get toString(): string {
|
||||||
return this.url || '';
|
return this.url || '';
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,2 +1,3 @@
|
|||||||
export { stream, stream_from_info } from './stream';
|
export { stream, stream_from_info, YouTubeStream } from './stream';
|
||||||
export * from './utils';
|
export * from './utils';
|
||||||
|
export { YouTube } from './search';
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
import { request } from './utils/request';
|
import { request } from './utils/request';
|
||||||
import { ParseSearchInterface, ParseSearchResult } from './utils/parser';
|
import { ParseSearchInterface, ParseSearchResult } from './utils/parser';
|
||||||
import { Video } from './classes/Video';
|
import { YouTubeVideo } from './classes/Video';
|
||||||
import { Channel } from './classes/Channel';
|
import { YouTubeChannel } from './classes/Channel';
|
||||||
import { PlayList } from './classes/Playlist';
|
import { YouTubePlayList } from './classes/Playlist';
|
||||||
|
|
||||||
enum SearchType {
|
enum SearchType {
|
||||||
Video = 'EgIQAQ%253D%253D',
|
Video = 'EgIQAQ%253D%253D',
|
||||||
@ -10,10 +10,9 @@ enum SearchType {
|
|||||||
Channel = 'EgIQAg%253D%253D'
|
Channel = 'EgIQAg%253D%253D'
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function yt_search(
|
export type YouTube = YouTubeVideo | YouTubeChannel | YouTubePlayList;
|
||||||
search: string,
|
|
||||||
options: ParseSearchInterface = {}
|
export async function yt_search(search: string, options: ParseSearchInterface = {}): Promise<YouTube[]> {
|
||||||
): Promise<(Video | Channel | PlayList)[]> {
|
|
||||||
let url = 'https://www.youtube.com/results?search_query=' + search.replaceAll(' ', '+');
|
let url = 'https://www.youtube.com/results?search_query=' + search.replaceAll(' ', '+');
|
||||||
options.type ??= 'video';
|
options.type ??= 'video';
|
||||||
if (!url.match('&sp=')) {
|
if (!url.match('&sp=')) {
|
||||||
|
|||||||
@ -38,13 +38,15 @@ function parseAudioFormats(formats: any[]) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function stream(url: string, options: StreamOptions = {}): Promise<Stream | LiveStreaming> {
|
export type YouTubeStream = Stream | LiveStreaming;
|
||||||
|
|
||||||
|
export async function stream(url: string, options: StreamOptions = {}): Promise<YouTubeStream> {
|
||||||
const info = await video_info(url, options.cookie);
|
const info = await video_info(url, options.cookie);
|
||||||
const final: any[] = [];
|
const final: any[] = [];
|
||||||
if (
|
if (
|
||||||
info.LiveStreamData.isLive === true &&
|
info.LiveStreamData.isLive === true &&
|
||||||
info.LiveStreamData.hlsManifestUrl !== null &&
|
info.LiveStreamData.hlsManifestUrl !== null &&
|
||||||
info.video_details.durationInSec === '0'
|
info.video_details.durationInSec === 0
|
||||||
) {
|
) {
|
||||||
return new LiveStreaming(
|
return new LiveStreaming(
|
||||||
info.LiveStreamData.dashManifestUrl,
|
info.LiveStreamData.dashManifestUrl,
|
||||||
@ -72,7 +74,7 @@ export async function stream(url: string, options: StreamOptions = {}): Promise<
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function stream_from_info(info: InfoData, options: StreamOptions = {}): Promise<Stream | LiveStreaming> {
|
export async function stream_from_info(info: InfoData, options: StreamOptions = {}): Promise<YouTubeStream> {
|
||||||
const final: any[] = [];
|
const final: any[] = [];
|
||||||
if (
|
if (
|
||||||
info.LiveStreamData.isLive === true &&
|
info.LiveStreamData.isLive === true &&
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import { request } from './request';
|
import { request } from './request';
|
||||||
import { format_decipher } from './cipher';
|
import { format_decipher } from './cipher';
|
||||||
import { Video } from '../classes/Video';
|
import { YouTubeVideo } from '../classes/Video';
|
||||||
import { PlayList } from '../classes/Playlist';
|
import { YouTubePlayList } from '../classes/Playlist';
|
||||||
|
|
||||||
const DEFAULT_API_KEY = 'AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8';
|
const DEFAULT_API_KEY = 'AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8';
|
||||||
const video_pattern =
|
const video_pattern =
|
||||||
@ -66,7 +66,7 @@ export async function video_basic_info(url: string, cookie?: string) {
|
|||||||
initial_response.contents.twoColumnWatchNextResults.results.results.contents[1]?.videoSecondaryInfoRenderer
|
initial_response.contents.twoColumnWatchNextResults.results.results.contents[1]?.videoSecondaryInfoRenderer
|
||||||
?.owner?.videoOwnerRenderer?.badges[0];
|
?.owner?.videoOwnerRenderer?.badges[0];
|
||||||
const html5player = `https://www.youtube.com${body.split('"jsUrl":"')[1].split('"')[0]}`;
|
const html5player = `https://www.youtube.com${body.split('"jsUrl":"')[1].split('"')[0]}`;
|
||||||
const related: any[] = [];
|
const related: string[] = [];
|
||||||
initial_response.contents.twoColumnWatchNextResults.secondaryResults.secondaryResults.results.forEach(
|
initial_response.contents.twoColumnWatchNextResults.secondaryResults.secondaryResults.results.forEach(
|
||||||
(res: any) => {
|
(res: any) => {
|
||||||
if (res.compactVideoRenderer)
|
if (res.compactVideoRenderer)
|
||||||
@ -76,7 +76,7 @@ export async function video_basic_info(url: string, cookie?: string) {
|
|||||||
const format = [];
|
const format = [];
|
||||||
const vid = player_response.videoDetails;
|
const vid = player_response.videoDetails;
|
||||||
const microformat = player_response.microformat.playerMicroformatRenderer;
|
const microformat = player_response.microformat.playerMicroformatRenderer;
|
||||||
const video_details = {
|
const video_details = new YouTubeVideo({
|
||||||
id: vid.videoId,
|
id: vid.videoId,
|
||||||
url: `https://www.youtube.com/watch?v=${vid.videoId}`,
|
url: `https://www.youtube.com/watch?v=${vid.videoId}`,
|
||||||
title: vid.title,
|
title: vid.title,
|
||||||
@ -96,7 +96,7 @@ export async function video_basic_info(url: string, cookie?: string) {
|
|||||||
averageRating: vid.averageRating,
|
averageRating: vid.averageRating,
|
||||||
live: vid.isLiveContent,
|
live: vid.isLiveContent,
|
||||||
private: vid.isPrivate
|
private: vid.isPrivate
|
||||||
};
|
});
|
||||||
format.push(...(player_response.streamingData.formats ?? []));
|
format.push(...(player_response.streamingData.formats ?? []));
|
||||||
format.push(...(player_response.streamingData.adaptiveFormats ?? []));
|
format.push(...(player_response.streamingData.adaptiveFormats ?? []));
|
||||||
const LiveStreamData = {
|
const LiveStreamData = {
|
||||||
@ -182,7 +182,7 @@ export async function playlist_info(url: string, parseIncomplete = false) {
|
|||||||
?.runs.pop()?.text ?? null;
|
?.runs.pop()?.text ?? null;
|
||||||
const videosCount = data.stats[0].runs[0].text.replace(/[^0-9]/g, '') || 0;
|
const videosCount = data.stats[0].runs[0].text.replace(/[^0-9]/g, '') || 0;
|
||||||
|
|
||||||
const res = new PlayList({
|
const res = new YouTubePlayList({
|
||||||
continuation: {
|
continuation: {
|
||||||
api: API_KEY,
|
api: API_KEY,
|
||||||
token: getContinuationToken(parsed),
|
token: getContinuationToken(parsed),
|
||||||
@ -217,13 +217,13 @@ export async function playlist_info(url: string, parseIncomplete = false) {
|
|||||||
thumbnail: data.thumbnailRenderer.playlistVideoThumbnailRenderer?.thumbnail.thumbnails.length
|
thumbnail: data.thumbnailRenderer.playlistVideoThumbnailRenderer?.thumbnail.thumbnails.length
|
||||||
? data.thumbnailRenderer.playlistVideoThumbnailRenderer.thumbnail.thumbnails[
|
? data.thumbnailRenderer.playlistVideoThumbnailRenderer.thumbnail.thumbnails[
|
||||||
data.thumbnailRenderer.playlistVideoThumbnailRenderer.thumbnail.thumbnails.length - 1
|
data.thumbnailRenderer.playlistVideoThumbnailRenderer.thumbnail.thumbnails.length - 1
|
||||||
].url
|
]
|
||||||
: null
|
: null
|
||||||
});
|
});
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getPlaylistVideos(data: any, limit = Infinity): Video[] {
|
export function getPlaylistVideos(data: any, limit = Infinity): YouTubeVideo[] {
|
||||||
const videos = [];
|
const videos = [];
|
||||||
|
|
||||||
for (let i = 0; i < data.length; i++) {
|
for (let i = 0; i < data.length; i++) {
|
||||||
@ -232,7 +232,7 @@ export function getPlaylistVideos(data: any, limit = Infinity): Video[] {
|
|||||||
if (!info || !info.shortBylineText) continue;
|
if (!info || !info.shortBylineText) continue;
|
||||||
|
|
||||||
videos.push(
|
videos.push(
|
||||||
new Video({
|
new YouTubeVideo({
|
||||||
id: info.videoId,
|
id: info.videoId,
|
||||||
index: parseInt(info.index?.simpleText) || 0,
|
index: parseInt(info.index?.simpleText) || 0,
|
||||||
duration: parseDuration(info.lengthText?.simpleText) || 0,
|
duration: parseDuration(info.lengthText?.simpleText) || 0,
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import { Video } from '../classes/Video';
|
import { YouTubeVideo } from '../classes/Video';
|
||||||
import { PlayList } from '../classes/Playlist';
|
import { YouTubePlayList } from '../classes/Playlist';
|
||||||
import { Channel } from '../classes/Channel';
|
import { YouTubeChannel } from '../classes/Channel';
|
||||||
|
|
||||||
export interface ParseSearchInterface {
|
export interface ParseSearchInterface {
|
||||||
type?: 'video' | 'playlist' | 'channel';
|
type?: 'video' | 'playlist' | 'channel';
|
||||||
@ -13,7 +13,10 @@ export interface thumbnail {
|
|||||||
url: string;
|
url: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function ParseSearchResult(html: string, options?: ParseSearchInterface): (Video | PlayList | Channel)[] {
|
export function ParseSearchResult(
|
||||||
|
html: string,
|
||||||
|
options?: ParseSearchInterface
|
||||||
|
): (YouTubeVideo | YouTubePlayList | YouTubeChannel)[] {
|
||||||
if (!html) throw new Error("Can't parse Search result without data");
|
if (!html) throw new Error("Can't parse Search result without data");
|
||||||
if (!options) options = { type: 'video', limit: 0 };
|
if (!options) options = { type: 'video', limit: 0 };
|
||||||
if (!options.type) options.type = 'video';
|
if (!options.type) options.type = 'video';
|
||||||
@ -62,14 +65,14 @@ function parseDuration(duration: string): number {
|
|||||||
return dur;
|
return dur;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parseChannel(data?: any): Channel | void {
|
export function parseChannel(data?: any): YouTubeChannel {
|
||||||
if (!data || !data.channelRenderer) return;
|
if (!data || !data.channelRenderer) throw new Error('Failed to Parse YouTube Channel');
|
||||||
const badge = data.channelRenderer.ownerBadges && data.channelRenderer.ownerBadges[0];
|
const badge = data.channelRenderer.ownerBadges && data.channelRenderer.ownerBadges[0];
|
||||||
const url = `https://www.youtube.com${
|
const url = `https://www.youtube.com${
|
||||||
data.channelRenderer.navigationEndpoint.browseEndpoint.canonicalBaseUrl ||
|
data.channelRenderer.navigationEndpoint.browseEndpoint.canonicalBaseUrl ||
|
||||||
data.channelRenderer.navigationEndpoint.commandMetadata.webCommandMetadata.url
|
data.channelRenderer.navigationEndpoint.commandMetadata.webCommandMetadata.url
|
||||||
}`;
|
}`;
|
||||||
const res = new Channel({
|
const res = new YouTubeChannel({
|
||||||
id: data.channelRenderer.channelId,
|
id: data.channelRenderer.channelId,
|
||||||
name: data.channelRenderer.title.simpleText,
|
name: data.channelRenderer.title.simpleText,
|
||||||
icon: {
|
icon: {
|
||||||
@ -91,11 +94,11 @@ export function parseChannel(data?: any): Channel | void {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parseVideo(data?: any): Video | void {
|
export function parseVideo(data?: any): YouTubeVideo {
|
||||||
if (!data || !data.videoRenderer) return;
|
if (!data || !data.videoRenderer) throw new Error('Failed to Parse YouTube Video');
|
||||||
|
|
||||||
const badge = data.videoRenderer.ownerBadges && data.videoRenderer.ownerBadges[0];
|
const badge = data.videoRenderer.ownerBadges && data.videoRenderer.ownerBadges[0];
|
||||||
const res = new Video({
|
const res = new YouTubeVideo({
|
||||||
id: data.videoRenderer.videoId,
|
id: data.videoRenderer.videoId,
|
||||||
url: `https://www.youtube.com/watch?v=${data.videoRenderer.videoId}`,
|
url: `https://www.youtube.com/watch?v=${data.videoRenderer.videoId}`,
|
||||||
title: data.videoRenderer.title.runs[0].text,
|
title: data.videoRenderer.title.runs[0].text,
|
||||||
@ -131,10 +134,10 @@ export function parseVideo(data?: any): Video | void {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parsePlaylist(data?: any): PlayList | void {
|
export function parsePlaylist(data?: any): YouTubePlayList {
|
||||||
if (!data.playlistRenderer) return;
|
if (!data.playlistRenderer) throw new Error('Failed to Parse YouTube Playlist');
|
||||||
|
|
||||||
const res = new PlayList(
|
const res = new YouTubePlayList(
|
||||||
{
|
{
|
||||||
id: data.playlistRenderer.playlistId,
|
id: data.playlistRenderer.playlistId,
|
||||||
title: data.playlistRenderer.title.simpleText,
|
title: data.playlistRenderer.title.simpleText,
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
export { playlist_info, video_basic_info, video_info, yt_validate, extractID } from './YouTube';
|
export { playlist_info, video_basic_info, video_info, yt_validate, extractID, YouTube, YouTubeStream } from './YouTube';
|
||||||
export { spotify, sp_validate, refreshToken, is_expired } from './Spotify';
|
export { spotify, sp_validate, refreshToken, is_expired, Spotify } from './Spotify';
|
||||||
export { soundcloud, so_validate } from './SoundCloud';
|
export { soundcloud, so_validate, SoundCloud, SoundCloudStream } from './SoundCloud';
|
||||||
|
|
||||||
interface SearchOptions {
|
interface SearchOptions {
|
||||||
limit?: number;
|
limit?: number;
|
||||||
@ -13,20 +13,32 @@ interface SearchOptions {
|
|||||||
|
|
||||||
import readline from 'readline';
|
import readline from 'readline';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import { sp_validate, yt_validate, so_validate } from '.';
|
import { sp_validate, yt_validate, so_validate, YouTubeStream, SoundCloudStream } from '.';
|
||||||
import { SpotifyAuthorize, sp_search } from './Spotify';
|
import { SpotifyAuthorize, sp_search } from './Spotify';
|
||||||
import { check_id, so_search, stream as so_stream, stream_from_info as so_stream_info } from './SoundCloud';
|
import { check_id, so_search, stream as so_stream, stream_from_info as so_stream_info } from './SoundCloud';
|
||||||
import { InfoData, stream as yt_stream, StreamOptions, stream_from_info as yt_stream_info } from './YouTube/stream';
|
import { InfoData, stream as yt_stream, StreamOptions, stream_from_info as yt_stream_info } from './YouTube/stream';
|
||||||
import { SoundCloudTrack, Stream as SoStream } from './SoundCloud/classes';
|
import { SoundCloudTrack } from './SoundCloud/classes';
|
||||||
import { LiveStreaming, Stream as YTStream } from './YouTube/classes/LiveStream';
|
|
||||||
import { yt_search } from './YouTube/search';
|
import { yt_search } from './YouTube/search';
|
||||||
|
|
||||||
export async function stream(url: string, options: StreamOptions = {}): Promise<YTStream | LiveStreaming | SoStream> {
|
/**
|
||||||
|
* Main stream Command for streaming through various sources
|
||||||
|
* @param url The video / track url to make stream of
|
||||||
|
* @param options contains quality and cookie to set for stream
|
||||||
|
* @returns YouTube / SoundCloud Stream to play
|
||||||
|
*/
|
||||||
|
|
||||||
|
export async function stream(url: string, options: StreamOptions = {}): Promise<YouTubeStream | SoundCloudStream> {
|
||||||
if (url.length === 0) throw new Error('Stream URL has a length of 0. Check your url again.');
|
if (url.length === 0) throw new Error('Stream URL has a length of 0. Check your url again.');
|
||||||
if (url.indexOf('soundcloud') !== -1) return await so_stream(url, options.quality);
|
if (url.indexOf('soundcloud') !== -1) return await so_stream(url, options.quality);
|
||||||
else return await yt_stream(url, { cookie: options.cookie });
|
else return await yt_stream(url, { cookie: options.cookie });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Main Search Command for searching through various sources
|
||||||
|
* @param query string to search.
|
||||||
|
* @param options contains limit and source to choose.
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
export async function search(query: string, options: SearchOptions = {}) {
|
export async function search(query: string, options: SearchOptions = {}) {
|
||||||
if (!options.source) options.source = { youtube: 'video' };
|
if (!options.source) options.source = { youtube: 'video' };
|
||||||
|
|
||||||
@ -35,25 +47,33 @@ export async function search(query: string, options: SearchOptions = {}) {
|
|||||||
else if (options.source.soundcloud) return await so_search(query, options.source.soundcloud, options.limit);
|
else if (options.source.soundcloud) return await so_search(query, options.source.soundcloud, options.limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param info
|
||||||
|
* @param options
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
export async function stream_from_info(
|
export async function stream_from_info(
|
||||||
info: InfoData | SoundCloudTrack,
|
info: InfoData | SoundCloudTrack,
|
||||||
options: StreamOptions = {}
|
options: StreamOptions = {}
|
||||||
): Promise<YTStream | LiveStreaming | SoStream> {
|
): Promise<YouTubeStream | SoundCloudStream> {
|
||||||
if (info instanceof SoundCloudTrack) return await so_stream_info(info);
|
if (info instanceof SoundCloudTrack) return await so_stream_info(info);
|
||||||
else return await yt_stream_info(info, { cookie: options.cookie });
|
else return await yt_stream_info(info, { cookie: options.cookie });
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function validate(url: string): Promise<"so_playlist" | "so_track" | "sp_track" | "sp_album" | "sp_playlist" | "yt_video" | "yt_playlist" | false> {
|
export async function validate(
|
||||||
|
url: string
|
||||||
|
): Promise<'so_playlist' | 'so_track' | 'sp_track' | 'sp_album' | 'sp_playlist' | 'yt_video' | 'yt_playlist' | false> {
|
||||||
let check;
|
let check;
|
||||||
if (url.indexOf('spotify') !== -1) {
|
if (url.indexOf('spotify') !== -1) {
|
||||||
check = sp_validate(url);
|
check = sp_validate(url);
|
||||||
return check !== false ? 'sp_' + check as "sp_track" | "sp_album" | "sp_playlist" : false;
|
return check !== false ? (('sp_' + check) as 'sp_track' | 'sp_album' | 'sp_playlist') : false;
|
||||||
} else if (url.indexOf('soundcloud') !== -1) {
|
} else if (url.indexOf('soundcloud') !== -1) {
|
||||||
check = await so_validate(url);
|
check = await so_validate(url);
|
||||||
return check !== false ? 'so_' + check as "so_playlist" | "so_track" : false;
|
return check !== false ? (('so_' + check) as 'so_playlist' | 'so_track') : false;
|
||||||
} else {
|
} else {
|
||||||
check = yt_validate(url);
|
check = yt_validate(url);
|
||||||
return check !== false ? 'yt_' + check as "yt_video" | "yt_playlist" : false;
|
return check !== false ? (('yt_' + check) as 'yt_video' | 'yt_playlist') : false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user