const fs = require("node:fs");
var exec = require("child_process").exec;
console.log("Writing some Dot Dot");
// Check to see if the -f argument is present
const strict = process.argv.indexOf("-s") > -1 ? true : false;
// Checks for --custom and if it has a value
const artistIndex = process.argv.indexOf("-a");
let artistValue;
var args = process.argv.slice(2);
console.log(args)
if (artistIndex > -1) {
// Retrieve the value after --custom
artistValue = process.argv[artistIndex + 1];
} else {
throw Error("No Artist");
}
const artist = artistValue || "Mark Almond";
// Checks for --custom and if it has a value
const titleIndex = process.argv.indexOf("-t");
let titleValue;
if (titleIndex > -1) {
// Retrieve the value after --custom
titleValue = process.argv[titleIndex + 1];
} else {
throw Error("No Title");
}
const title = titleValue || "What Am I Living For";
console.log("strict:", `${strict}`);
console.log("title:", `${title}`);
console.log("artist:", `${artist}`);
// Run Commands
async function runCMD(cmd) {
return new Promise(function (resolve, reject) {
exec(cmd, (err, stdout, stderr) => {
if (err) {
reject(err);
} else {
resolve({ stdout, stderr });
}
});
});
}
const getLyrics = async (title, artist) => {
const respone = await fetch(`https://lyrist.vercel.app/api/${title}/${artist}`);
let object = await respone.json();
if (object.lyrics) {
return object;
}
throw Error("Lyric Fetch Failed");
};
const splitWords = (lyrics) => {
lyrics = lyrics.replace(/ *\[[^\]]*]/g, "");
const splitOne = lyrics.split(/[ ,]+/);
let splitWords = [];
splitOne.map((word) => {
let thisWord = word.split(/\r?\n|\r|\n/g);
splitWords = [...splitWords, ...thisWord];
});
return splitWords.filter((n) => n);
};
const createGraphViz = async () => {
const songData = await getLyrics(title, artist);
const songLyrics = songData.lyrics;
const songArtist = songData.artist;
const songTitle = songData.title;
console.log("Found lryics for " + songArtist + " " + songTitle);
let words = splitWords(songLyrics);
let string = strict ? "strict" : "";
string += ` digraph {
ratio=0.7;
pad=0.5;
`;
words.map((word, i) => {
word = word.replace('"', "`");
word = word.replace(")", "");
word = word.replace("(", "");
word = word.replace(`"`, `'`);
word = word.toLowerCase();
string += '"' + word + '"';
if (i != words.length - 1) {
string += " -> ";
}
});
string += `
labelloc="t";
fontsize = 50
label="${songArtist} - ${songTitle}";}`;
let filename = songArtist.replace(/ /g, "_") + "---" + songTitle.replace(/ /g, "_");
fs.writeFile(`${filename}.dot`, string, (err) => {
if (err) {
console.error(err);
} else {
runCMD(`dot ${filename}.dot -Tpng -O -n`);
runCMD(`dot ${filename}.dot -Tsvg -O -n`);
}
});
};
createGraphViz();