chore(*): remove duplicate cache rule + cleanup cache headers; cleanup + formatting

This commit is contained in:
Cory Dransfeldt 2025-05-17 13:25:40 -07:00
parent 425fed6ff6
commit 0e565970a5
No known key found for this signature in database
42 changed files with 223 additions and 217 deletions

View file

@ -13,38 +13,31 @@ class SeasonImportHandler extends ApiHandler
public function __construct()
{
parent::__construct();
$this->ensureCliAccess();
$this->ensureCliAccess();
$this->tmdbApiKey = getenv("TMDB_API_KEY") ?: $_ENV["TMDB_API_KEY"];
$this->seasonsImportToken = getenv("SEASONS_IMPORT_TOKEN") ?: $_ENV["SEASONS_IMPORT_TOKEN"];
$this->authenticateRequest();
}
private function authenticateRequest(): void
{
if ($_SERVER["REQUEST_METHOD"] !== "POST") {
$this->sendErrorResponse("Method Not Allowed", 405);
}
if ($_SERVER["REQUEST_METHOD"] !== "POST") $this->sendErrorResponse("Method Not Allowed", 405);
$authHeader = $_SERVER["HTTP_AUTHORIZATION"] ?? "";
if (!preg_match('/Bearer\s+(.+)/', $authHeader, $matches)) {
$this->sendErrorResponse("Unauthorized", 401);
}
if (!preg_match('/Bearer\s+(.+)/', $authHeader, $matches)) $this->sendErrorResponse("Unauthorized", 401);
$providedToken = trim($matches[1]);
if ($providedToken !== $this->seasonsImportToken) {
$this->sendErrorResponse("Forbidden", 403);
}
if ($providedToken !== $this->seasonsImportToken) $this->sendErrorResponse("Forbidden", 403);
}
public function importSeasons(): void
{
$ongoingShows = $this->fetchFromApi("optimized_shows", "ongoing=eq.true");
if (empty($ongoingShows)) {
$this->sendResponse(["message" => "No ongoing shows to update"], 200);
}
if (empty($ongoingShows)) $this->sendResponse(["message" => "No ongoing shows to update"], 200);
foreach ($ongoingShows as $show) {
$this->processShowSeasons($show);
@ -86,6 +79,7 @@ class SeasonImportHandler extends ApiHandler
try {
$response = $client->get($url, ["headers" => ["Accept" => "application/json"]]);
return json_decode($response->getBody(), true) ?? [];
} catch (\Exception $e) {
return [];
@ -107,9 +101,11 @@ class SeasonImportHandler extends ApiHandler
private function processSeasonEpisodes(int $showId, string $tmdbId, array $season): void
{
$seasonNumber = $season["season_number"] ?? null;
if ($seasonNumber === null || $seasonNumber == 0) return;
$episodes = $this->fetchSeasonEpisodes($tmdbId, $seasonNumber);
if (empty($episodes)) return;
$watched = $this->fetchWatchedEpisodes($showId);
@ -125,9 +121,9 @@ class SeasonImportHandler extends ApiHandler
foreach ($episodes as $episode) {
$episodeNumber = $episode["episode_number"] ?? null;
if ($episodeNumber === null) continue;
if (in_array($episodeNumber, $scheduledEpisodeNumbers)) continue;
if ($lastWatchedSeason !== null && $seasonNumber < $lastWatchedSeason) return;
if ($seasonNumber == $lastWatchedSeason && $episodeNumber <= $lastWatchedEpisode) continue;
@ -142,6 +138,7 @@ class SeasonImportHandler extends ApiHandler
try {
$response = $client->get($url, ["headers" => ["Accept" => "application/json"]]);
return json_decode($response->getBody(), true)["episodes"] ?? [];
} catch (\Exception $e) {
return [];
@ -151,11 +148,11 @@ class SeasonImportHandler extends ApiHandler
private function addEpisodeToSchedule(int $showId, int $seasonNumber, array $episode): void
{
$airDate = $episode["air_date"] ?? null;
if (!$airDate) return;
$today = date("Y-m-d");
$status = ($airDate < $today) ? "aired" : "upcoming";
$payload = [
"show_id" => $showId,
"season_number" => $seasonNumber,