Types Improved

This commit is contained in:
killer069
2021-09-27 22:20:50 +05:30
parent 842d704bf2
commit 70d1774508
12 changed files with 104 additions and 131 deletions

View File

@@ -4,17 +4,18 @@ export interface ChannelIconInterface {
height: number;
}
export class Channel {
export class YouTubeChannel {
name?: string;
verified?: boolean;
id?: string;
type: 'video' | 'playlist' | 'channel';
url?: string;
icon?: ChannelIconInterface;
subscribers?: string;
constructor(data: any) {
if (!data) throw new Error(`Cannot instantiate the ${this.constructor.name} class without data!`);
this.type = 'channel';
this._patch(data);
}
@@ -41,10 +42,6 @@ export class Channel {
return this.icon.url.replace(`=s${def}-c`, `=s${options.size}-c`);
}
get type(): 'channel' {
return 'channel';
}
toString(): string {
return this.name || '';
}

View File

@@ -1,22 +1,27 @@
import { getPlaylistVideos, getContinuationToken } from '../utils/extractor';
import { request } from '../utils/request';
import { Thumbnail } from './Thumbnail';
import { Channel } from './Channel';
import { Video } from './Video';
import { YouTubeChannel } from './Channel';
import { YouTubeVideo } from './Video';
const BASE_API = 'https://www.youtube.com/youtubei/v1/browse?key=';
export class PlayList {
export class YouTubePlayList {
id?: string;
title?: string;
type: 'video' | 'playlist' | 'channel';
videoCount?: number;
lastUpdate?: string;
views?: number;
url?: string;
link?: string;
channel?: Channel;
thumbnail?: Thumbnail;
channel?: YouTubeChannel;
thumbnail?: {
id: string | undefined;
width: number | undefined;
height: number | undefined;
url: string | undefined;
};
private videos?: [];
private fetched_videos: Map<string, Video[]>;
private fetched_videos: Map<string, YouTubeVideo[]>;
private _continuation: {
api?: string;
token?: string;
@@ -28,6 +33,7 @@ export class PlayList {
if (!data) throw new Error(`Cannot instantiate the ${this.constructor.name} class without data!`);
this.__count = 0;
this.fetched_videos = new Map();
this.type = 'playlist';
if (searchResult) this.__patchSearch(data);
else this.__patch(data);
}
@@ -44,7 +50,7 @@ export class PlayList {
this.thumbnail = data.thumbnail || undefined;
this.videos = data.videos || [];
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.token = data.continuation?.token ?? undefined;
this._continuation.clientVersion = data.continuation?.clientVersion ?? '<important data>';
@@ -63,7 +69,7 @@ export class PlayList {
this.views = 0;
}
async next(limit = Infinity): Promise<Video[]> {
async next(limit = Infinity): Promise<YouTubeVideo[]> {
if (!this._continuation || !this._continuation.token) return [];
const nextPage = await request(`${BASE_API}${this._continuation.api}`, {
@@ -109,14 +115,10 @@ export class PlayList {
return this;
}
get type(): 'playlist' {
return 'playlist';
}
page(number: number): Video[] {
page(number: number): YouTubeVideo[] {
if (!number) throw new Error('Page number is not provided');
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() {
@@ -125,7 +127,7 @@ export class PlayList {
get total_videos() {
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() {

View File

@@ -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
};
}
}

View File

@@ -1,5 +1,4 @@
import { Channel } from './Channel';
import { Thumbnail } from './Thumbnail';
import { YouTubeChannel } from './Channel';
interface VideoOptions {
id?: string;
@@ -21,7 +20,6 @@ interface VideoOptions {
id: string;
icon: string;
};
videos?: Video[];
type: string;
ratings: {
likes: number;
@@ -32,9 +30,10 @@ interface VideoOptions {
tags: string[];
}
export class Video {
export class YouTubeVideo {
id?: string;
url?: string;
url: string;
type: 'video' | 'playlist' | 'channel';
title?: string;
description?: string;
durationRaw: string;
@@ -47,8 +46,7 @@ export class Video {
height: number | undefined;
url: string | undefined;
};
channel?: Channel;
videos?: Video[];
channel?: YouTubeChannel;
likes: number;
dislikes: number;
live: boolean;
@@ -60,6 +58,7 @@ export class Video {
this.id = data.id || undefined;
this.url = `https://www.youtube.com/watch?v=${this.id}`;
this.type = 'video';
this.title = data.title || undefined;
this.description = data.description || undefined;
this.durationRaw = data.duration_raw || '0:00';
@@ -75,10 +74,6 @@ export class Video {
this.tags = data.tags || [];
}
get type(): 'video' {
return 'video';
}
get toString(): string {
return this.url || '';
}

View File

@@ -1,2 +1,3 @@
export { stream, stream_from_info } from './stream';
export { stream, stream_from_info, YouTubeStream } from './stream';
export * from './utils';
export { YouTube } from './search';

View File

@@ -1,8 +1,8 @@
import { request } from './utils/request';
import { ParseSearchInterface, ParseSearchResult } from './utils/parser';
import { Video } from './classes/Video';
import { Channel } from './classes/Channel';
import { PlayList } from './classes/Playlist';
import { YouTubeVideo } from './classes/Video';
import { YouTubeChannel } from './classes/Channel';
import { YouTubePlayList } from './classes/Playlist';
enum SearchType {
Video = 'EgIQAQ%253D%253D',
@@ -10,10 +10,9 @@ enum SearchType {
Channel = 'EgIQAg%253D%253D'
}
export async function yt_search(
search: string,
options: ParseSearchInterface = {}
): Promise<(Video | Channel | PlayList)[]> {
export type YouTube = YouTubeVideo | YouTubeChannel | YouTubePlayList;
export async function yt_search(search: string, options: ParseSearchInterface = {}): Promise<YouTube[]> {
let url = 'https://www.youtube.com/results?search_query=' + search.replaceAll(' ', '+');
options.type ??= 'video';
if (!url.match('&sp=')) {

View File

@@ -38,13 +38,15 @@ function parseAudioFormats(formats: any[]) {
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 final: any[] = [];
if (
info.LiveStreamData.isLive === true &&
info.LiveStreamData.hlsManifestUrl !== null &&
info.video_details.durationInSec === '0'
info.video_details.durationInSec === 0
) {
return new LiveStreaming(
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[] = [];
if (
info.LiveStreamData.isLive === true &&

View File

@@ -1,7 +1,7 @@
import { request } from './request';
import { format_decipher } from './cipher';
import { Video } from '../classes/Video';
import { PlayList } from '../classes/Playlist';
import { YouTubeVideo } from '../classes/Video';
import { YouTubePlayList } from '../classes/Playlist';
const DEFAULT_API_KEY = 'AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8';
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
?.owner?.videoOwnerRenderer?.badges[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(
(res: any) => {
if (res.compactVideoRenderer)
@@ -76,7 +76,7 @@ export async function video_basic_info(url: string, cookie?: string) {
const format = [];
const vid = player_response.videoDetails;
const microformat = player_response.microformat.playerMicroformatRenderer;
const video_details = {
const video_details = new YouTubeVideo({
id: vid.videoId,
url: `https://www.youtube.com/watch?v=${vid.videoId}`,
title: vid.title,
@@ -96,7 +96,7 @@ export async function video_basic_info(url: string, cookie?: string) {
averageRating: vid.averageRating,
live: vid.isLiveContent,
private: vid.isPrivate
};
});
format.push(...(player_response.streamingData.formats ?? []));
format.push(...(player_response.streamingData.adaptiveFormats ?? []));
const LiveStreamData = {
@@ -182,7 +182,7 @@ export async function playlist_info(url: string, parseIncomplete = false) {
?.runs.pop()?.text ?? null;
const videosCount = data.stats[0].runs[0].text.replace(/[^0-9]/g, '') || 0;
const res = new PlayList({
const res = new YouTubePlayList({
continuation: {
api: API_KEY,
token: getContinuationToken(parsed),
@@ -217,13 +217,13 @@ export async function playlist_info(url: string, parseIncomplete = false) {
thumbnail: data.thumbnailRenderer.playlistVideoThumbnailRenderer?.thumbnail.thumbnails.length
? data.thumbnailRenderer.playlistVideoThumbnailRenderer.thumbnail.thumbnails[
data.thumbnailRenderer.playlistVideoThumbnailRenderer.thumbnail.thumbnails.length - 1
].url
]
: null
});
return res;
}
export function getPlaylistVideos(data: any, limit = Infinity): Video[] {
export function getPlaylistVideos(data: any, limit = Infinity): YouTubeVideo[] {
const videos = [];
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;
videos.push(
new Video({
new YouTubeVideo({
id: info.videoId,
index: parseInt(info.index?.simpleText) || 0,
duration: parseDuration(info.lengthText?.simpleText) || 0,

View File

@@ -1,6 +1,6 @@
import { Video } from '../classes/Video';
import { PlayList } from '../classes/Playlist';
import { Channel } from '../classes/Channel';
import { YouTubeVideo } from '../classes/Video';
import { YouTubePlayList } from '../classes/Playlist';
import { YouTubeChannel } from '../classes/Channel';
export interface ParseSearchInterface {
type?: 'video' | 'playlist' | 'channel';
@@ -13,7 +13,10 @@ export interface thumbnail {
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 (!options) options = { type: 'video', limit: 0 };
if (!options.type) options.type = 'video';
@@ -62,14 +65,14 @@ function parseDuration(duration: string): number {
return dur;
}
export function parseChannel(data?: any): Channel | void {
if (!data || !data.channelRenderer) return;
export function parseChannel(data?: any): YouTubeChannel {
if (!data || !data.channelRenderer) throw new Error('Failed to Parse YouTube Channel');
const badge = data.channelRenderer.ownerBadges && data.channelRenderer.ownerBadges[0];
const url = `https://www.youtube.com${
data.channelRenderer.navigationEndpoint.browseEndpoint.canonicalBaseUrl ||
data.channelRenderer.navigationEndpoint.commandMetadata.webCommandMetadata.url
}`;
const res = new Channel({
const res = new YouTubeChannel({
id: data.channelRenderer.channelId,
name: data.channelRenderer.title.simpleText,
icon: {
@@ -91,11 +94,11 @@ export function parseChannel(data?: any): Channel | void {
return res;
}
export function parseVideo(data?: any): Video | void {
if (!data || !data.videoRenderer) return;
export function parseVideo(data?: any): YouTubeVideo {
if (!data || !data.videoRenderer) throw new Error('Failed to Parse YouTube Video');
const badge = data.videoRenderer.ownerBadges && data.videoRenderer.ownerBadges[0];
const res = new Video({
const res = new YouTubeVideo({
id: data.videoRenderer.videoId,
url: `https://www.youtube.com/watch?v=${data.videoRenderer.videoId}`,
title: data.videoRenderer.title.runs[0].text,
@@ -131,10 +134,10 @@ export function parseVideo(data?: any): Video | void {
return res;
}
export function parsePlaylist(data?: any): PlayList | void {
if (!data.playlistRenderer) return;
export function parsePlaylist(data?: any): YouTubePlayList {
if (!data.playlistRenderer) throw new Error('Failed to Parse YouTube Playlist');
const res = new PlayList(
const res = new YouTubePlayList(
{
id: data.playlistRenderer.playlistId,
title: data.playlistRenderer.title.simpleText,