feat(*.php, *.psql): deduplicate API code + performance improvements
This commit is contained in:
parent
cf3dac8a46
commit
4bad005e58
31 changed files with 502 additions and 666 deletions
|
@ -7,9 +7,6 @@ use GuzzleHttp\Client;
|
|||
|
||||
class SeasonImportHandler extends ApiHandler
|
||||
{
|
||||
protected string $postgrestUrl;
|
||||
protected string $postgrestApiKey;
|
||||
|
||||
private string $tmdbApiKey;
|
||||
private string $seasonsImportToken;
|
||||
|
||||
|
@ -17,62 +14,43 @@ class SeasonImportHandler extends ApiHandler
|
|||
{
|
||||
parent::__construct();
|
||||
$this->ensureCliAccess();
|
||||
$this->loadEnvironment();
|
||||
$this->authenticateRequest();
|
||||
}
|
||||
|
||||
private function loadEnvironment(): void
|
||||
{
|
||||
$this->postgrestUrl = getenv("POSTGREST_URL") ?: $_ENV["POSTGREST_URL"];
|
||||
$this->postgrestApiKey = getenv("POSTGREST_API_KEY") ?: $_ENV["POSTGREST_API_KEY"];
|
||||
$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") {
|
||||
http_response_code(405);
|
||||
echo json_encode(["error" => "Method Not Allowed"]);
|
||||
exit();
|
||||
$this->sendErrorResponse("Method Not Allowed", 405);
|
||||
}
|
||||
|
||||
$authHeader = $_SERVER["HTTP_AUTHORIZATION"] ?? "";
|
||||
if (!preg_match('/Bearer\s+(.+)/', $authHeader, $matches)) {
|
||||
http_response_code(401);
|
||||
echo json_encode(["error" => "Unauthorized"]);
|
||||
exit();
|
||||
$this->sendErrorResponse("Unauthorized", 401);
|
||||
}
|
||||
|
||||
$providedToken = trim($matches[1]);
|
||||
if ($providedToken !== $this->seasonsImportToken) {
|
||||
http_response_code(403);
|
||||
echo json_encode(["error" => "Forbidden"]);
|
||||
exit();
|
||||
$this->sendErrorResponse("Forbidden", 403);
|
||||
}
|
||||
}
|
||||
|
||||
public function importSeasons(): void
|
||||
{
|
||||
$ongoingShows = $this->fetchOngoingShows();
|
||||
$ongoingShows = $this->fetchFromApi("optimized_shows", "ongoing=eq.true");
|
||||
|
||||
if (empty($ongoingShows)) {
|
||||
http_response_code(200);
|
||||
echo json_encode(["message" => "No ongoing shows to update"]);
|
||||
return;
|
||||
$this->sendResponse(["message" => "No ongoing shows to update"], 200);
|
||||
}
|
||||
|
||||
foreach ($ongoingShows as $show) {
|
||||
$this->processShowSeasons($show);
|
||||
}
|
||||
|
||||
http_response_code(200);
|
||||
echo json_encode(["message" => "Season import completed"]);
|
||||
}
|
||||
|
||||
private function fetchOngoingShows(): array
|
||||
{
|
||||
return $this->fetchFromPostgREST("optimized_shows", "ongoing=eq.true", "GET");
|
||||
$this->sendResponse(["message" => "Season import completed"], 200);
|
||||
}
|
||||
|
||||
private function processShowSeasons(array $show): void
|
||||
|
@ -98,8 +76,7 @@ class SeasonImportHandler extends ApiHandler
|
|||
|
||||
private function shouldKeepOngoing(string $status): bool
|
||||
{
|
||||
$validStatuses = ["Returning Series", "In Production"];
|
||||
return in_array($status, $validStatuses);
|
||||
return in_array($status, ["Returning Series", "In Production"]);
|
||||
}
|
||||
|
||||
private function fetchShowDetails(string $tmdbId): array
|
||||
|
@ -117,49 +94,40 @@ class SeasonImportHandler extends ApiHandler
|
|||
|
||||
private function fetchWatchedEpisodes(int $showId): array
|
||||
{
|
||||
$watchedEpisodes = $this->fetchFromPostgREST(
|
||||
"optimized_last_watched_episodes",
|
||||
"show_id=eq.{$showId}&order=last_watched_at.desc&limit=1",
|
||||
"GET"
|
||||
);
|
||||
$episodes = $this->fetchFromApi("optimized_last_watched_episodes", "show_id=eq.{$showId}&order=last_watched_at.desc&limit=1");
|
||||
|
||||
if (empty($watchedEpisodes)) return [];
|
||||
if (empty($episodes)) return [];
|
||||
|
||||
$lastWatched = $watchedEpisodes[0] ?? null;
|
||||
|
||||
if ($lastWatched) return [
|
||||
"season_number" => (int) $lastWatched["season_number"],
|
||||
"episode_number" => (int) $lastWatched["episode_number"]
|
||||
];
|
||||
|
||||
return [];
|
||||
return [
|
||||
"season_number" => (int) $episodes[0]["season_number"],
|
||||
"episode_number" => (int) $episodes[0]["episode_number"],
|
||||
];
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
$watchedEpisodes = $this->fetchWatchedEpisodes($showId);
|
||||
$lastWatchedSeason = $watchedEpisodes["season_number"] ?? null;
|
||||
$lastWatchedEpisode = $watchedEpisodes["episode_number"] ?? null;
|
||||
$scheduledEpisodes = $this->fetchFromPostgREST(
|
||||
"optimized_scheduled_episodes",
|
||||
"show_id=eq.{$showId}&season_number=eq.{$seasonNumber}",
|
||||
"GET"
|
||||
$watched = $this->fetchWatchedEpisodes($showId);
|
||||
$lastWatchedSeason = $watched["season_number"] ?? null;
|
||||
$lastWatchedEpisode = $watched["episode_number"] ?? null;
|
||||
|
||||
$scheduled = $this->fetchFromApi(
|
||||
"optimized_scheduled_episodes",
|
||||
"show_id=eq.{$showId}&season_number=eq.{$seasonNumber}"
|
||||
);
|
||||
$scheduledEpisodeNumbers = array_column($scheduledEpisodes, "episode_number");
|
||||
|
||||
$scheduledEpisodeNumbers = array_column($scheduled, "episode_number");
|
||||
|
||||
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;
|
||||
|
||||
|
@ -183,11 +151,10 @@ class SeasonImportHandler extends ApiHandler
|
|||
private function addEpisodeToSchedule(int $showId, int $seasonNumber, array $episode): void
|
||||
{
|
||||
$airDate = $episode["air_date"] ?? null;
|
||||
|
||||
if (!$airDate) return;
|
||||
|
||||
$currentDate = date("Y-m-d");
|
||||
$status = ($airDate && $airDate < $currentDate) ? "aired" : "upcoming";
|
||||
$today = date("Y-m-d");
|
||||
$status = ($airDate < $today) ? "aired" : "upcoming";
|
||||
|
||||
$payload = [
|
||||
"show_id" => $showId,
|
||||
|
@ -197,7 +164,7 @@ class SeasonImportHandler extends ApiHandler
|
|||
"status" => $status,
|
||||
];
|
||||
|
||||
$this->fetchFromPostgREST("scheduled_episodes", "", "POST", $payload);
|
||||
$this->makeRequest("POST", "scheduled_episodes", ["json" => $payload]);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue