playdl-music-extractor
Version:
PlayDL Music Extractor is a Extractor/Scrapper and Helps Players to fetch data from play-dl or Custom Extractors , as Per reduces extra work and credentials
128 lines (112 loc) • 2.9 kB
JavaScript
const { EventEmitter } = require('events');
class queue extends EventEmitter {
static __caches = {};
#raw = { rawQuery: undefined, options: undefined, playdl: undefined };
constructor(rawQuery, __scrapperOptions, playdl) {
super();
this.#raw = {
totalTracks: 0,
rawQuery,
options: __scrapperOptions,
status: 0,
};
this.id = this.generateId();
this.creationTime = Date.now();
this.album = false;
this.tracks = new Map();
this.totalTracks = 0;
this.destroyed = false;
this.stopped = false;
this.completedTracks = 0;
this.playdl = playdl;
}
destroy() {
this.destroyed = true;
this.stopped = true;
this.complete();
this.tracks.clear();
this.tracks = null;
this.album = null;
delete this.tracks;
delete this.album;
this.filter();
return true;
}
stop() {
this.stopped = true;
this.complete();
this.filter();
return true;
}
complete(reverse = false) {
this.#raw = { ...this.#raw, status: reverse ? 0 : 1 };
if (this.completed) this.filter();
return true;
}
add(rawMetadata, type = 'track') {
if (
!(
type
&& typeof type === 'string'
&& type?.trim() !== ''
&& rawMetadata
&& !this.destroyed
&& !this.completed
)
) {
return undefined;
}
switch (type?.toLowerCase()?.trim()) {
case 'track':
if (!(rawMetadata && rawMetadata?.url && rawMetadata?.uniqueId)) return undefined;
++this.completedTracks;
this.tracks.set(rawMetadata?.uniqueId, rawMetadata);
break;
case 'album':
this.album = rawMetadata;
if (this.album?.id) this.totalTracks = this.album?.tracksCount;
this.emit('album', rawMetadata);
this.playdl.emit('album', rawMetadata, this, rawMetadata?.metadata);
break;
case 'trackscount':
this.totalTracks = parseInt(rawMetadata) ?? this.totalTracks;
if (this.album?.id) this.album.tracksCount = this.totalTracks;
break;
default:
return undefined;
}
return true;
}
filter() {
if (!this.tracks?.values()?.[0]) return undefined;
if (this.album?.id) {
this.album.tracksCount = this.tracks?.size;
this.totalTracks = this.album?.tracksCount;
}
return true;
}
generateId() {
let requestedId = '';
while (
this.playdl?.queues?.has(requestedId?.trim())
|| requestedId?.trim() === ''
) requestedId = Math.random().toString(36).slice(2, 15);
return requestedId;
}
get completed() {
return Boolean(this.raw?.status);
}
get tracksCount() {
return this.tracks?.size;
}
get rawQuery() {
return this.raw?.rawQuery;
}
get options() {
return this.raw?.options;
}
get raw() {
return this.#raw;
}
}
module.exports = queue;