chore(api/*): cleans up unnecessary network calls, fixes tag + genre assignments

This commit is contained in:
Cory Dransfeldt 2025-06-06 12:01:42 -07:00
parent 8890f7dcfc
commit 5a133a7c23
No known key found for this signature in database
5 changed files with 33 additions and 44 deletions

View file

@ -37,9 +37,11 @@ class ArtistImportHandler extends ApiHandler
try { try {
$artistData = $this->fetchNavidromeArtist($artistId); $artistData = $this->fetchNavidromeArtist($artistId);
$artistExists = $this->processArtist($artistData); $albumData = $this->fetchNavidromeAlbums($artistId);
$genre = $albumData[0]["genre"] ?? ($albumData[0]["genres"][0]["name"] ?? "");
$artistExists = $this->processArtist($artistData, $genre);
if ($artistExists) $this->processAlbums($artistId, $artistData->name); if ($artistExists) $this->processAlbums($artistId, $artistData->name, $albumData);
$this->sendJsonResponse("message", "Artist and albums synced successfully", 200); $this->sendJsonResponse("message", "Artist and albums synced successfully", 200);
} catch (\Exception $e) { } catch (\Exception $e) {
@ -90,7 +92,7 @@ class ArtistImportHandler extends ApiHandler
return json_decode($response->getBody(), true); return json_decode($response->getBody(), true);
} }
private function processArtist(object $artistData): bool private function processArtist(object $artistData, string $genreName = ""): bool
{ {
$artistName = $artistData->name ?? ""; $artistName = $artistData->name ?? "";
@ -103,7 +105,7 @@ class ArtistImportHandler extends ApiHandler
$artistKey = sanitizeMediaString($artistName); $artistKey = sanitizeMediaString($artistName);
$slug = "/music/artists/{$artistKey}"; $slug = "/music/artists/{$artistKey}";
$description = strip_tags($artistData->biography ?? ""); $description = strip_tags($artistData->biography ?? "");
$genre = $this->resolveGenreId($artistData->genres[0]->name ?? ""); $genre = $this->resolveGenreId(strtolower($genreName));
$starred = $artistData->starred ?? false; $starred = $artistData->starred ?? false;
$artistPayload = [ $artistPayload = [
"name_string" => $artistName, "name_string" => $artistName,
@ -121,7 +123,7 @@ class ArtistImportHandler extends ApiHandler
return true; return true;
} }
private function processAlbums(string $artistId, string $artistName): void private function processAlbums(string $artistId, string $artistName, array $albumData): void
{ {
$artist = $this->getArtistByName($artistName); $artist = $this->getArtistByName($artistName);
@ -129,9 +131,8 @@ class ArtistImportHandler extends ApiHandler
$existingAlbums = $this->getExistingAlbums($artist["id"]); $existingAlbums = $this->getExistingAlbums($artist["id"]);
$existingAlbumKeys = array_column($existingAlbums, "key"); $existingAlbumKeys = array_column($existingAlbums, "key");
$navidromeAlbums = $this->fetchNavidromeAlbums($artistId);
foreach ($navidromeAlbums as $album) { foreach ($albumData as $album) {
$albumName = $album["name"] ?? ""; $albumName = $album["name"] ?? "";
$releaseYearRaw = $album["date"] ?? null; $releaseYearRaw = $album["date"] ?? null;
$releaseYear = null; $releaseYear = null;

View file

@ -131,7 +131,7 @@ class NavidromeScrobbleHandler extends ApiHandler
if (!empty($existingArtist)) return $this->artistCache[$artistName] = $existingArtist[0]; if (!empty($existingArtist)) return $this->artistCache[$artistName] = $existingArtist[0];
$this->makeRequest("POST", "artists", [ $response = $this->makeRequest("POST", "artists", [
"json" => [ "json" => [
"mbid" => "", "mbid" => "",
"art" => "4cef75db-831f-4f5d-9333-79eaa5bb55ee", "art" => "4cef75db-831f-4f5d-9333-79eaa5bb55ee",
@ -143,13 +143,14 @@ class NavidromeScrobbleHandler extends ApiHandler
"favorite" => false, "favorite" => false,
"tattoo" => false, "tattoo" => false,
"total_plays" => 0 "total_plays" => 0
] ],
"headers" => ["Prefer" => "return=representation"]
]); ]);
$this->sendFailureEmail("New tentative artist record", "A new tentative artist record was inserted for: $artistName");
$artistData = $this->fetchFromApi("artists", "name_string=eq.{$encodedArtist}&limit=1"); $inserted = $response[0] ?? null;
if ($inserted) $this->sendFailureEmail("New tentative artist record", "A new tentative artist record was inserted for: $artistName");
return $this->artistCache[$artistName] = $artistData[0] ?? []; return $this->artistCache[$artistName] = $inserted ?? [];
} }
private function getOrCreateAlbum(string $albumName, array $artistData): array private function getOrCreateAlbum(string $albumName, array $artistData): array
@ -172,7 +173,7 @@ class NavidromeScrobbleHandler extends ApiHandler
return []; return [];
} }
$this->makeRequest("POST", "albums", [ $response = $this->makeRequest("POST", "albums", [
"json" => [ "json" => [
"mbid" => null, "mbid" => null,
"art" => "4cef75db-831f-4f5d-9333-79eaa5bb55ee", "art" => "4cef75db-831f-4f5d-9333-79eaa5bb55ee",
@ -181,14 +182,14 @@ class NavidromeScrobbleHandler extends ApiHandler
"tentative" => true, "tentative" => true,
"total_plays" => 0, "total_plays" => 0,
"artist" => $artistId "artist" => $artistId
] ],
"headers" => ["Prefer" => "return=representation"]
]); ]);
$this->sendFailureEmail("New tentative album record", "A new tentative album record was inserted:\n\nAlbum: $albumName\nKey: $albumKey"); $inserted = $response[0] ?? null;
if ($inserted) $this->sendFailureEmail("New tentative album record", "A new tentative album record was inserted:\n\nAlbum: $albumName\nKey: $albumKey");
$albumData = $this->fetchFromApi("albums", "key=eq.{$encodedAlbumKey}&limit=1"); return $this->albumCache[$albumKey] = $inserted ?? [];
return $this->albumCache[$albumKey] = $albumData[0] ?? [];
} }
private function insertListen(array $track, string $albumKey): void private function insertListen(array $track, string $albumKey): void

View file

@ -92,16 +92,20 @@ class WatchingImportHandler extends ApiHandler
$table = $mediaType === "movie" ? "movies" : "shows"; $table = $mediaType === "movie" ? "movies" : "shows";
try { try {
$response = $this->makeRequest("POST", $table, ["json" => $payload]); $response = $this->makeRequest("POST", $table, [
"json" => $payload,
"headers" => ["Prefer" => "return=representation"]
]);
} catch (\Exception $e) { } catch (\Exception $e) {
$response = $this->fetchFromApi($table, "tmdb_id=eq.{$id}")[0] ?? []; $response = $this->fetchFromApi($table, "tmdb_id=eq.{$id}")[0] ?? [];
} }
if (!empty($response["id"])) { $record = $response[0] ?? [];
$mediaId = $response["id"];
$existingTagMap = $this->getTagIds($tags); if (!empty($record["id"])) {
$updatedTagMap = $this->insertMissingTags($tags, $existingTagMap); $mediaId = $record["id"];
$this->associateTagsWithMedia($mediaType, $mediaId, array_values($updatedTagMap)); $tagIds = $this->getTagIds($tags);
if (!empty($tagIds)) $this->associateTagsWithMedia($mediaType, $mediaId, array_values($tagIds));
} }
} }
@ -119,23 +123,6 @@ class WatchingImportHandler extends ApiHandler
return $map; return $map;
} }
private function insertMissingTags(array $tags, array $existingMap): array
{
$newTags = array_diff($tags, array_keys($existingMap));
foreach ($newTags as $tag) {
try {
$created = $this->makeRequest("POST", "tags", ["json" => ["name" => $tag]]);
if (!empty($created["id"])) $existingMap[$tag] = $created["id"];
} catch (\Exception $e) {
$fallback = $this->fetchFromApi("tags", "name=eq." . urlencode($tag));
if (!empty($fallback[0]["id"])) $existingMap[$tag] = $fallback[0]["id"];
}
}
return $existingMap;
}
private function associateTagsWithMedia(string $mediaType, int $mediaId, array $tagIds): void private function associateTagsWithMedia(string $mediaType, int $mediaId, array $tagIds): void
{ {
$junction = $mediaType === "movie" ? "movies_tags" : "shows_tags"; $junction = $mediaType === "movie" ? "movies_tags" : "shows_tags";

4
package-lock.json generated
View file

@ -1,12 +1,12 @@
{ {
"name": "coryd.dev", "name": "coryd.dev",
"version": "9.0.4", "version": "9.1.4",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "coryd.dev", "name": "coryd.dev",
"version": "9.0.4", "version": "9.1.4",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"minisearch": "^7.1.2", "minisearch": "^7.1.2",

View file

@ -1,6 +1,6 @@
{ {
"name": "coryd.dev", "name": "coryd.dev",
"version": "9.0.4", "version": "9.1.4",
"description": "The source for my personal site. Built using 11ty (and other tools).", "description": "The source for my personal site. Built using 11ty (and other tools).",
"type": "module", "type": "module",
"engines": { "engines": {