@jadestudios/discord-music-player
Version:
Complete framework to facilitate music commands using discord.js v13
117 lines (116 loc) • 4.02 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getApple = exports.AppleTrackList = exports.AppleTrack = exports.AppleLinkType = void 0;
const axios_1 = __importDefault(require("axios"));
const cheerio = __importStar(require("cheerio"));
/**
* Written to replace apple-music-metadata
*/
var AppleLinkType;
(function (AppleLinkType) {
AppleLinkType[AppleLinkType["Song"] = 0] = "Song";
AppleLinkType[AppleLinkType["Album"] = 1] = "Album";
})(AppleLinkType || (exports.AppleLinkType = AppleLinkType = {}));
/**
* Minimal class of what a track should contain
*/
class AppleTrack {
constructor(artist, title) {
this.artist = artist;
this.title = title;
}
}
exports.AppleTrack = AppleTrack;
/**
* Minimal class of what a Playlist should contain
*/
class AppleTrackList {
constructor() {
this.tracks = [];
this.trackCount = 0;
}
/**
* addTrack
*/
addTrack(track) {
this.tracks.push(track);
this.trackCount = this.tracks.length;
}
}
exports.AppleTrackList = AppleTrackList;
/**
* Expects a valid apple url
* @param url
* @param lt: AppleLinkType from caller
*/
async function getApple(url, lt) {
const page = await axios_1.default.get(url).then((res) => res.data).catch(() => undefined);
if (!page)
return undefined;
const $ = cheerio.load(page);
const scripts = $("script").toArray();
let script_data = Object();
/**
* Finds all script tags -> finds embedded data -> parse from child
*/
for (let index = 0; index < scripts.length; index++) {
const script = scripts[index];
if (script.attribs['id'] == 'serialized-server-data') {
const script_child = script?.children;
for (let j = 0; j < script_child.length; j++) {
const t3 = script_child[j];
if (typeof (t3['data']) == 'string') {
script_data = JSON.parse(t3['data']);
break;
}
}
break;
}
}
const trackList = new AppleTrackList();
/**
* From parsed JSON -> check all for track list -> create track for each track in JSON list
*/
for (let i = 0; i < script_data[0]['data']['sections'].length; i++) {
const sections = script_data[0]['data']['sections'][i];
if (sections['id'].includes('track-list - ')) {
sections['items'].forEach((items) => {
trackList.addTrack(new AppleTrack(items['artistName'], items['title']));
});
}
}
if (trackList.trackCount > 0) {
if (lt == AppleLinkType.Song)
return trackList.tracks[0];
if (lt == AppleLinkType.Album)
return trackList;
}
return undefined;
}
exports.getApple = getApple;