chore(*): remove duplicate cache rule + cleanup cache headers; cleanup + formatting
This commit is contained in:
parent
425fed6ff6
commit
0e565970a5
42 changed files with 223 additions and 217 deletions
|
@ -13,7 +13,6 @@ class NavidromeScrobbleHandler extends ApiHandler
|
|||
private string $navidromeApiUrl;
|
||||
private string $navidromeAuthToken;
|
||||
private string $forwardEmailApiKey;
|
||||
|
||||
private array $artistCache = [];
|
||||
private array $albumCache = [];
|
||||
|
||||
|
@ -39,7 +38,9 @@ class NavidromeScrobbleHandler extends ApiHandler
|
|||
|
||||
if ($authHeader !== $expectedToken) {
|
||||
http_response_code(401);
|
||||
|
||||
echo json_encode(["error" => "Unauthorized."]);
|
||||
|
||||
exit();
|
||||
}
|
||||
}
|
||||
|
@ -60,6 +61,7 @@ class NavidromeScrobbleHandler extends ApiHandler
|
|||
private function fetchRecentlyPlayed(): array
|
||||
{
|
||||
$client = new Client();
|
||||
|
||||
try {
|
||||
$response = $client->request("GET", "{$this->navidromeApiUrl}/api/song", [
|
||||
"query" => [
|
||||
|
@ -76,9 +78,11 @@ class NavidromeScrobbleHandler extends ApiHandler
|
|||
]);
|
||||
|
||||
$data = json_decode($response->getBody()->getContents(), true);
|
||||
|
||||
return $data ?? [];
|
||||
} catch (\Exception $e) {
|
||||
error_log("Error fetching tracks: " . $e->getMessage());
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
@ -113,15 +117,12 @@ class NavidromeScrobbleHandler extends ApiHandler
|
|||
private function getOrCreateArtist(string $artistName): array
|
||||
{
|
||||
if (!$this->isDatabaseAvailable()) return [];
|
||||
|
||||
if (isset($this->artistCache[$artistName])) return $this->artistCache[$artistName];
|
||||
|
||||
$encodedArtist = rawurlencode($artistName);
|
||||
$existingArtist = $this->fetchFromApi("artists", "name_string=eq.{$encodedArtist}&limit=1");
|
||||
|
||||
if (!empty($existingArtist)) {
|
||||
return $this->artistCache[$artistName] = $existingArtist[0];
|
||||
}
|
||||
if (!empty($existingArtist)) return $this->artistCache[$artistName] = $existingArtist[0];
|
||||
|
||||
$this->makeRequest("POST", "artists", [
|
||||
"json" => [
|
||||
|
@ -137,7 +138,6 @@ class NavidromeScrobbleHandler extends ApiHandler
|
|||
"total_plays" => 0
|
||||
]
|
||||
]);
|
||||
|
||||
$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");
|
||||
|
@ -156,9 +156,7 @@ class NavidromeScrobbleHandler extends ApiHandler
|
|||
$encodedAlbumKey = rawurlencode($albumKey);
|
||||
$existingAlbum = $this->fetchFromApi("albums", "key=eq.{$encodedAlbumKey}&limit=1");
|
||||
|
||||
if (!empty($existingAlbum)) {
|
||||
return $this->albumCache[$albumKey] = $existingAlbum[0];
|
||||
}
|
||||
if (!empty($existingAlbum)) return $this->albumCache[$albumKey] = $existingAlbum[0];
|
||||
|
||||
$artistId = $artistData["id"] ?? null;
|
||||
|
||||
|
@ -205,6 +203,7 @@ class NavidromeScrobbleHandler extends ApiHandler
|
|||
{
|
||||
$artistKey = sanitizeMediaString($artistName);
|
||||
$albumKey = sanitizeMediaString($albumName);
|
||||
|
||||
return "{$artistKey}-{$albumKey}";
|
||||
}
|
||||
|
||||
|
@ -230,9 +229,8 @@ class NavidromeScrobbleHandler extends ApiHandler
|
|||
]);
|
||||
} catch (\GuzzleHttp\Exception\RequestException $e) {
|
||||
error_log("Request Exception: " . $e->getMessage());
|
||||
if ($e->hasResponse()) {
|
||||
error_log("Error Response: " . (string) $e->getResponse()->getBody());
|
||||
}
|
||||
|
||||
if ($e->hasResponse()) error_log("Error Response: " . (string) $e->getResponse()->getBody());
|
||||
} catch (\Exception $e) {
|
||||
error_log("General Exception: " . $e->getMessage());
|
||||
}
|
||||
|
@ -242,9 +240,11 @@ class NavidromeScrobbleHandler extends ApiHandler
|
|||
{
|
||||
try {
|
||||
$response = $this->fetchFromApi("listens", "limit=1");
|
||||
|
||||
return is_array($response);
|
||||
} catch (\Exception $e) {
|
||||
error_log("Database check failed: " . $e->getMessage());
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -255,5 +255,6 @@ try {
|
|||
$handler->runScrobbleCheck();
|
||||
} catch (\Exception $e) {
|
||||
http_response_code(500);
|
||||
|
||||
echo json_encode(["error" => $e->getMessage()]);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue