ariaa
Version:
A CLI for music lovers
90 lines • 3.17 kB
JavaScript
import { Command, logger } from "#lib/structures";
import { getClosestYoutubeTrack, getConfig, getMetadata, getSong, map, save, search } from "#utils";
import { blueBright, greenBright, redBright, underline } from "colorette";
import inquirer from "inquirer";
import ora from "ora";
var song_default = new Command({
description: "Save songs",
flags: [
{
longFlags: "mp3",
description: "save in .mp3 file"
},
{
longFlags: "flac",
description: "save in .flac file"
},
{
longFlags: "raw",
description: "Do not add metadata in the song"
},
{
longFlags: "spotify",
description: "Use Spotify to search"
},
{
longFlags: "yt",
description: "Use YouTube to search"
}
],
args: [{ name: "song", description: "Search query (OPTIONAL)" }],
async run(args, flags) {
const { clientId, clientSecret, preference } = getConfig(true);
const format = flags.mp3 ? "mp3" : flags.flac ? "flac" : void 0;
const provider = flags.yt ? "youtube" : flags.spotify ? "spotify" : preference;
logger.debug(`Using ${provider === "spotify" ? greenBright("Spotify") : redBright("YouTube")} as the provider`);
let songName;
if (args) songName = args;
else {
logger.debug("Song name was not provided!");
songName = (await inquirer.prompt([
{
message: "Please enter name of the song to save:",
name: "songName",
type: "input"
}
])).songName;
}
if (!songName.length) throw new Error("Song Name is mandatory");
if (songName.match(/https:\/\/open\.spotify\.com\/track\/[a-zA-Z0-9]+/)) {
logger.debug("Detected Spotify URI");
const id = songName.split("/").pop().split("?")[0];
const metadata2 = await getSong(id);
const ytsong2 = await getClosestYoutubeTrack(metadata2);
await save(ytsong2, format, metadata2);
return;
}
const spinner = ora("Searching...").start();
let ytSearch;
let spotifySearch;
if (provider === "youtube") {
ytSearch = await search(songName, provider);
} else spotifySearch = await search(songName, provider);
const searches = ytSearch ?? spotifySearch;
logger.debug(`Found ${searches.length} songs!`);
spinner.stop();
if (!searches.length) throw new Error(`No results found, please input better search term!`);
const { url } = await inquirer.prompt([
{
type: "list",
loop: true,
message: "Results found",
name: "url",
choices: map(searches)
}
]);
let metadata = spotifySearch?.find((r) => r.id === url);
const ytsong = ytSearch?.find((r) => r.url === url) ?? await getClosestYoutubeTrack(metadata);
logger.debug(`Found ${underline(blueBright(ytsong.title))}`);
if (clientId.length && clientSecret.length && !flags.raw && provider === "youtube") {
logger.debug("Fetching Spotify details");
metadata = await getMetadata(songName, ytsong);
}
logger.debug("Proceeding to download...");
await save(ytsong, format, metadata);
}
});
export {
song_default as default
};
//# sourceMappingURL=song.js.map