Add ability for album specific art overrides (requires genius api); add example overrides
This commit is contained in:
1 parent
a55d26fd62
commit
8dbb502d4a
3 files changed
+82
-15
No files matched your search
+39
-13
@@ -27,17 +27,35 @@ if ($artist === '' && $title === '') {
|
||||
}
|
||||
|
||||
/* -- Operator-defined overrides (config/overrides.json) -------------------- */
|
||||
$overrides = [];
|
||||
$overridesFile = __DIR__ . '/../config/overrides.json';
|
||||
if (is_file($overridesFile)) {
|
||||
$ov = json_decode((string) file_get_contents($overridesFile), true);
|
||||
foreach (($ov['artwork'] ?? []) as $rule) {
|
||||
$ra = $rule['artist'] ?? null;
|
||||
$rt = $rule['title'] ?? null;
|
||||
$artistMatch = ($ra === null || strcasecmp($ra, $artist) === 0);
|
||||
$titleMatch = ($rt === null || strcasecmp($rt, $title) === 0);
|
||||
if ($artistMatch && $titleMatch && !empty($rule['art'])) {
|
||||
json_out(['art' => $rule['art'], 'album' => ($rule['album'] ?? ''), 'url' => '']);
|
||||
}
|
||||
if (is_array($ov)) $overrides = $ov['artwork'] ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Does an override rule match this track? A rule may set any of artist / title
|
||||
* / album (all case-insensitive). artist & title are exact matches; album is a
|
||||
* substring match, so "Because the Internet" also catches "...(Deluxe)". An
|
||||
* album rule can only match once $album is known (after the provider lookup),
|
||||
* which is what lets you target one album without hitting the artist's others.
|
||||
*/
|
||||
function override_matches($rule, $artist, $title, $album) {
|
||||
if (isset($rule['artist']) && strcasecmp($rule['artist'], $artist) !== 0) return false;
|
||||
if (isset($rule['title']) && strcasecmp($rule['title'], $title) !== 0) return false;
|
||||
if (isset($rule['album'])) {
|
||||
if ($album === '' || stripos($album, (string) $rule['album']) === false) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Fast path: artist/title-only rules match before any API call. (Rules that
|
||||
// target an album can't match yet — the album is still unknown.)
|
||||
foreach ($overrides as $rule) {
|
||||
if (!empty($rule['art']) && !isset($rule['album'])
|
||||
&& override_matches($rule, $artist, $title, '')) {
|
||||
json_out(['art' => $rule['art'], 'album' => '', 'url' => '']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,15 +121,23 @@ function lastfm_artwork($artist, $title) {
|
||||
$order = $source === 'lastfm' ? ['lastfm']
|
||||
: ($source === 'genius' ? ['genius'] : ['genius', 'lastfm']);
|
||||
|
||||
$album = '';
|
||||
$art = ''; $album = ''; $url = '';
|
||||
foreach ($order as $provider) {
|
||||
$res = ($provider === 'genius') ? genius_artwork($artist, $title) : lastfm_artwork($artist, $title);
|
||||
if ($res === null) continue;
|
||||
if ($album === '' && $res['album'] !== '') $album = $res['album'];
|
||||
if ($res['art'] !== '') {
|
||||
$res['album'] = $res['album'] ?: $album;
|
||||
json_out($res);
|
||||
if ($res['art'] !== '') { $art = $res['art']; $url = $res['url']; break; }
|
||||
}
|
||||
|
||||
// Album-level overrides: now that the album is known, swap the art if a rule
|
||||
// targets this album (e.g. a special animated cover for one album only). This
|
||||
// replaces whatever the provider returned, but only for the matching album.
|
||||
foreach ($overrides as $rule) {
|
||||
if (!empty($rule['art']) && isset($rule['album'])
|
||||
&& override_matches($rule, $artist, $title, $album)) {
|
||||
$art = $rule['art'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
json_out(['art' => '', 'album' => $album, 'url' => '']);
|
||||
json_out(['art' => $art, 'album' => $album, 'url' => $url]);
|
||||
Reference in new issue
Block a user