diff --git a/api/artist-import.php b/api/artist-import.php index 38dd487..ca4b803 100644 --- a/api/artist-import.php +++ b/api/artist-import.php @@ -37,9 +37,11 @@ class ArtistImportHandler extends ApiHandler try { $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); } catch (\Exception $e) { @@ -90,7 +92,7 @@ class ArtistImportHandler extends ApiHandler return json_decode($response->getBody(), true); } - private function processArtist(object $artistData): bool + private function processArtist(object $artistData, string $genreName = ""): bool { $artistName = $artistData->name ?? ""; @@ -103,7 +105,7 @@ class ArtistImportHandler extends ApiHandler $artistKey = sanitizeMediaString($artistName); $slug = "/music/artists/{$artistKey}"; $description = strip_tags($artistData->biography ?? ""); - $genre = $this->resolveGenreId($artistData->genres[0]->name ?? ""); + $genre = $this->resolveGenreId(strtolower($genreName)); $starred = $artistData->starred ?? false; $artistPayload = [ "name_string" => $artistName, @@ -121,7 +123,7 @@ class ArtistImportHandler extends ApiHandler return true; } - private function processAlbums(string $artistId, string $artistName): void + private function processAlbums(string $artistId, string $artistName, array $albumData): void { $artist = $this->getArtistByName($artistName); @@ -129,9 +131,8 @@ class ArtistImportHandler extends ApiHandler $existingAlbums = $this->getExistingAlbums($artist["id"]); $existingAlbumKeys = array_column($existingAlbums, "key"); - $navidromeAlbums = $this->fetchNavidromeAlbums($artistId); - foreach ($navidromeAlbums as $album) { + foreach ($albumData as $album) { $albumName = $album["name"] ?? ""; $releaseYearRaw = $album["date"] ?? null; $releaseYear = null; diff --git a/api/scrobble.php b/api/scrobble.php index b0b9b7c..1ce21da 100644 --- a/api/scrobble.php +++ b/api/scrobble.php @@ -131,7 +131,7 @@ class NavidromeScrobbleHandler extends ApiHandler if (!empty($existingArtist)) return $this->artistCache[$artistName] = $existingArtist[0]; - $this->makeRequest("POST", "artists", [ + $response = $this->makeRequest("POST", "artists", [ "json" => [ "mbid" => "", "art" => "4cef75db-831f-4f5d-9333-79eaa5bb55ee", @@ -143,13 +143,14 @@ class NavidromeScrobbleHandler extends ApiHandler "favorite" => false, "tattoo" => false, "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 @@ -172,7 +173,7 @@ class NavidromeScrobbleHandler extends ApiHandler return []; } - $this->makeRequest("POST", "albums", [ + $response = $this->makeRequest("POST", "albums", [ "json" => [ "mbid" => null, "art" => "4cef75db-831f-4f5d-9333-79eaa5bb55ee", @@ -181,14 +182,14 @@ class NavidromeScrobbleHandler extends ApiHandler "tentative" => true, "total_plays" => 0, "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] = $albumData[0] ?? []; + return $this->albumCache[$albumKey] = $inserted ?? []; } private function insertListen(array $track, string $albumKey): void diff --git a/api/watching-import.php b/api/watching-import.php index 7741c24..09d44f2 100644 --- a/api/watching-import.php +++ b/api/watching-import.php @@ -92,16 +92,20 @@ class WatchingImportHandler extends ApiHandler $table = $mediaType === "movie" ? "movies" : "shows"; try { - $response = $this->makeRequest("POST", $table, ["json" => $payload]); + $response = $this->makeRequest("POST", $table, [ + "json" => $payload, + "headers" => ["Prefer" => "return=representation"] + ]); } catch (\Exception $e) { $response = $this->fetchFromApi($table, "tmdb_id=eq.{$id}")[0] ?? []; } - if (!empty($response["id"])) { - $mediaId = $response["id"]; - $existingTagMap = $this->getTagIds($tags); - $updatedTagMap = $this->insertMissingTags($tags, $existingTagMap); - $this->associateTagsWithMedia($mediaType, $mediaId, array_values($updatedTagMap)); + $record = $response[0] ?? []; + + if (!empty($record["id"])) { + $mediaId = $record["id"]; + $tagIds = $this->getTagIds($tags); + if (!empty($tagIds)) $this->associateTagsWithMedia($mediaType, $mediaId, array_values($tagIds)); } } @@ -119,23 +123,6 @@ class WatchingImportHandler extends ApiHandler 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 { $junction = $mediaType === "movie" ? "movies_tags" : "shows_tags"; diff --git a/package-lock.json b/package-lock.json index a7241d4..44be9bb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "coryd.dev", - "version": "9.0.4", + "version": "9.1.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "coryd.dev", - "version": "9.0.4", + "version": "9.1.4", "license": "MIT", "dependencies": { "minisearch": "^7.1.2", diff --git a/package.json b/package.json index ae0b250..6fa74bf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "coryd.dev", - "version": "9.0.4", + "version": "9.1.4", "description": "The source for my personal site. Built using 11ty (and other tools).", "type": "module", "engines": {