ariaa
Version:
A CLI for music lovers
91 lines • 3.42 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
import { assignKey, getGenre } from "#utils";
import { fetch, request } from "undici";
import { YouTube } from "youtube-sr";
import { getConfig } from "./config.js";
const BASE = `https://api.spotify.com/v1`;
async function authorize() {
const { clientId, clientSecret } = getConfig(true);
const res = await request(`https://accounts.spotify.com/api/token`, {
headers: {
Authorization: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString("base64")}`,
"Content-Type": "application/x-www-form-urlencoded"
},
body: "grant_type=client_credentials",
// json: true,
method: "POST"
});
return (await res.body.json()).access_token;
}
__name(authorize, "authorize");
async function headers() {
return {
Authorization: `Bearer ${await authorize()}`,
"Content-Type": "application/json"
};
}
__name(headers, "headers");
async function searchSpotify(query, type = "track", limit = 3) {
const res = await fetch(`${BASE}/search?q=${encodeURI(query)}&type=${type}&limit=${limit}`, { headers: await headers() });
if (type === "track") {
const tracks = await res.json();
for (const song of tracks.tracks.items) {
const features = await getAudioFeatures(song.id);
song.genre = getGenre((await getArtist(song.artists[0].id)).genres) ?? "Unknown";
song.key = assignKey(features.key);
song.tempo = Math.round(features.tempo);
}
return tracks;
}
return res.json();
}
__name(searchSpotify, "searchSpotify");
async function getAudioFeatures(id) {
const res = await fetch(`${BASE}/audio-features/${id}`, { headers: await headers() });
return res.json();
}
__name(getAudioFeatures, "getAudioFeatures");
async function getArtist(id) {
const res = await fetch(`${BASE}/artists/${id}`, { headers: await headers() });
return res.json();
}
__name(getArtist, "getArtist");
async function getAlbum(id) {
const res = await fetch(`${BASE}/albums/${id}`, { headers: await headers() });
return res.json();
}
__name(getAlbum, "getAlbum");
async function getClosestYoutubeTrack(song) {
const results = await YouTube.search(`${song.name} ${song.artists[0].name} lyrics`, { limit: 20, type: "video" });
const videos = results.filter(
(s) => s.duration && s.title && song.name.toLowerCase().includes("remix") === s.title.toLowerCase().includes("remix")
);
const times = videos.map((r) => r.duration);
const closest = times.reduce((prev, curr) => Math.abs(curr - song.duration_ms) < Math.abs(prev - song.duration_ms) ? curr : prev);
const result = videos.find((s) => s.duration === closest);
if (result) return result;
throw new Error("No similar videos found");
}
__name(getClosestYoutubeTrack, "getClosestYoutubeTrack");
async function getSong(id) {
const res = await fetch(`${BASE}/tracks/${id}`, { headers: await headers() });
const song = await res.json();
const features = await getAudioFeatures(id);
song.genre = getGenre((await getArtist(song.artists[0].id)).genres) ?? "Unknown";
song.key = assignKey(features.key);
song.tempo = Math.round(features.tempo);
return song;
}
__name(getSong, "getSong");
export {
authorize,
getAlbum,
getArtist,
getAudioFeatures,
getClosestYoutubeTrack,
getSong,
headers,
searchSpotify
};
//# sourceMappingURL=spotify.js.map