Files
play-dl-test/play-dl/SoundCloud/classes.ts

531 lines
14 KiB
TypeScript
Raw Normal View History

2021-10-11 18:38:08 +05:30
import { request, request_stream } from '../Request';
2021-10-28 19:40:19 +02:00
import { Readable } from 'node:stream';
import { IncomingMessage } from 'node:http';
2021-09-17 14:36:32 +05:30
import { StreamType } from '../YouTube/stream';
2021-10-05 18:47:09 +05:30
import { Timer } from '../YouTube/classes/LiveStream';
2021-11-19 13:49:21 +05:30
import { PlaylistJSON, SoundTrackJSON } from './constants';
2021-09-17 14:36:32 +05:30
2021-11-18 17:41:23 +05:30
export interface SoundCloudUser {
/**
* SoundCloud User Name
*/
2021-09-17 14:36:32 +05:30
name: string;
2021-11-18 17:41:23 +05:30
/**
* SoundCloud User ID
*/
2021-09-17 14:36:32 +05:30
id: string;
2021-11-18 17:41:23 +05:30
/**
* SoundCloud User URL
*/
2021-09-17 14:36:32 +05:30
url: string;
2021-11-18 17:41:23 +05:30
/**
* SoundCloud Class type. == "user"
*/
2021-09-17 14:36:32 +05:30
type: 'track' | 'playlist' | 'user';
2021-11-18 17:41:23 +05:30
/**
* SoundCloud User Verified status
*/
2021-09-17 14:36:32 +05:30
verified: boolean;
2021-11-18 17:41:23 +05:30
/**
* SoundCloud User Description
*/
2021-09-17 14:36:32 +05:30
description: string;
2021-11-18 17:41:23 +05:30
/**
* SoundCloud User First Name
*/
2021-09-17 14:36:32 +05:30
first_name: string;
2021-11-18 17:41:23 +05:30
/**
* SoundCloud User Full Name
*/
2021-09-17 14:36:32 +05:30
full_name: string;
2021-11-18 17:41:23 +05:30
/**
* SoundCloud User Last Name
*/
2021-09-17 14:36:32 +05:30
last_name: string;
2021-11-18 17:41:23 +05:30
/**
* SoundCloud User thumbnail URL
*/
2021-09-17 14:36:32 +05:30
thumbnail: string;
}
2021-11-18 17:41:23 +05:30
export interface SoundCloudTrackDeprecated {
/**
* SoundCloud Track fetched status
*/
2021-09-17 14:36:32 +05:30
fetched: boolean;
2021-11-18 17:41:23 +05:30
/**
* SoundCloud Track ID
*/
2021-09-17 14:36:32 +05:30
id: number;
2021-11-18 17:41:23 +05:30
/**
* SoundCloud Class type. == "track"
*/
2021-09-17 14:36:32 +05:30
type: 'track';
}
2021-09-24 12:49:39 +05:30
export interface SoundCloudTrackFormat {
2021-11-18 17:41:23 +05:30
/**
* SoundCloud Track Format Url
*/
2021-09-17 14:36:32 +05:30
url: string;
2021-11-18 17:41:23 +05:30
/**
* SoundCloud Track Format preset
*/
2021-09-17 14:36:32 +05:30
preset: string;
2021-11-18 17:41:23 +05:30
/**
* SoundCloud Track Format Duration
*/
2021-09-17 14:36:32 +05:30
duration: number;
2021-11-18 17:41:23 +05:30
/**
* SoundCloud Track Format data containing protocol and mime_type
*/
2021-09-17 14:36:32 +05:30
format: {
protocol: string;
mime_type: string;
};
2021-11-18 17:41:23 +05:30
/**
* SoundCloud Track Format quality
*/
2021-09-17 14:36:32 +05:30
quality: string;
}
2021-09-29 20:23:16 +05:30
/**
2021-11-18 17:41:23 +05:30
* SoundCloud Track Class
2021-09-29 20:23:16 +05:30
*/
2021-09-17 14:36:32 +05:30
export class SoundCloudTrack {
2021-11-18 17:41:23 +05:30
/**
* SoundCloud Track Name
*/
2021-09-17 14:36:32 +05:30
name: string;
2021-11-18 17:41:23 +05:30
/**
* SoundCloud Track ID
*/
2021-09-17 14:36:32 +05:30
id: number;
2021-11-18 17:41:23 +05:30
/**
* SoundCloud Track url
*/
2021-09-17 14:36:32 +05:30
url: string;
2021-11-18 17:41:23 +05:30
/**
* SoundCloud Track fetched status
*/
2021-09-17 14:36:32 +05:30
fetched: boolean;
2021-11-18 17:41:23 +05:30
/**
* SoundCloud Class type. === "track"
*/
2021-09-17 14:36:32 +05:30
type: 'track' | 'playlist' | 'user';
2021-11-18 17:41:23 +05:30
/**
* SoundCloud Track Duration in seconds
*/
2021-09-17 14:36:32 +05:30
durationInSec: number;
2021-11-18 17:41:23 +05:30
/**
* SoundCloud Track Duration in miili seconds
*/
2021-09-17 14:36:32 +05:30
durationInMs: number;
2021-11-18 17:41:23 +05:30
/**
* SoundCloud Track formats data
*/
2021-09-17 14:36:32 +05:30
formats: SoundCloudTrackFormat[];
2021-11-18 17:41:23 +05:30
/**
* SoundCloud Track Publisher Data
*/
2021-09-17 14:36:32 +05:30
publisher: {
name: string;
id: number;
artist: string;
contains_music: boolean;
writer_composer: string;
2021-09-20 17:20:15 +05:30
} | null;
2021-11-18 17:41:23 +05:30
/**
* SoundCloud Track thumbnail
*/
2021-09-21 13:00:38 +05:30
thumbnail: string;
2021-11-18 17:41:23 +05:30
/**
* SoundCloud Track user data
*/
2021-09-17 14:36:32 +05:30
user: SoundCloudUser;
2021-11-18 17:41:23 +05:30
/**
* Constructor for SoundCloud Track Class
* @param data JSON parsed track html data
*/
2021-09-17 14:36:32 +05:30
constructor(data: any) {
this.name = data.title;
this.id = data.id;
this.url = data.uri;
this.fetched = true;
this.type = 'track';
2021-10-13 20:51:33 +05:30
this.durationInSec = Math.round(Number(data.duration) / 1000);
2021-09-17 14:36:32 +05:30
this.durationInMs = Number(data.duration);
2021-09-20 17:20:15 +05:30
if (data.publisher_metadata)
this.publisher = {
name: data.publisher_metadata.publisher,
id: data.publisher_metadata.id,
artist: data.publisher_metadata.artist,
contains_music: Boolean(data.publisher_metadata.contains_music) || false,
writer_composer: data.publisher_metadata.writer_composer
};
else this.publisher = null;
2021-09-17 14:36:32 +05:30
this.formats = data.media.transcodings;
this.user = {
name: data.user.username,
id: data.user.id,
type: 'user',
url: data.user.permalink_url,
verified: Boolean(data.user.verified) || false,
description: data.user.description,
first_name: data.user.first_name,
full_name: data.user.full_name,
last_name: data.user.last_name,
thumbnail: data.user.avatar_url
};
2021-09-21 13:00:38 +05:30
this.thumbnail = data.artwork_url;
}
2021-11-19 13:49:21 +05:30
/**
* Converts class to JSON
* @returns JSON parsed Data
*/
2021-11-23 09:56:08 +05:30
toJSON(): SoundTrackJSON {
2021-09-21 13:00:38 +05:30
return {
name: this.name,
id: this.id,
url: this.url,
2021-09-24 12:49:39 +05:30
fetched: this.fetched,
2021-09-21 13:00:38 +05:30
durationInMs: this.durationInMs,
durationInSec: this.durationInSec,
publisher: this.publisher,
formats: this.formats,
thumbnail: this.thumbnail,
2021-09-24 12:49:39 +05:30
user: this.user
2021-09-21 13:00:38 +05:30
};
2021-09-17 14:36:32 +05:30
}
}
2021-09-29 20:23:16 +05:30
/**
2021-11-18 17:41:23 +05:30
* SoundCloud Playlist Class
2021-09-29 20:23:16 +05:30
*/
2021-09-17 14:36:32 +05:30
export class SoundCloudPlaylist {
2021-11-19 13:49:21 +05:30
/**
* SoundCloud Playlist Name
*/
2021-09-17 14:36:32 +05:30
name: string;
2021-11-19 13:49:21 +05:30
/**
* SoundCloud Playlist ID
*/
2021-09-17 14:36:32 +05:30
id: number;
2021-11-19 13:49:21 +05:30
/**
* SoundCloud Playlist URL
*/
2021-09-17 14:36:32 +05:30
url: string;
2021-11-19 13:49:21 +05:30
/**
* SoundCloud Class type. == "playlist"
*/
2021-09-17 14:36:32 +05:30
type: 'track' | 'playlist' | 'user';
2021-11-19 13:49:21 +05:30
/**
* SoundCloud Playlist Sub type. == "album" for soundcloud albums
*/
2021-09-17 14:36:32 +05:30
sub_type: string;
2021-11-19 13:49:21 +05:30
/**
* SoundCloud Playlist Total Duration in seconds
*/
2021-09-17 14:36:32 +05:30
durationInSec: number;
2021-11-19 13:49:21 +05:30
/**
* SoundCloud Playlist Total Duration in milli seconds
*/
2021-09-17 14:36:32 +05:30
durationInMs: number;
2021-11-19 13:49:21 +05:30
/**
* SoundCloud Playlist user data
*/
2021-09-17 14:36:32 +05:30
user: SoundCloudUser;
2021-11-19 13:49:21 +05:30
/**
* SoundCloud Playlist tracks [ It can be fetched or not fetched ]
*/
2021-09-17 14:36:32 +05:30
tracks: SoundCloudTrack[] | SoundCloudTrackDeprecated[];
2021-11-19 13:49:21 +05:30
/**
* SoundCloud Playlist tracks number
*/
2021-09-17 14:36:32 +05:30
tracksCount: number;
2021-11-19 13:49:21 +05:30
/**
* SoundCloud Client ID provided by user
* @private
*/
private client_id: string;
/**
* Constructor for SoundCloud Playlist
* @param data JSON parsed SoundCloud playlist data
* @param client_id Provided SoundCloud Client ID
*/
2021-09-17 14:36:32 +05:30
constructor(data: any, client_id: string) {
this.name = data.title;
this.id = data.id;
this.url = data.uri;
this.client_id = client_id;
this.type = 'playlist';
this.sub_type = data.set_type;
2021-10-13 20:51:33 +05:30
this.durationInSec = Math.round(Number(data.duration) / 1000);
2021-09-17 14:36:32 +05:30
this.durationInMs = Number(data.duration);
this.user = {
name: data.user.username,
id: data.user.id,
type: 'user',
url: data.user.permalink_url,
verified: Boolean(data.user.verified) || false,
description: data.user.description,
first_name: data.user.first_name,
full_name: data.user.full_name,
last_name: data.user.last_name,
thumbnail: data.user.avatar_url
};
this.tracksCount = data.track_count;
const tracks: any[] = [];
data.tracks.forEach((track: any) => {
if (track.title) {
tracks.push(new SoundCloudTrack(track));
} else
tracks.push({
id: track.id,
fetched: false,
type: 'track'
});
});
this.tracks = tracks;
}
2021-11-19 13:49:21 +05:30
/**
* Fetches all unfetched songs in a playlist.
2021-11-23 09:56:08 +05:30
*
2021-11-19 13:49:21 +05:30
* For fetching songs and getting all songs, see `fetched_tracks` property.
* @returns playlist class
*/
async fetch(): Promise<SoundCloudPlaylist> {
2021-09-17 14:36:32 +05:30
const work: any[] = [];
for (let i = 0; i < this.tracks.length; i++) {
if (!this.tracks[i].fetched) {
work.push(
2021-09-20 17:20:15 +05:30
new Promise(async (resolve) => {
2021-09-17 14:36:32 +05:30
const num = i;
const data = await request(
`https://api-v2.soundcloud.com/tracks/${this.tracks[i].id}?client_id=${this.client_id}`
);
this.tracks[num] = new SoundCloudTrack(JSON.parse(data));
resolve('');
})
);
}
}
await Promise.allSettled(work);
2021-11-19 13:49:21 +05:30
return this;
2021-09-17 14:36:32 +05:30
}
2021-11-19 13:49:21 +05:30
/**
* Get total no. of fetched tracks
* @see {@link SoundCloudPlaylist.all_tracks}
2021-11-19 13:49:21 +05:30
*/
get total_tracks(): number {
2021-09-24 12:49:39 +05:30
let count = 0;
2021-09-21 13:00:38 +05:30
this.tracks.forEach((track) => {
2021-09-24 12:49:39 +05:30
if (track instanceof SoundCloudTrack) count++;
else return;
});
return count;
2021-09-21 13:00:38 +05:30
}
2021-11-19 13:49:21 +05:30
/**
* Fetches all the tracks in the playlist and returns them
*
2021-11-19 13:49:21 +05:30
* ```ts
* const playlist = await play.soundcloud('playlist url')
*
* const tracks = await playlist.all_tracks()
2021-11-19 13:49:21 +05:30
* ```
* @returns An array of {@link SoundCloudTrack}
2021-11-19 13:49:21 +05:30
*/
async all_tracks(): Promise<SoundCloudTrack[]> {
await this.fetch()
return this.tracks as SoundCloudTrack[]
2021-11-19 13:49:21 +05:30
}
/**
* Converts Class to JSON data
* @returns JSON parsed data
*/
toJSON(): PlaylistJSON {
2021-09-21 13:00:38 +05:30
return {
name: this.name,
id: this.id,
2021-09-24 12:49:39 +05:30
sub_type: this.sub_type,
2021-09-21 13:00:38 +05:30
url: this.url,
durationInMs: this.durationInMs,
durationInSec: this.durationInSec,
2021-09-24 12:49:39 +05:30
tracksCount: this.tracksCount,
user: this.user,
tracks: this.tracks
2021-09-21 13:00:38 +05:30
};
}
2021-09-17 14:36:32 +05:30
}
2021-09-29 20:23:16 +05:30
/**
* SoundCloud Stream class
*/
2021-11-18 15:48:33 +05:30
export class SoundCloudStream {
2021-11-19 13:49:21 +05:30
/**
* Readable Stream through which data passes
*/
2021-10-11 17:29:10 +05:30
stream: Readable;
2021-11-23 09:56:08 +05:30
/**
* Type of audio data that we recieved from normal youtube url.
*/
2021-09-17 14:36:32 +05:30
type: StreamType;
2021-11-19 13:49:21 +05:30
/**
* Dash Url containing segment urls.
* @private
*/
2021-09-17 14:36:32 +05:30
private url: string;
2021-11-19 13:49:21 +05:30
/**
* Total time of downloaded segments data.
* @private
*/
2021-09-17 14:36:32 +05:30
private downloaded_time: number;
2021-11-19 13:49:21 +05:30
/**
* Timer for looping code every 5 minutes
* @private
*/
2021-10-05 18:47:09 +05:30
private timer: Timer;
2021-11-19 13:49:21 +05:30
/**
* Total segments Downloaded so far
* @private
*/
2021-09-20 17:20:15 +05:30
private downloaded_segments: number;
2021-11-19 13:49:21 +05:30
/**
* Incoming message that we recieve.
2021-11-23 09:56:08 +05:30
*
2021-11-19 13:49:21 +05:30
* Storing this is essential.
* This helps to destroy the TCP connection completely if you stopped player in between the stream
* @private
*/
2021-09-20 17:20:15 +05:30
private request: IncomingMessage | null;
2021-11-19 13:49:21 +05:30
/**
* Array of segment time. Useful for calculating downloaded_time.
*/
2021-09-17 14:36:32 +05:30
private time: number[];
2021-11-19 13:49:21 +05:30
/**
* Array of segment_urls in dash file.
*/
2021-09-17 14:36:32 +05:30
private segment_urls: string[];
2021-11-19 13:49:21 +05:30
/**
* Constructor for SoundCloud Stream
* @param url Dash url containing dash file.
* @param type Stream Type
*/
2021-09-20 17:20:15 +05:30
constructor(url: string, type: StreamType = StreamType.Arbitrary) {
2021-11-19 13:49:21 +05:30
this.stream = new Readable({ highWaterMark: 5 * 1000 * 1000, read() {} });
2021-09-17 14:36:32 +05:30
this.type = type;
2021-09-20 17:20:15 +05:30
this.url = url;
2021-09-17 14:36:32 +05:30
this.downloaded_time = 0;
2021-09-20 17:20:15 +05:30
this.request = null;
this.downloaded_segments = 0;
2021-09-17 14:36:32 +05:30
this.time = [];
2021-10-05 18:47:09 +05:30
this.timer = new Timer(() => {
this.timer.reuse();
this.start();
}, 280);
2021-09-17 14:36:32 +05:30
this.segment_urls = [];
2021-09-21 22:04:45 +05:30
this.stream.on('close', () => {
2021-09-17 14:36:32 +05:30
this.cleanup();
});
this.start();
}
2021-11-19 13:49:21 +05:30
/**
* Parses SoundCloud dash file.
* @private
*/
2021-09-17 14:36:32 +05:30
private async parser() {
const response = await request(this.url).catch((err: Error) => {
return err;
});
if (response instanceof Error) throw response;
const array = response.split('\n');
array.forEach((val) => {
if (val.startsWith('#EXTINF:')) {
this.time.push(parseFloat(val.replace('#EXTINF:', '')));
} else if (val.startsWith('https')) {
this.segment_urls.push(val);
}
});
return;
}
2021-11-19 13:49:21 +05:30
/**
* Starts looping of code for getting all segments urls data
*/
2021-09-17 14:36:32 +05:30
private async start() {
2021-09-21 22:04:45 +05:30
if (this.stream.destroyed) {
2021-09-17 14:36:32 +05:30
this.cleanup();
return;
}
2021-09-20 17:20:15 +05:30
this.time = [];
this.segment_urls = [];
this.downloaded_time = 0;
2021-11-19 13:49:21 +05:30
await this.parser();
2021-09-20 17:20:15 +05:30
this.segment_urls.splice(0, this.downloaded_segments);
this.loop();
}
2021-11-19 13:49:21 +05:30
/**
* Main Loop function for getting all segments urls data
*/
2021-09-20 17:20:15 +05:30
private async loop() {
2021-11-07 16:00:56 +05:30
if (this.stream.destroyed) {
this.cleanup();
return;
}
2021-11-07 16:01:49 +05:30
if (this.time.length === 0 || this.segment_urls.length === 0) {
2021-09-20 17:20:15 +05:30
this.cleanup();
2021-11-07 16:00:56 +05:30
this.stream.push(null);
2021-09-20 17:20:15 +05:30
return;
}
this.downloaded_time += this.time.shift() as number;
this.downloaded_segments++;
const stream = await request_stream(this.segment_urls.shift() as string).catch((err: Error) => err);
2021-10-05 18:47:09 +05:30
if (stream instanceof Error) {
this.stream.emit('error', stream);
this.cleanup();
return;
}
2021-09-20 17:20:15 +05:30
2021-10-09 16:18:05 +05:30
this.request = stream;
2021-10-11 17:29:10 +05:30
stream.on('data', (c) => {
2021-10-12 14:09:14 +05:30
this.stream.push(c);
});
2021-09-20 17:20:15 +05:30
stream.on('end', () => {
if (this.downloaded_time >= 300) return;
else this.loop();
});
stream.once('error', (err) => {
2021-09-21 22:04:45 +05:30
this.stream.emit('error', err);
2021-09-20 17:20:15 +05:30
});
2021-09-17 14:36:32 +05:30
}
2021-11-19 13:49:21 +05:30
/**
* This cleans every used variable in class.
2021-11-23 09:56:08 +05:30
*
2021-11-19 13:49:21 +05:30
* This is used to prevent re-use of this class and helping garbage collector to collect it.
*/
2021-09-20 17:20:15 +05:30
private cleanup() {
2021-10-05 18:47:09 +05:30
this.timer.destroy();
2021-09-20 17:20:15 +05:30
this.request?.destroy();
this.url = '';
this.downloaded_time = 0;
this.downloaded_segments = 0;
this.request = null;
this.time = [];
this.segment_urls = [];
}
2021-11-19 13:49:21 +05:30
/**
* Pauses timer.
* Stops running of loop.
2021-11-23 09:56:08 +05:30
*
2021-11-19 13:49:21 +05:30
* Useful if you don't want to get excess data to be stored in stream.
*/
2021-10-05 18:47:09 +05:30
pause() {
this.timer.pause();
}
2021-11-19 13:49:21 +05:30
/**
* Resumes timer.
* Starts running of loop.
*/
2021-10-05 18:47:09 +05:30
resume() {
this.timer.resume();
}
2021-09-17 14:36:32 +05:30
}