Compare commits

...

3 Commits

2 changed files with 45 additions and 11 deletions

View File

@@ -1,5 +1,6 @@
import axios from "axios";
import fs from "node:fs";
import path from "node:path";
export async function download_image_from_caa(mbid: string) {
console.log("[DEBUG]: download_image_from_caa:", mbid);
@@ -14,7 +15,14 @@ export async function download_image_from_caa(mbid: string) {
},
);
fs.writeFileSync(`public/covers/mb/${mbid}.png`, img_response.data);
const mb_dist = path.join(
process.cwd(),
"public",
"covers",
"mb",
`${mbid}.png`,
);
fs.writeFileSync(mb_dist, img_response.data);
} catch (error) {
console.error("Error fetching data:", error);
throw error;
@@ -32,14 +40,20 @@ export async function itunes_cover_url(artist: string, album: string) {
.replace(/[-_,"':;|]/g, " ")
.toLowerCase();
const apple_music_results = await fetch("https://itunes.apple.com/search", {
const response = await fetch("https://itunes.apple.com/search", {
body: new URLSearchParams({
term: query,
entity: "song",
limit: "10",
}),
method: "POST",
}).then((res) => res.json());
});
if (!response.ok) {
throw new Error(`iTunes API failed with status ${response.status}`);
}
const apple_music_results = await response.json();
for (let result of apple_music_results.results) {
if (artist == result.artistName.toLowerCase()) {
@@ -62,16 +76,22 @@ export async function download_image_from_itunes(
throw new Error(`iTunes URL not found for ${rawArtist} - ${rawAlbum}`);
}
const artist_dir = `public/covers/itunes/${safeArtist}`;
if (!fs.existsSync(artist_dir)) {
fs.mkdirSync(artist_dir, { recursive: true });
const artist_dist = path.join(
process.cwd(),
"public",
"covers",
"itunes",
safeArtist,
);
if (!fs.existsSync(artist_dist)) {
fs.mkdirSync(artist_dist, { recursive: true });
}
const img_response = await axios.get(itunes_url, {
responseType: "arraybuffer",
});
fs.writeFileSync(`${artist_dir}/${safeAlbum}.png`, img_response.data);
fs.writeFileSync(`${artist_dist}/${safeAlbum}.png`, img_response.data);
} catch (error) {
console.error("Error fetching data:", error);
throw error;

View File

@@ -1,6 +1,7 @@
import { Elysia } from "elysia";
import { cors } from "@elysiajs/cors";
import { node } from "@elysiajs/node";
import path from "node:path";
import fs from "node:fs";
@@ -18,7 +19,7 @@ app.get("/cover/:artist/:album/:id", ({ params: { artist, album, id } }) =>
get_image_from_server(artist, album, id),
);
app.listen(3000, ({ hostname, port }) => {
app.listen({ hostname: "0.0.0.0", port: 3000 }, ({ hostname, port }) => {
console.log(`🦊 Elysia is running at ${hostname}:${port}`);
});
@@ -35,8 +36,21 @@ async function get_image_from_server(
.trim();
const safeMbid = mbid.replace(/[^a-zA-Z0-9\-]/g, "");
const MBFilePath = `public/covers/mb/${safeMbid}.png`;
const iTunesFilePath = `public/covers/itunes/${safeArtist}/${safeAlbum}.png`;
const MBFilePath = path.join(
process.cwd(),
"public",
"covers",
"mb",
`${safeMbid}.png`,
);
const iTunesFilePath = path.join(
process.cwd(),
"public",
"covers",
"itunes",
safeArtist,
`${safeAlbum}.png`,
);
let resultFilePath = MBFilePath;
@@ -60,7 +74,7 @@ async function get_image_from_server(
first_error,
secong_error,
);
throw Error("cover not found");
return new Response("Cover not found", { status: 404 });
}
}
}