feat(discovery): derive core-album suggestions for surfaced artists

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jonathan
2026-07-12 00:13:40 +02:00
parent ccf3fb59ab
commit 0338d8e119
4 changed files with 108 additions and 1 deletions
+43 -1
View File
@@ -3,6 +3,7 @@ from dataclasses import dataclass
import psycopg
from lyra_worker.browser import MbBrowser
from lyra_worker.release_filter import is_core_release
from lyra_worker.similarity.base import SimilaritySource
_TRUE = {"1", "true", "yes", "on"}
@@ -82,9 +83,50 @@ def _upsert_artist(conn: psycopg.Connection, cand: dict) -> int:
return cur.rowcount
def _existing_rg_mbids(conn: psycopg.Connection) -> set[str]:
with conn.cursor() as cur:
cur.execute('SELECT "rgMbid" FROM "MonitoredRelease"')
return {r[0] for r in cur.fetchall()}
def _upsert_album(conn: psycopg.Connection, artist: dict, rg) -> int:
dedupe = f"album:{artist['mbid']}:{rg.rg_mbid}"
with conn.cursor() as cur:
cur.execute(
'INSERT INTO "DiscoverySuggestion" (id, kind, "artistMbid", "artistName", '
'"rgMbid", album, "primaryType", "secondaryTypes", "firstReleaseDate", '
'score, "seedCount", sources, status, "dedupeKey", "createdAt", "updatedAt") '
"VALUES (gen_random_uuid()::text, 'album', %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, "
"'pending', %s, now(), now()) "
'ON CONFLICT ("dedupeKey") DO UPDATE SET score = EXCLUDED.score, '
'"updatedAt" = now() '
"WHERE \"DiscoverySuggestion\".status = 'pending'",
(artist["mbid"], artist["name"], rg.rg_mbid, rg.title,
rg.primary_type or None, list(rg.secondary_types), rg.first_release_date or None,
artist["score"], artist["seed_count"], sorted(artist["sources"]), dedupe),
)
return cur.rowcount
def _derive_albums(conn: psycopg.Connection, browser: MbBrowser, surfaced: list[dict],
cfg: DiscoveryConfig) -> int:
return 0 # replaced in the album-derivation task
if cfg.albums_per_artist <= 0:
return 0
have = _existing_rg_mbids(conn)
albums = 0
for artist in surfaced:
try:
groups = browser.browse_release_groups(artist["mbid"])
except Exception as e: # one artist's browse failure must not abort the sweep
print(f"worker: discovery album browse failed for {artist['mbid']}: {e}", flush=True)
continue
core = [g for g in groups
if is_core_release(g.primary_type, g.secondary_types) and g.rg_mbid not in have]
core.sort(key=lambda g: g.first_release_date or "", reverse=True)
for rg in core[: cfg.albums_per_artist]:
albums += _upsert_album(conn, artist, rg)
have.add(rg.rg_mbid)
return albums
def run_discovery(conn: psycopg.Connection, sources: list[SimilaritySource],