Merge pull request #206 from absidue/performance-optimisations

Performance improvements and cleanup
This commit is contained in:
Killer069 2021-12-17 15:04:50 +05:30 committed by GitHub
commit 3f2bfcf3c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 25 deletions

View File

@ -4,7 +4,7 @@ import { request } from '../Request';
import { SoundCloudPlaylist, SoundCloudTrack, SoundCloudTrackFormat, SoundCloudStream } from './classes'; import { SoundCloudPlaylist, SoundCloudTrack, SoundCloudTrackFormat, SoundCloudStream } from './classes';
let soundData: SoundDataOptions; let soundData: SoundDataOptions;
if (existsSync('.data/soundcloud.data')) { if (existsSync('.data/soundcloud.data')) {
soundData = JSON.parse(readFileSync('.data/soundcloud.data').toString()); soundData = JSON.parse(readFileSync('.data/soundcloud.data', 'utf-8'));
} }
interface SoundDataOptions { interface SoundDataOptions {
@ -29,7 +29,7 @@ const pattern = /^(?:(https?):\/\/)?(?:(?:www|m)\.)?(api\.soundcloud\.com|soundc
* @returns A {@link SoundCloudTrack} or {@link SoundCloudPlaylist} * @returns A {@link SoundCloudTrack} or {@link SoundCloudPlaylist}
*/ */
export async function soundcloud(url: string): Promise<SoundCloud> { export async function soundcloud(url: string): Promise<SoundCloud> {
if (!soundData) throw new Error('SoundCloud Data is missing\nDid you forgot to do authorization ?'); if (!soundData) throw new Error('SoundCloud Data is missing\nDid you forget to do authorization ?');
if (!url.match(pattern)) throw new Error('This is not a SoundCloud URL'); if (!url.match(pattern)) throw new Error('This is not a SoundCloud URL');
const data = await request( const data = await request(
@ -82,7 +82,7 @@ export async function so_search(
export async function stream(url: string, quality?: number): Promise<SoundCloudStream> { export async function stream(url: string, quality?: number): Promise<SoundCloudStream> {
const data = await soundcloud(url); const data = await soundcloud(url);
if (data instanceof SoundCloudPlaylist) throw new Error("Streams can't be created from Playlist url"); if (data instanceof SoundCloudPlaylist) throw new Error("Streams can't be created from playlist urls");
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;

View File

@ -4,7 +4,7 @@ import { existsSync, readFileSync, writeFileSync } from 'node:fs';
let spotifyData: SpotifyDataOptions; let spotifyData: SpotifyDataOptions;
if (existsSync('.data/spotify.data')) { if (existsSync('.data/spotify.data')) {
spotifyData = JSON.parse(readFileSync('.data/spotify.data').toString()); spotifyData = JSON.parse(readFileSync('.data/spotify.data', 'utf-8'));
spotifyData.file = true; spotifyData.file = true;
} }
/** /**
@ -174,7 +174,7 @@ export async function sp_search(
limit: number = 10 limit: number = 10
): Promise<Spotify[]> { ): Promise<Spotify[]> {
const results: Spotify[] = []; const results: Spotify[] = [];
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 forget 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.');
if (limit > 50 || limit < 0) throw new Error(`You crossed limit range of Spotify [ 0 - 50 ]`); if (limit > 50 || limit < 0) throw new Error(`You crossed limit range of Spotify [ 0 - 50 ]`);
const response = await request( const response = await request(

View File

@ -2,7 +2,7 @@ import { existsSync, readFileSync, writeFileSync } from 'node:fs';
let youtubeData: youtubeDataOptions; let youtubeData: youtubeDataOptions;
if (existsSync('.data/youtube.data')) { if (existsSync('.data/youtube.data')) {
youtubeData = JSON.parse(readFileSync('.data/youtube.data').toString()); youtubeData = JSON.parse(readFileSync('.data/youtube.data', 'utf-8'));
youtubeData.file = true; youtubeData.file = true;
} }

View File

@ -105,7 +105,7 @@ export async function video_basic_info(url: string, options: InfoOptions = {}):
body = url; body = url;
} else { } else {
if (yt_validate(url) !== 'video') throw new Error('This is not a YouTube Watch URL'); if (yt_validate(url) !== 'video') throw new Error('This is not a YouTube Watch URL');
const video_id: string = extractID(url); const video_id = extractID(url);
const new_url = `https://www.youtube.com/watch?v=${video_id}&has_verified=1`; const new_url = `https://www.youtube.com/watch?v=${video_id}&has_verified=1`;
body = await request(new_url, { body = await request(new_url, {
headers: { headers: {
@ -138,7 +138,7 @@ export async function video_basic_info(url: string, options: InfoOptions = {}):
const ownerInfo = const ownerInfo =
initial_response.contents.twoColumnWatchNextResults.results?.results?.contents[1]?.videoSecondaryInfoRenderer initial_response.contents.twoColumnWatchNextResults.results?.results?.contents[1]?.videoSecondaryInfoRenderer
?.owner?.videoOwnerRenderer; ?.owner?.videoOwnerRenderer;
const badge = ownerInfo?.badges && ownerInfo?.badges[0]; const badge = ownerInfo?.badges?.[0]?.metadataBadgeRenderer?.style?.toLowerCase();
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: string[] = []; const related: string[] = [];
initial_response.contents.twoColumnWatchNextResults.secondaryResults.secondaryResults.results.forEach( initial_response.contents.twoColumnWatchNextResults.secondaryResults.secondaryResults.results.forEach(
@ -147,40 +147,38 @@ export async function video_basic_info(url: string, options: InfoOptions = {}):
related.push(`https://www.youtube.com/watch?v=${res.compactVideoRenderer.videoId}`); related.push(`https://www.youtube.com/watch?v=${res.compactVideoRenderer.videoId}`);
} }
); );
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 ratingButtons =
initial_response.contents.twoColumnWatchNextResults.results.results.contents.find(
(content: any) => content.videoPrimaryInfoRenderer
)?.videoPrimaryInfoRenderer.videoActions.menuRenderer.topLevelButtons ?? [];
const video_details = new YouTubeVideo({ const video_details = new YouTubeVideo({
id: vid.videoId, id: vid.videoId,
title: vid.title, title: vid.title,
description: vid.shortDescription, description: vid.shortDescription,
duration: Number(vid.lengthSeconds), duration: Number(vid.lengthSeconds),
duration_raw: parseSeconds(Number(vid.lengthSeconds)), duration_raw: parseSeconds(vid.lengthSeconds),
uploadedAt: microformat.publishDate, uploadedAt: microformat.publishDate,
thumbnails: vid.thumbnail.thumbnails, thumbnails: vid.thumbnail.thumbnails,
channel: { channel: {
name: vid.author, name: vid.author,
id: vid.channelId, id: vid.channelId,
url: `https://www.youtube.com/channel/${vid.channelId}`, url: `https://www.youtube.com/channel/${vid.channelId}`,
verified: Boolean(badge?.metadataBadgeRenderer?.style?.toLowerCase().includes('verified')), verified: Boolean(badge?.includes('verified')),
artist: Boolean(badge?.metadataBadgeRenderer?.style?.toLowerCase().includes('artist')), artist: Boolean(badge?.includes('artist')),
icons: ownerInfo?.thumbnail?.thumbnails || undefined icons: ownerInfo?.thumbnail?.thumbnails || undefined
}, },
views: vid.viewCount, views: vid.viewCount,
tags: vid.keywords, tags: vid.keywords,
likes: parseInt( likes: parseInt(
ratingButtons initial_response.contents.twoColumnWatchNextResults.results.results.contents
.find((button: any) => button.toggleButtonRenderer.defaultIcon.iconType === 'LIKE') .find((content: any) => content.videoPrimaryInfoRenderer)
?.videoPrimaryInfoRenderer.videoActions.menuRenderer.topLevelButtons?.find(
(button: any) => button.toggleButtonRenderer.defaultIcon.iconType === 'LIKE'
)
?.toggleButtonRenderer.defaultText.accessibility?.accessibilityData.label.replace(/\D+/g, '') ?? 0 ?.toggleButtonRenderer.defaultText.accessibility?.accessibilityData.label.replace(/\D+/g, '') ?? 0
), ),
live: vid.isLiveContent, live: vid.isLiveContent,
private: vid.isPrivate private: vid.isPrivate
}); });
format.push(...(player_response.streamingData.formats ?? [])); const format = player_response.streamingData.formats ?? [];
format.push(...(player_response.streamingData.adaptiveFormats ?? [])); format.push(...(player_response.streamingData.adaptiveFormats ?? []));
const LiveStreamData = { const LiveStreamData = {
isLive: video_details.live, isLive: video_details.live,
@ -212,7 +210,7 @@ export async function video_stream_info(url: string, options: InfoOptions = {}):
body = url; body = url;
} else { } else {
if (yt_validate(url) !== 'video') throw new Error('This is not a YouTube Watch URL'); if (yt_validate(url) !== 'video') throw new Error('This is not a YouTube Watch URL');
const video_id: string = extractID(url); const video_id = extractID(url);
const new_url = `https://www.youtube.com/watch?v=${video_id}&has_verified=1`; const new_url = `https://www.youtube.com/watch?v=${video_id}&has_verified=1`;
body = await request(new_url, { body = await request(new_url, {
headers: { 'accept-language': 'en-US,en;q=0.9' }, headers: { 'accept-language': 'en-US,en;q=0.9' },
@ -240,8 +238,7 @@ export async function video_stream_info(url: string, options: InfoOptions = {}):
url: `https://www.youtube.com/watch?v=${player_response.videoDetails.videoId}`, url: `https://www.youtube.com/watch?v=${player_response.videoDetails.videoId}`,
durationInSec: (duration < 0 ? 0 : duration) || 0 durationInSec: (duration < 0 ? 0 : duration) || 0
}; };
const format = []; const format = 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 = {

View File

@ -121,6 +121,7 @@ export function parseVideo(data?: any): YouTubeVideo {
const channel = data.videoRenderer.ownerText.runs[0]; const channel = data.videoRenderer.ownerText.runs[0];
const badge = data.videoRenderer.ownerBadges?.[0]?.metadataBadgeRenderer?.style?.toLowerCase(); const badge = data.videoRenderer.ownerBadges?.[0]?.metadataBadgeRenderer?.style?.toLowerCase();
const durationText = data.videoRenderer.lengthText;
const res = new YouTubeVideo({ 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}`,
@ -128,8 +129,8 @@ export function parseVideo(data?: any): YouTubeVideo {
description: data.videoRenderer.detailedMetadataSnippets?.[0].snippetText.runs.length description: data.videoRenderer.detailedMetadataSnippets?.[0].snippetText.runs.length
? data.videoRenderer.detailedMetadataSnippets[0].snippetText.runs.map((run: any) => run.text).join('') ? data.videoRenderer.detailedMetadataSnippets[0].snippetText.runs.map((run: any) => run.text).join('')
: '', : '',
duration: data.videoRenderer.lengthText ? parseDuration(data.videoRenderer.lengthText.simpleText) : 0, duration: durationText ? parseDuration(durationText.simpleText) : 0,
duration_raw: data.videoRenderer.lengthText ? data.videoRenderer.lengthText.simpleText : null, duration_raw: durationText ? durationText.simpleText : null,
thumbnails: data.videoRenderer.thumbnail.thumbnails, thumbnails: data.videoRenderer.thumbnail.thumbnails,
channel: { channel: {
id: channel.navigationEndpoint.browseEndpoint.browseId || null, id: channel.navigationEndpoint.browseEndpoint.browseId || null,
@ -145,7 +146,7 @@ export function parseVideo(data?: any): YouTubeVideo {
}, },
uploadedAt: data.videoRenderer.publishedTimeText?.simpleText ?? null, uploadedAt: data.videoRenderer.publishedTimeText?.simpleText ?? null,
views: data.videoRenderer.viewCountText?.simpleText?.replace(/[^0-9]/g, '') ?? 0, views: data.videoRenderer.viewCountText?.simpleText?.replace(/[^0-9]/g, '') ?? 0,
live: data.videoRenderer.lengthText ? false : true live: durationText ? false : true
}); });
return res; return res;