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 {
$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;

View file

@ -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

View file

@ -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";