Add album art page for uploading, retrying and correcting missing art
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QHPkt2v797pphGCFKfTFmv
This commit is contained in:
1 parent
bee83cb9f3
commit
9c84a64d4e
6 files changed
+226
-4
No files matched your search
@@ -0,0 +1,22 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Album art · Scrobbler</title>
|
||||
<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Ccircle cx='8' cy='8' r='7' fill='%231c2330'/%3E%3Ccircle cx='8' cy='8' r='3.5' fill='%23f2a541'/%3E%3C/svg%3E">
|
||||
<link rel="stylesheet" href="css/style.css">
|
||||
<script src="config.js"></script>
|
||||
<script type="module" src="js/art-failures.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<header class="site-header" id="site-header"></header>
|
||||
<main id="main">
|
||||
<h1 class="page-title">Album art</h1>
|
||||
<p class="lede" id="art-summary">Albums you've scrobbled that have no cover art.</p>
|
||||
<div id="messages" aria-live="polite"></div>
|
||||
<ul class="rows art-rows" id="albums"></ul>
|
||||
<nav class="pager" aria-label="Pages" id="pager"></nav>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1149,3 +1149,24 @@ input {
|
||||
height: 2.25rem;
|
||||
align-self: center; /* .log li items default to stretch/start, which suits text, not art */
|
||||
}
|
||||
|
||||
/* ---- Album art: fixing missing art ---------------------------------------------- */
|
||||
.art-row {
|
||||
display: grid;
|
||||
grid-template-columns: 4rem minmax(0, 1fr);
|
||||
gap: 1rem;
|
||||
align-items: start;
|
||||
}
|
||||
|
||||
.art-thumb-lg {
|
||||
width: 4rem;
|
||||
height: 4rem;
|
||||
}
|
||||
|
||||
.art-row-body .muted {
|
||||
margin: 0.35rem 0 0.75rem;
|
||||
}
|
||||
|
||||
.art-correction {
|
||||
margin-top: 1rem;
|
||||
}
|
||||
+3
-3
@@ -102,7 +102,7 @@ export async function api(path, { method = "GET", body, auth = true } = {}) {
|
||||
|
||||
// Turn webargs validation details into a single readable sentence.
|
||||
export function describeError(error) {
|
||||
const fields = error.details?.json ?? error.details?.query;
|
||||
const fields = error.details?.json ?? error.details?.query ?? error.details?.files;
|
||||
if (fields) {
|
||||
return Object.entries(fields)
|
||||
.map(([field, messages]) => `${field}: ${[].concat(messages).join(" ")}`)
|
||||
@@ -112,10 +112,10 @@ export function describeError(error) {
|
||||
}
|
||||
|
||||
// Multipart upload with progress (fetch can't report upload progress).
|
||||
export function upload(path, formData, onProgress) {
|
||||
export function upload(path, formData, onProgress, method = "POST") {
|
||||
return new Promise((resolve, reject) => {
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open("POST", `${apiBase}/api/v1${path}`);
|
||||
xhr.open(method, `${apiBase}/api/v1${path}`);
|
||||
const token = getToken();
|
||||
if (token) xhr.setRequestHeader("Authorization", `Bearer ${token}`);
|
||||
xhr.upload.addEventListener("progress", (event) => {
|
||||
|
||||
@@ -0,0 +1,170 @@
|
||||
import { api, describeError, upload } from "./api.js";
|
||||
import { artThumb, forgetArt } from "./art.js";
|
||||
import { errorBox, h, num, replace } from "./dom.js";
|
||||
import { signedInPage } from "./layout.js";
|
||||
|
||||
const PER_PAGE = 25;
|
||||
const list = document.getElementById("albums");
|
||||
const messages = document.getElementById("messages");
|
||||
const pager = document.getElementById("pager");
|
||||
const page = Math.max(1, Number.parseInt(new URLSearchParams(location.search).get("page"), 10) || 1);
|
||||
|
||||
await signedInPage("art.html");
|
||||
await refresh();
|
||||
|
||||
async function refresh() {
|
||||
try {
|
||||
render(await api(`/art/failures?page=${page}&limit=${PER_PAGE}`));
|
||||
} catch (error) {
|
||||
replace(list, h("li", {}, errorBox(error.message)));
|
||||
}
|
||||
}
|
||||
|
||||
function render(result) {
|
||||
document.getElementById("art-summary").textContent = result.total
|
||||
? `${num(result.total)} of your albums have no cover art, most-played first. Upload a picture, retry the lookup, or correct what it searches for.`
|
||||
: "Every album you've scrobbled has cover art, or is still being looked up.";
|
||||
if (!result.items.length) {
|
||||
replace(list);
|
||||
} else {
|
||||
replace(list, result.items.map(albumRow));
|
||||
}
|
||||
replace(
|
||||
pager,
|
||||
result.page > 1 ? h("a", { class: "button button-quiet", href: `?page=${result.page - 1}` }, "Previous") : h("span"),
|
||||
result.page < result.total_pages
|
||||
? h("a", { class: "button button-quiet", href: `?page=${result.page + 1}` }, "Next")
|
||||
: h("span"),
|
||||
);
|
||||
}
|
||||
|
||||
function statusText(item) {
|
||||
const own = item.override;
|
||||
if (own?.status === "pending") return "Your correction is waiting to be looked up.";
|
||||
if (own?.status === "error") {
|
||||
return own.error_code === "NotFound"
|
||||
? "Your correction didn't find any art."
|
||||
: `Your correction failed (${own.error_code ?? "unknown error"}).`;
|
||||
}
|
||||
if (item.status === "error") return `The lookup failed (${item.error_code ?? "unknown error"}). It will be tried again.`;
|
||||
if (item.status === "not_found") return "No cover art was found for this album.";
|
||||
return "Waiting to be looked up.";
|
||||
}
|
||||
|
||||
function say(notice) {
|
||||
replace(messages, notice);
|
||||
}
|
||||
|
||||
function albumRow(item) {
|
||||
const { artist, album } = item;
|
||||
const query = `artist=${encodeURIComponent(artist)}&album=${encodeURIComponent(album)}`;
|
||||
const fileInput = h("input", { type: "file", accept: "image/jpeg,image/png,image/webp", hidden: true });
|
||||
const correction = correctionForm(item, query);
|
||||
const buttons = h("div", { class: "button-row" });
|
||||
const busy = (on) => buttons.querySelectorAll("button").forEach((b) => (b.disabled = on));
|
||||
|
||||
async function act(work, done) {
|
||||
busy(true);
|
||||
replace(messages);
|
||||
try {
|
||||
await work();
|
||||
say(h("p", { class: "notice notice-ok", role: "status" }, done));
|
||||
forgetArt(artist, album);
|
||||
await refresh();
|
||||
} catch (error) {
|
||||
say(errorBox(describeError(error)));
|
||||
} finally {
|
||||
busy(false);
|
||||
}
|
||||
}
|
||||
|
||||
fileInput.addEventListener("change", () => {
|
||||
const file = fileInput.files[0];
|
||||
if (!file) return;
|
||||
const data = new FormData();
|
||||
data.append("file", file);
|
||||
act(() => upload(`/art/album?${query}`, data), `Uploaded art for ${album}.`);
|
||||
fileInput.value = "";
|
||||
});
|
||||
|
||||
replace(
|
||||
buttons,
|
||||
h("button", { type: "button", class: "button", onclick: () => fileInput.click() }, "Upload art"),
|
||||
h(
|
||||
"button",
|
||||
{
|
||||
type: "button",
|
||||
class: "button button-quiet",
|
||||
onclick: () => act(() => api(`/art/album/retry?${query}`, { method: "POST" }), `Looking up ${album} again.`),
|
||||
},
|
||||
"Retry lookup",
|
||||
),
|
||||
h(
|
||||
"button",
|
||||
{ type: "button", class: "button button-quiet", onclick: () => (correction.hidden = !correction.hidden) },
|
||||
"Correct search",
|
||||
),
|
||||
item.override
|
||||
? h(
|
||||
"button",
|
||||
{
|
||||
type: "button",
|
||||
class: "button button-quiet",
|
||||
onclick: () => act(() => api(`/art/album?${query}`, { method: "DELETE" }), `Removed your correction for ${album}.`),
|
||||
},
|
||||
"Remove mine",
|
||||
)
|
||||
: null,
|
||||
);
|
||||
correction.addEventListener("submit", (event) => {
|
||||
event.preventDefault();
|
||||
const body = { artist, album };
|
||||
const { search_artist, search_album, release_group_mbid } = correction.elements;
|
||||
if (search_artist.value.trim()) body.search_artist = search_artist.value.trim();
|
||||
if (search_album.value.trim()) body.search_album = search_album.value.trim();
|
||||
if (release_group_mbid.value.trim()) body.release_group_mbid = release_group_mbid.value.trim();
|
||||
act(() => api("/art/album/correction", { method: "PUT", body }), `Queued a corrected lookup for ${album}.`);
|
||||
});
|
||||
|
||||
return h(
|
||||
"li",
|
||||
{ class: "art-row" },
|
||||
artThumb(artist, album, "art-thumb-lg"),
|
||||
h(
|
||||
"div",
|
||||
{ class: "art-row-body" },
|
||||
h("span", { class: "row-title" }, album),
|
||||
h("span", { class: "by" }, ` by ${artist}`),
|
||||
h("p", { class: "muted" }, `${num(item.scrobbles)} ${item.scrobbles === 1 ? "scrobble" : "scrobbles"} · ${statusText(item)}`),
|
||||
buttons,
|
||||
fileInput,
|
||||
correction,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
function correctionForm(item, query) {
|
||||
const own = item.override ?? {};
|
||||
const field = (name, label, value, hint, extra = {}) =>
|
||||
h(
|
||||
"div",
|
||||
{ class: "field" },
|
||||
h("label", { for: `${name}-${query}` }, label),
|
||||
h("input", { id: `${name}-${query}`, name, type: "text", value: value ?? "", autocomplete: "off", ...extra }),
|
||||
hint ? h("p", { class: "hint" }, hint) : null,
|
||||
);
|
||||
return h(
|
||||
"form",
|
||||
{ class: "stacked-form art-correction", hidden: true },
|
||||
field("search_artist", "Search for artist", own.search_artist ?? item.artist),
|
||||
field("search_album", "Search for album", own.search_album ?? item.album),
|
||||
field(
|
||||
"release_group_mbid",
|
||||
"MusicBrainz release-group ID (optional)",
|
||||
own.release_group_mbid,
|
||||
"If you know it, paste the ID from the album's MusicBrainz page. It skips the search.",
|
||||
{ placeholder: "00000000-0000-0000-0000-000000000000", pattern: "[0-9a-fA-F-]{36}" },
|
||||
),
|
||||
h("button", { type: "submit", class: "button" }, "Look it up"),
|
||||
);
|
||||
}
|
||||
+9
-1
@@ -6,12 +6,20 @@ import { h } from "./dom.js";
|
||||
|
||||
const cache = new Map(); // dedupes repeat/concurrent lookups for the same album within a page
|
||||
|
||||
const keyFor = (artist, album) => `${artist.toLowerCase()}␟${album.toLowerCase()}`;
|
||||
|
||||
// Forget a looked-up album (after the user uploads or corrects its art) so the next
|
||||
// artThumb() for it asks the server again instead of reusing the stale answer.
|
||||
export function forgetArt(artist, album) {
|
||||
cache.delete(keyFor(artist, album));
|
||||
}
|
||||
|
||||
export function artThumb(artist, album, extraClass) {
|
||||
const classes = extraClass ? `art-thumb ${extraClass}` : "art-thumb";
|
||||
const el = h("div", { class: `${classes} art-thumb-empty` });
|
||||
if (!album) return el; // nothing to look up
|
||||
|
||||
const key = `${artist.toLowerCase()}␟${album.toLowerCase()}`;
|
||||
const key = keyFor(artist, album);
|
||||
if (!cache.has(key)) {
|
||||
cache.set(
|
||||
key,
|
||||
|
||||
@@ -6,6 +6,7 @@ import { h, replace } from "./dom.js";
|
||||
const NAV = [
|
||||
["index.html", "Overview"],
|
||||
["history.html", "History"],
|
||||
["art.html", "Album art"],
|
||||
["apps.html", "Apps"],
|
||||
["sessions.html", "Sessions"],
|
||||
["import.html", "Import"],
|
||||
|
||||
Reference in new issue
Block a user