speed up series update

This commit is contained in:
2025-11-10 15:05:15 +01:00
parent 0066143712
commit d3a2618dd9
7 changed files with 616 additions and 167 deletions
+131 -1
View File
@@ -47,7 +47,7 @@ function inputBody(): array {
$in = $_POST ?: [];
}
// Normalize: decode JSON strings for nested payloads (e.g., tmdb)
foreach (['tmdb'] as $k) {
foreach (['tmdb','episodes','seasons'] as $k) {
if (isset($in[$k]) && is_string($in[$k])) {
$d = json_decode($in[$k], true);
if (is_array($d)) $in[$k] = $d;
@@ -191,6 +191,136 @@ try {
}
resp(['ok'=>true,'id'=>(int)$id]);
}
case 'upsert_episodes_bulk': {
$seasonId = (int)($in['season_id'] ?? 0);
$episodes = $in['episodes'] ?? null;
if (!$seasonId || !is_array($episodes)) fail('bad params');
// Detect if episodes table has tmdb_id column
$hasTmdbId = false;
try {
$pdo->query('SELECT tmdb_id FROM episodes LIMIT 0');
$hasTmdbId = true;
} catch (Throwable $e) {
$hasTmdbId = false;
}
// Prepare statements
$stmtWith = null; $stmtWithout = null;
try {
if ($hasTmdbId) {
$stmtWith = $pdo->prepare("INSERT INTO episodes (season_id, tmdb_id, episode_number, name, runtime, json)
VALUES (?,?,?,?,?,?)
ON DUPLICATE KEY UPDATE episode_number=VALUES(episode_number), name=VALUES(name), runtime=VALUES(runtime), json=VALUES(json)");
}
} catch (Throwable $_) {
$hasTmdbId = false; $stmtWith = null;
}
try {
$stmtWithout = $pdo->prepare("INSERT INTO episodes (season_id, episode_number, name, runtime, json)
VALUES (?,?,?,?,?)
ON DUPLICATE KEY UPDATE name=VALUES(name), runtime=VALUES(runtime), json=VALUES(json)");
} catch (Throwable $e) {
fail('episodes table schema not supported: '.$e->getMessage(), 500);
}
$count = 0;
$pdo->beginTransaction();
try {
foreach ($episodes as $tmdb) {
if (!is_array($tmdb)) continue;
$epNo = isset($tmdb['episode_number']) ? (int)$tmdb['episode_number'] : null;
if ($epNo === null) continue;
$name = $tmdb['name'] ?? ('Episode '.$epNo);
$runtime = isset($tmdb['runtime']) ? (int)$tmdb['runtime'] : null;
$tmdbId = $tmdb['id'] ?? null;
$json = json_encode($tmdb, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
if ($hasTmdbId && $tmdbId) {
try {
$stmtWith->execute([$seasonId, $tmdbId, $epNo, $name, $runtime, $json]);
$count++;
continue;
} catch (Throwable $_) {
// Fall back to without tmdb_id
}
}
$stmtWithout->execute([$seasonId, $epNo, $name, $runtime, $json]);
$count++;
}
$pdo->commit();
} catch (Throwable $e) {
$pdo->rollBack();
fail(MM_DEBUG ? ('bulk upsert failed: '.$e->getMessage()) : 'bulk upsert failed', 500);
}
resp(['ok'=>true, 'count'=>$count]);
}
case 'upsert_seasons_bulk': {
$showId = (int)($in['show_id'] ?? 0);
$seasons = $in['seasons'] ?? null;
if (!$showId || !is_array($seasons)) fail('bad params');
$map = [];
$count = 0;
$pdo->beginTransaction();
try {
// Prepare both statement variants to handle older schemas
$stmtWith = null; $stmtWithout = null;
try {
$stmtWith = $pdo->prepare("INSERT INTO seasons (show_id, season_number, name, air_date, json)
VALUES (?,?,?,?,?)
ON DUPLICATE KEY UPDATE name=VALUES(name), air_date=VALUES(air_date), json=VALUES(json)");
} catch (Throwable $_) {}
try {
$stmtWithout = $pdo->prepare("INSERT INTO seasons (show_id, season_number, name, json)
VALUES (?,?,?,?)
ON DUPLICATE KEY UPDATE name=VALUES(name), json=VALUES(json)");
} catch (Throwable $e) { fail('seasons table schema not supported: '.$e->getMessage(), 500); }
foreach ($seasons as $tmdb) {
if (!is_array($tmdb)) continue;
$seasonNo = isset($tmdb['season_number']) ? (int)$tmdb['season_number'] : null;
if ($seasonNo === null) continue;
if ($seasonNo < 0) continue;
$name = $tmdb['name'] ?? ('Season '.$seasonNo);
$airDate = $tmdb['air_date'] ?? null;
$json = json_encode($tmdb, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
$ok = false;
if ($stmtWith) {
try { $stmtWith->execute([$showId, $seasonNo, $name, $airDate, $json]); $ok = true; }
catch (Throwable $_) { /* fallback below */ }
}
if (!$ok && $stmtWithout) {
$stmtWithout->execute([$showId, $seasonNo, $name, $json]);
}
$count++;
// Resolve ID
$id = $pdo->lastInsertId();
if (!$id) {
$q=$pdo->prepare('SELECT id FROM seasons WHERE show_id=? AND season_number=?');
$q->execute([$showId,$seasonNo]);
$id=$q->fetchColumn();
}
if ($id) $map[(int)$seasonNo] = (int)$id;
}
$pdo->commit();
} catch (Throwable $e) {
$pdo->rollBack();
fail(MM_DEBUG ? ('bulk seasons upsert failed: '.$e->getMessage()) : 'bulk seasons upsert failed', 500);
}
resp(['ok'=>true, 'count'=>$count, 'map'=>$map]);
}
case 'get_capabilities': {
// Advertise supported actions; clients can probe this to choose bulk paths
$caps = [
'upsert_episodes_bulk' => true,
'upsert_seasons_bulk' => true,
];
resp(['ok'=>true, 'capabilities'=>$caps]);
}
case 'upsert_movie': {
$tmdb = $in['tmdb'] ?? null; if (!$tmdb || !isset($tmdb['id'])) fail('missing tmdb payload');
$stmt = $pdo->prepare("INSERT INTO movies (tmdb_id, title, original_title, release_year, poster_path, backdrop_path, runtime, json)