329 lines
6.4 KiB
JavaScript
329 lines
6.4 KiB
JavaScript
// @ts-check
|
|
|
|
import fs from 'fs';
|
|
import https from 'https';
|
|
import chalk from 'chalk';
|
|
import {config} from 'dotenv';
|
|
config()
|
|
|
|
let songCount = 1;
|
|
|
|
const AUTHTOKEN = process.env.AUTHTOKEN;
|
|
|
|
if (!AUTHTOKEN) {
|
|
console.error('Please enter all the required information');
|
|
process.exit(1);
|
|
}
|
|
|
|
/** @typedef {("focus" | "relax" | "sleep" | "meditate")} mentalState */
|
|
|
|
/** @type {mentalState[]} */
|
|
let mentalStates = ["focus", "relax", "sleep", "meditate"]
|
|
|
|
/**
|
|
* @type {Record<mentalState, {base: string[], nature: string[]}>}
|
|
*/
|
|
let genres = {
|
|
"focus": {
|
|
base: [
|
|
"Acoustic",
|
|
"Atmospheric",
|
|
"Cinematic",
|
|
"Classical",
|
|
"Drone",
|
|
"Electronic",
|
|
"Grooves",
|
|
"Lofi",
|
|
"Piano",
|
|
"Post Rock"
|
|
],
|
|
nature: [
|
|
"Beach",
|
|
"Chimes & Bowls",
|
|
"Forest",
|
|
"Nightsounds",
|
|
"Rain",
|
|
"Rainforest",
|
|
"River",
|
|
"Thunder",
|
|
"Underwater",
|
|
"Wind"
|
|
]
|
|
},
|
|
"relax": {
|
|
base: [
|
|
"Atmospheric",
|
|
"Electronic"
|
|
],
|
|
nature: [
|
|
"Beach",
|
|
"Chimes & Bowls",
|
|
"Forest",
|
|
"Nightsounds",
|
|
"Rain",
|
|
"Rainforest",
|
|
"River",
|
|
"Thunder",
|
|
"Underwater",
|
|
"Wind"
|
|
]
|
|
},
|
|
"sleep": {
|
|
base: [
|
|
"Atmospheric"
|
|
],
|
|
nature: [
|
|
"Beach",
|
|
"Forest",
|
|
"Nightsounds",
|
|
"Rain",
|
|
"Rainforest",
|
|
"River",
|
|
"Thunder",
|
|
"Underwater",
|
|
"Wind"
|
|
]
|
|
},
|
|
"meditate": {
|
|
base: [
|
|
"Atmospheric",
|
|
"Electronic"
|
|
],
|
|
nature: [
|
|
"Beach",
|
|
"Chimes & Bowls",
|
|
"Forest",
|
|
"Nightsounds",
|
|
"Rain",
|
|
"Rainforest",
|
|
"River",
|
|
"Thunder",
|
|
"Underwater",
|
|
"Wind"
|
|
]
|
|
}
|
|
};
|
|
|
|
// @ts-ignore
|
|
let moods = {
|
|
"focus": [
|
|
"Brooding",
|
|
"Calm",
|
|
"Chill",
|
|
"Dark",
|
|
"Downtempo",
|
|
"Dreamlike",
|
|
"Driving",
|
|
"Energizing",
|
|
"Epic",
|
|
"Floating",
|
|
"Heavy",
|
|
"Hopeful",
|
|
"Inspiring",
|
|
"Meditative",
|
|
"Mysterious",
|
|
"Ominous",
|
|
"Optimistic",
|
|
"Playful",
|
|
"Ponderous",
|
|
"Serene",
|
|
"Strong",
|
|
"Upbeat",
|
|
"Uplifting"
|
|
],
|
|
"relax": [
|
|
"Brooding",
|
|
"Calm",
|
|
"Chill",
|
|
"Dark",
|
|
"Downtempo",
|
|
"Dreamlike",
|
|
"Driving",
|
|
"Energizing",
|
|
"Epic",
|
|
"Floating",
|
|
"Hopeful",
|
|
"Inspiring",
|
|
"Meditative",
|
|
"Mysterious",
|
|
"Optimistic",
|
|
"Playful",
|
|
"Ponderous",
|
|
"Serene",
|
|
"Strong",
|
|
"Upbeat",
|
|
"Uplifting"
|
|
],
|
|
"sleep": [
|
|
"Brooding",
|
|
"Calm",
|
|
"Chill",
|
|
"Dark",
|
|
"Dreamlike",
|
|
"Epic",
|
|
"Floating",
|
|
"Heavy",
|
|
"Meditative",
|
|
"Mysterious",
|
|
"Optimistic",
|
|
"Ponderous",
|
|
"Serene",
|
|
"Strong"
|
|
],
|
|
"meditate": [
|
|
"Brooding",
|
|
"Calm",
|
|
"Chill",
|
|
"Dark",
|
|
"Downtempo",
|
|
"Dreamlike",
|
|
"Driving",
|
|
"Energizing",
|
|
"Epic",
|
|
"Floating",
|
|
"Heavy",
|
|
"Hopeful",
|
|
"Inspiring",
|
|
"Meditative",
|
|
"Mysterious",
|
|
"Optimistic",
|
|
"Playful",
|
|
"Ponderous",
|
|
"Serene",
|
|
"Strong",
|
|
"Upbeat",
|
|
"Uplifting"
|
|
]
|
|
}
|
|
|
|
/**
|
|
* @typedef {{url: string, folder: string, filename: string}} QueueEntry
|
|
*/
|
|
|
|
class DownloadQueue {
|
|
/** @param {number} maxConcurrency */
|
|
constructor(maxConcurrency) {
|
|
/** @type {QueueEntry[]} */
|
|
this.queue = [];
|
|
this.activeDownloads = 0;
|
|
this.maxConcurrency = maxConcurrency;
|
|
}
|
|
|
|
|
|
/**
|
|
* @param {string} url
|
|
* @param {string} folder
|
|
* @param {string} filename
|
|
*/
|
|
enqueue(url, folder, filename) {
|
|
this.queue.push({ url, folder, filename });
|
|
this.processQueue();
|
|
}
|
|
|
|
async processQueue() {
|
|
if (this.activeDownloads < this.maxConcurrency && this.queue.length > 0) {
|
|
// @ts-ignore
|
|
const { url, folder, filename } = this.queue.shift();
|
|
this.activeDownloads++;
|
|
await downloadSong(url, folder, filename);
|
|
console.log(`${songCount} songs downloaded successfully \n${this.queue.length} remaining`)
|
|
this.activeDownloads--;
|
|
this.processQueue();
|
|
}
|
|
}
|
|
}
|
|
|
|
const downloadQueue = new DownloadQueue(3)
|
|
|
|
for (const mentalState of mentalStates) {
|
|
console.log(chalk.red(`Starting mental state ${mentalState}`))
|
|
for (const genre of [...genres[mentalState].base, ...genres[mentalState].nature]) {
|
|
console.log(chalk.yellow(`Starting genre ${genre}`))
|
|
|
|
//
|
|
// Phase 1 : Fetch all song data
|
|
//
|
|
let data = await fetch(`https://api.brain.fm/v3/servings/search?genre=${genre}&dynamicMentalStateId=${mentalState}`, {
|
|
headers: {
|
|
authorization: `Bearer ${AUTHTOKEN}`
|
|
}
|
|
});
|
|
data = await data.json();
|
|
|
|
// @ts-ignore
|
|
data = formatAudioData(data.result);
|
|
|
|
if (checkIfJsonExists(`./json-data/${mentalState}/${genre}.json`)) continue;
|
|
ensureDirectory(`./json-data/${mentalState}`);
|
|
let file = fs.createWriteStream(`./json-data/${mentalState}/${genre}.json`);
|
|
|
|
file.write(JSON.stringify(data));
|
|
|
|
file.close();
|
|
|
|
//
|
|
// Phase 2 : Download songs to device
|
|
//
|
|
|
|
console.log(chalk.green(`Started downloading ${genre} ${mentalState}`))
|
|
// @ts-ignore
|
|
for (const song of data) {
|
|
// @ts-ignore
|
|
let activity = song.track.tags.filter(x => x.type == 'activity').map(x => x.value).join('/');
|
|
let NEL = song.trackVariation.neuralEffectLevel;
|
|
let level = (NEL > 0.66 ? "high" : NEL > 0.33 ? "medium" : "low");
|
|
|
|
let folder = `./songs/${genre}/${mentalState}/${activity}/${level}`
|
|
let filename = song.trackVariation.baseUrl;
|
|
let downloadLink = song.trackVariation.tokenedUrl;
|
|
|
|
downloadQueue.enqueue(downloadLink, folder, filename);
|
|
}
|
|
|
|
}
|
|
};
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
async function downloadSong(downloadLink, folder, filename) {
|
|
return new Promise((resolve, reject) => {
|
|
ensureDirectory(folder)
|
|
https.get(downloadLink, (response) => {
|
|
response.pipe(fs.createWriteStream(`${folder}/${filename}.mp3`))
|
|
.on('finish', () => {
|
|
songCount++
|
|
// @ts-ignore
|
|
resolve();
|
|
})
|
|
.on('error', (error) => {
|
|
console.error('Error downloading song:', error);
|
|
reject(error);
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
// @ts-ignore
|
|
function formatAudioData(arr) {
|
|
// @ts-ignore
|
|
return arr.map(item => {
|
|
delete item.track.similarTracks;
|
|
return item;
|
|
});
|
|
}
|
|
|
|
// @ts-ignore
|
|
function ensureDirectory(directory) {
|
|
if (!fs.existsSync(directory)) {
|
|
fs.mkdirSync(directory, { recursive: true });
|
|
}
|
|
}
|
|
|
|
// @ts-ignore
|
|
function checkIfJsonExists(mentalState, genre) {
|
|
const filePath = `./json-data/${mentalState}/${genre}.json`;
|
|
return fs.existsSync(filePath);
|
|
}
|
|
|