feat: spotify-only matching + optional local-LLM gate + google→bandcamp fallback (closes task/drop-musicbrainz-google-fallback, closes task/ollama-classifier)
Source: human-feedback note 01KVDYV3A35B57N15CRBQDXPCH (project/reddit-spotify-linker, 2026-06-18) — "remove the musicbrainz integration and just use spotify"; a google search for "<name> bandcamp" fallback; and "would a local llm be better able to categorise these ... include instructions for running something suitable via ollama (16GB M2)". - Remove MusicBrainz entirely (viaMusicBrainz + url-rels + rate-gate; lib relUrls/mbResult). Spotify is now the only music API; it both gates by exact-name match and supplies the direct artist link. - Optional ollama LLM gate (OLLAMA_URL enables it, OLLAMA_MODEL default qwen2.5:3b) called over plain HTTP -> stays zero npm-dep. It classifies which candidates are real artists/albums (the matching-quality lever). Pure prompt/parse logic in new llm.js (unit-tested in llm.test.js); the fetch + resolveAll gate wiring live in resolver.js. - A confirmed name links to its direct Spotify page when available, else a Google search for "<name> bandcamp". New "google" primary platform rendered by the extension (styles.css blue accent + platform-aware link title in content.js). - README: drop MusicBrainz; document the two gates + a runnable local-LLM setup for a 16GB MacBook M2. AGENTS.md: flip the old "no ML/NER" standing decision per the human override. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
+19
-58
@@ -4,44 +4,22 @@
|
||||
|
||||
export const norm = (s) => String(s).normalize("NFKC").trim().toLowerCase();
|
||||
|
||||
export const bandcampSearch = (n) => `https://bandcamp.com/search?q=${encodeURIComponent(n)}&item_type=b`;
|
||||
export const spotifySearch = (n) => `https://open.spotify.com/search/${encodeURIComponent(n)}`;
|
||||
// Fallback link when we have no direct Spotify page: a Google search biased at Bandcamp,
|
||||
// which surfaces the artist's Bandcamp page (when it exists) plus general results.
|
||||
export const googleSearch = (n) => `https://www.google.com/search?q=${encodeURIComponent(`${n} bandcamp`)}`;
|
||||
|
||||
// The precision lever: return the result whose name matches the candidate
|
||||
// case-insensitively, else null. `minScore` (MusicBrainz) additionally requires a
|
||||
// confidence score. Tolerates null/garbage items.
|
||||
export function pickArtist(name, items, { minScore = null } = {}) {
|
||||
// The precision lever: return the item whose name matches the candidate
|
||||
// case-insensitively, else null. Tolerates null/garbage items.
|
||||
export function pickArtist(name, items) {
|
||||
const want = norm(name);
|
||||
return (
|
||||
(items ?? []).find(
|
||||
(a) => a && norm(a.name) === want && (minScore == null || (a.score ?? 0) >= minScore),
|
||||
) ?? null
|
||||
);
|
||||
return (items ?? []).find((a) => a && norm(a.name) === want) ?? null;
|
||||
}
|
||||
|
||||
// A "/search" url means we don't have a direct artist page (just a search fallback).
|
||||
export const isSearchUrl = (url) => typeof url === "string" && url.includes("/search");
|
||||
|
||||
// Choose the single best link to show: prefer Spotify when the artist is actually ON
|
||||
// Spotify (a direct, non-search url); else a direct Bandcamp; else the Spotify search.
|
||||
export function primaryOf({ spotify, bandcamp }) {
|
||||
if (spotify && !isSearchUrl(spotify)) return { platform: "spotify", url: spotify };
|
||||
if (bandcamp && !isSearchUrl(bandcamp)) return { platform: "bandcamp", url: bandcamp };
|
||||
return { platform: "spotify", url: spotify };
|
||||
}
|
||||
|
||||
// Link object for a confirmed Spotify hit (direct artist url when present, else search).
|
||||
export function spotifyResult(hit) {
|
||||
const r = {
|
||||
name: hit.name,
|
||||
spotify: hit.external_urls?.spotify ?? spotifySearch(hit.name),
|
||||
bandcamp: bandcampSearch(hit.name),
|
||||
};
|
||||
return { ...r, primary: primaryOf(r) };
|
||||
}
|
||||
|
||||
// True for http/https URLs only — reject javascript:, ftp:, garbage. These values flow
|
||||
// into an injected link's href, so validate the third-party MB data defensively.
|
||||
// True for http/https URLs only — reject javascript:, ftp:, garbage. The Spotify
|
||||
// external_url flows into an injected link's href, so validate it defensively.
|
||||
export function isHttpUrl(u) {
|
||||
try {
|
||||
const p = new URL(u).protocol;
|
||||
@@ -51,31 +29,14 @@ export function isHttpUrl(u) {
|
||||
}
|
||||
}
|
||||
|
||||
// Extract direct links from a MusicBrainz artist's url-relations (zero-cred: MB stores
|
||||
// official Bandcamp / streaming URLs). Accepts only http(s) and matches by parsed hostname
|
||||
// (not substring, so "evilbandcamp.com" / "javascript:…bandcamp.com…" don't slip through).
|
||||
// Either field may be null.
|
||||
export function relUrls(relations) {
|
||||
const urls = (relations ?? []).map((r) => r && r.url && r.url.resource).filter(isHttpUrl);
|
||||
const byHost = (host) =>
|
||||
urls.find((u) => {
|
||||
try {
|
||||
const h = new URL(u).hostname;
|
||||
return h === host || h.endsWith("." + host);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}) ?? null;
|
||||
return { bandcamp: byHost("bandcamp.com"), spotify: byHost("open.spotify.com") };
|
||||
}
|
||||
|
||||
// Link object for a confirmed MusicBrainz hit: prefer the direct url-rel links, fall back
|
||||
// to search urls when MB has no relation for that platform.
|
||||
export function mbResult(hit, links) {
|
||||
const r = {
|
||||
name: hit.name,
|
||||
spotify: links?.spotify ?? spotifySearch(hit.name),
|
||||
bandcamp: links?.bandcamp ?? bandcampSearch(hit.name),
|
||||
};
|
||||
return { ...r, primary: primaryOf(r) };
|
||||
// Build the single link to show for a confirmed name. Prefer the direct Spotify artist
|
||||
// url (validated http(s), not a /search url); otherwise fall back to a Google→Bandcamp
|
||||
// search. `primary` is what the extension renders ({ platform, url }).
|
||||
export function linkResult(name, spotifyUrl) {
|
||||
const direct = spotifyUrl && isHttpUrl(spotifyUrl) && !isSearchUrl(spotifyUrl) ? spotifyUrl : null;
|
||||
if (direct) {
|
||||
return { name, spotify: direct, primary: { platform: "spotify", url: direct } };
|
||||
}
|
||||
const url = googleSearch(name);
|
||||
return { name, google: url, primary: { platform: "google", url } };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user