music: no logic yet just soundcloud client id grabbing

This commit is contained in:
Cynthia Foxwell 2021-09-15 21:45:55 -06:00
parent a066ab27c1
commit 4ab7307ba7

46
src/modules/music.js Normal file
View file

@ -0,0 +1,46 @@
const fetch = require("node-fetch");
const ytdl = require("ytdl-core");
const Eris = require("eris");
const REGEX_YOUTUBE = /^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.?be)\/.+$/;
const REGEX_YOUTUBE_PLAYLIST =
/^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.?be)\/playlist\?list=(.+)$/;
const REGEX_YOUTUBE_PLAYLIST_SHORT = /^PL[a-zA-Z0-9-_]{1,32}$/;
const REGEX_SOUNDCLOUD =
/^((https?:\/\/)?(www\.|m\.)?soundcloud\.com\/|sc:).+\/.+$/;
const REGEX_SOUNDCLOUD_PLAYLIST =
/^((https?:\/\/)?(www\.|m\.)?soundcloud\.com\/|sc:).+\/(sets\/.+|likes|tracks)$/;
const REGEX_FILE =
/^(https?:\/\/)?.*\..*\/.+\.(mp3|ogg|flac|wav|webm|mp4|mov|mkv)$/;
let SOUNDCLOUD_CLIENTID;
hf.voiceStorage = hf.voiceStorage || new Eris.Collection();
// https://stackoverflow.com/a/12646864 § "Updating to ES6 / ECMAScript 2015"
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
async function getSoundcloudClientID() {
if (SOUNDCLOUD_CLIENTID != null) {
return SOUNDCLOUD_CLIENTID;
}
const page = await fetch("https://soundcloud.com").then((res) => res.text());
const scripts = page
.match(/<script crossorigin src="(.+?)"><\/script>/g)
.reverse();
for (const script of scripts) {
const url = script.match(/src="(.+?)"/)[1];
const contents = await fetch(url).then((res) => res.text());
if (/,client_id:"(.+?)",/.test(contents)) {
const client_id = contents.match(/,client_id:"(.+?)",/)[1];
SOUNDCLOUD_CLIENTID = client_id;
return SOUNDCLOUD_CLIENTID;
}
}
return null;
}