SoundCloud Work Completed

This commit is contained in:
killer069
2021-09-20 17:20:15 +05:30
parent 4abe6fbdb0
commit 65026abca1
7 changed files with 143 additions and 65 deletions

View File

@@ -48,7 +48,7 @@ export class SoundCloudTrack {
artist: string;
contains_music: boolean;
writer_composer: string;
};
} | null;
thumbanil: string;
user: SoundCloudUser;
constructor(data: any) {
@@ -59,13 +59,15 @@ export class SoundCloudTrack {
this.type = 'track';
this.durationInSec = Number(data.duration) / 1000;
this.durationInMs = Number(data.duration);
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
};
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;
this.formats = data.media.transcodings;
this.user = {
name: data.user.username,
@@ -131,12 +133,12 @@ export class SoundCloudPlaylist {
this.tracks = tracks;
}
async fetch() {
async fetch(): Promise<void> {
const work: any[] = [];
for (let i = 0; i < this.tracks.length; i++) {
if (!this.tracks[i].fetched) {
work.push(
new Promise(async (resolve, reject) => {
new Promise(async (resolve) => {
const num = i;
const data = await request(
`https://api-v2.soundcloud.com/tracks/${this.tracks[i].id}?client_id=${this.client_id}`
@@ -158,16 +160,20 @@ export class Stream {
private url: string;
private playing_count: number;
private downloaded_time: number;
private request : IncomingMessage | null
private downloaded_segments: number;
private request: IncomingMessage | null;
private data_ended: boolean;
private time: number[];
private segment_urls: string[];
constructor(url: string, type: StreamType = StreamType.Arbitrary, client_id: string) {
constructor(url: string, type: StreamType = StreamType.Arbitrary) {
this.type = type;
this.url = url + client_id;
this.url = url;
this.stream = new PassThrough({ highWaterMark: 10 * 1000 * 1000 });
this.playing_count = 0;
this.downloaded_time = 0;
this.request = null
this.request = null;
this.downloaded_segments = 0;
this.data_ended = false;
this.time = [];
this.segment_urls = [];
this.stream.on('close', () => {
@@ -175,6 +181,13 @@ export class Stream {
});
this.stream.on('pause', () => {
this.playing_count++;
if (this.data_ended) {
this.cleanup();
this.stream.removeAllListeners('pause');
} else if (this.playing_count === 120) {
this.playing_count = 0;
this.start();
}
});
this.start();
}
@@ -200,26 +213,47 @@ export class Stream {
this.cleanup();
return;
}
this.time = [];
this.segment_urls = [];
await this.parser();
for await (const segment of this.segment_urls) {
await new Promise(async (resolve, reject) => {
const stream = await request_stream(segment).catch((err: Error) => err);
if (stream instanceof Error) {
this.stream.emit('error', stream);
reject(stream)
return;
}
this.request = stream
stream.pipe(this.stream, { end : false })
stream.on('end', () => {
resolve('');
});
stream.once('error', (err) => {
this.stream.emit('error', err);
});
});
}
this.downloaded_time = 0;
this.segment_urls.splice(0, this.downloaded_segments);
this.loop();
}
private cleanup() {}
private async loop() {
if (this.stream.destroyed) {
this.cleanup();
return;
}
if (this.time.length === 0 || this.segment_urls.length === 0) {
this.data_ended = true;
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);
if (stream instanceof Error) throw stream;
stream.pipe(this.stream, { end: false });
stream.on('end', () => {
if (this.downloaded_time >= 300) return;
else this.loop();
});
stream.once('error', (err) => {
this.stream.emit('error', err);
});
}
private cleanup() {
this.request?.unpipe(this.stream);
this.request?.destroy();
this.url = '';
this.playing_count = 0;
this.downloaded_time = 0;
this.downloaded_segments = 0;
this.request = null;
this.time = [];
this.segment_urls = [];
}
}

View File

@@ -1,6 +1,7 @@
import fs from 'fs';
import { StreamType } from '../YouTube/stream';
import { request } from '../YouTube/utils/request';
import { SoundCloudPlaylist, SoundCloudTrack } from './classes';
import { SoundCloudPlaylist, SoundCloudTrack, Stream } from './classes';
let soundData: SoundDataOptions;
if (fs.existsSync('.data/soundcloud.data')) {
@@ -32,7 +33,29 @@ export async function soundcloud(url: string): Promise<SoundCloudTrack | SoundCl
else return new SoundCloudPlaylist(json_data, soundData.client_id);
}
export async function check_id(id: string) {
export async function stream(url: string): Promise<Stream> {
const data = await soundcloud(url);
if (data instanceof SoundCloudPlaylist) throw new Error("Streams can't be created from Playlist url");
const req_url = data.formats[data.formats.length - 1].url + '?client_id=' + soundData.client_id;
const s_data = JSON.parse(await request(req_url));
const type = data.formats[data.formats.length - 1].format.mime_type.startsWith('audio/ogg')
? StreamType.OggOpus
: StreamType.Arbitrary;
return new Stream(s_data.url, type);
}
export async function stream_from_info(data: SoundCloudTrack): Promise<Stream> {
const req_url = data.formats[data.formats.length - 1].url + '?client_id=' + soundData.client_id;
const s_data = JSON.parse(await request(req_url));
const type = data.formats[data.formats.length - 1].format.mime_type.startsWith('audio/ogg')
? StreamType.OggOpus
: StreamType.Arbitrary;
return new Stream(s_data.url, type);
}
export async function check_id(id: string): Promise<boolean> {
const response = await request(`https://api-v2.soundcloud.com/search?client_id=${id}&q=Rick+Roll&limit=0`).catch(
(err: Error) => {
return err;
@@ -41,3 +64,16 @@ export async function check_id(id: string) {
if (response instanceof Error) return false;
else return true;
}
export async function so_validate(url: string): Promise<false | 'track' | 'playlist'> {
const data = await request(
`https://api-v2.soundcloud.com/resolve?url=${url}&client_id=${soundData.client_id}`
).catch((err: Error) => err);
if (data instanceof Error) throw data;
const json_data = JSON.parse(data);
if (json_data.kind === 'track') return 'track';
else if (json_data.kind === 'playlist') return 'playlist';
else return false;
}