forked from
tokono.ma/diffuse
A music player that connects to your cloud/distributed storage.
1import { ostiary, rpc } from "~/common/worker.js";
2
3/**
4 * @import {Actions} from "~/components/artwork/types.d.ts"
5 */
6
7////////////////////////////////////////////
8// ACTIONS
9////////////////////////////////////////////
10
11/**
12 * @type {Actions['get']}
13 */
14export async function get(track) {
15 if (!navigator.onLine) return null;
16
17 const query = track.tags?.artist;
18 if (!query) return null;
19
20 return await fetch(
21 `https://ws.audioscrobbler.com/2.0/?method=album.search&album=${query}&api_key=4f0fe85b67baef8bb7d008a8754a95e5&format=json`,
22 )
23 .then((r) => r.json())
24 .then((r) => findCover(r.results.albummatches.album))
25 .catch((err) => {
26 console.error(err);
27 return null;
28 });
29}
30
31////////////////////////////////////////////
32// ⚡️
33////////////////////////////////////////////
34
35ostiary((context) => {
36 rpc(context, { get });
37});
38
39////////////////////////////////////////////
40// 🛠️
41////////////////////////////////////////////
42
43/**
44 * @param {any[]} remainingMatches
45 * @returns {Promise<Uint8Array | null>}
46 */
47async function findCover(remainingMatches) {
48 const album = remainingMatches[0];
49 const url = album ? album.image[album.image.length - 1]["#text"] : null;
50
51 return url && url !== ""
52 ? await fetch(url)
53 .then((r) => r.blob())
54 .then(async (b) => new Uint8Array(await b.arrayBuffer()))
55 .catch(() => findCover(remainingMatches.slice(1)))
56 : album
57 ? findCover(remainingMatches.slice(1))
58 : null;
59}