tracker
All repositories: gitoria
9.7 KB
// lib/tmdb.hl — TALKING TO TMDB AND TVMAZE (tracker.worldapi.org#9/#12/#15): the one GET with the token, the TVmaze lookups, the// external ids a title may take from them, TMDB's adult flag, the poster download, and the pacing every runner keeps between// two titles. Shared by the sync (sync.hl), the backfills (backfill.hl), the completion and the credits (details.hl, credits.hl),// the person fill (people.hl), the search (search.hl), the daily run's change lists (dailysync.hl) and the collection seed// (collections.hl). Until mission 028 this was the top half of tmdbsync.hl.//// Env: TMDB_READ_TOKEN (required unless TMDB_BASE_URL points at a fake), TMDB_BASE_URL// (https://api.themoviedb.org/3), TMDB_IMAGE_URL (https://image.tmdb.org/t/p) — the gate points both at its// fake TMDB (tests/browser.mjs).import { fetch } from 'hl:fetch'import { mkDir, exists, writeFile } from 'hl:fs'import { envOr, postersDir, textOr, hasId } from './util.hl'import { stripHtml } from './sync-helpers.hl'static tmdbBase = envOr('TMDB_BASE_URL', 'https://api.themoviedb.org/3')static tmdbImageBase = envOr('TMDB_IMAGE_URL', 'https://image.tmdb.org/t/p')static tmdbToken = envOr('TMDB_READ_TOKEN', '')static tvmazeBase = envOr('TVMAZE_BASE_URL', 'https://api.tvmaze.com')// the sync can run: a token is set (never printed) — the dev .env on Loreana has one, a gate sets its ownstatic syncEnabled = tmdbToken != ''// TMDB allows 40 requests / 10 s: every runner waits this long per request it made before the next showstatic msPerRequest = 260// TVmaze allows 20 requests / 10 s (its API docs): 550 ms per lookup madestatic msPerTvmazeRequest = 550// how long a runner waits after a show before the next one (both runners: jobs.hl syncTick, tools/sync-tmdb.hl)static pauseMsAfter = (&r) => { return r.requests * msPerRequest + r.tvmazeRequests * msPerTvmazeRequest }// one GET → the parsed JSON, or { failed = '<why>' }. A refused/timed-out fetch is an Error event (absorbed// by the runner's `on Error`), the fetch then yields null.static tmdbGet = (&path) => {r = fetch(tmdbBase + path, { headers = { 'authorization' = 'Bearer ' + tmdbToken 'accept' = 'application/json' 'user-agent' = 'tracker.worldapi.org (tmdb sync)' } timeoutMs = 20000 })if (r == null || r.status == null || r.status == 0) { return { failed = 'no answer for ' + path } }if (r.status != 200) { return { failed = 'HTTP ' + r.status + ' for ' + path status = r.status } }j = r.json()if (j == null) { return { failed = 'no JSON for ' + path } }return j}// ---- external ids (tracker.worldapi.org#12) -----------------------------------------------------// an IMDb id as TMDB gives it ('tt' + digits), else nullstatic imdbOr = (&v) => {t = textOr(&v)if (t == null || t.length < 3 || t.slice(0, 2) != 'tt') { return null }let i = 2while (i < t.length) {if (!'0123456789'.includes(t[i])) { return null }i = i + 1}return t}// a positive numeric id (TMDB's tvdb_id, TVmaze's id), else nullstatic numIdOr = (&v) => { return v != null && hlTypeName(v) == 'Number' && v > 0 ? v : null }// TVmaze's lookup by one external id: { id } (null = TVmaze doesn't know it, 404) or { failed }. hl:fetch follows the// 301 to /shows/<id> by itself, so a hit answers the show's own JSON.static tvmazeLookup = (¶m, &value) => {r = fetch(tvmazeBase + '/lookup/shows?' + param + '=' + value, { headers = { 'accept' = 'application/json' 'user-agent' = 'tracker.worldapi.org (tmdb sync)' } timeoutMs = 20000 })if (r == null || r.status == null || r.status == 0) { return { failed = 'TVmaze: no answer for ' + param + '=' + value } }if (r.status == 404) { return { id = null } }if (r.status != 200) { return { failed = 'TVmaze: HTTP ' + r.status + ' for ' + param + '=' + value } }j = r.json()if (j == null || numIdOr(&j.id) == null) { return { failed = 'TVmaze: no id for ' + param + '=' + value } }return { id = j.id }}// the ids to SET on a show from TMDB's external_ids (+ TVmaze for a series): { imdbId, tvdbId, tvmzId } — each the// show's own value when it has one (never overwritten), else the found one, else null — plus { filled, tvmazeRequests,// failed }. TVmaze is asked by imdb, then by thetvdb, only while the show (after TMDB) still has no tvmzId — and only// when `askTvmaze` (the adult backfill does not: one request per title)static externalIdsFor = (&show, &ext, &isSeries, &askTvmaze) => {out = { imdbId = show.imdbId tvdbId = show.tvdbId tvmzId = show.tvmzId filled = 0 tvmazeRequests = 0 failed = null }e = ext != null ? ext : {}if (!hasId(&out.imdbId) && imdbOr(&e.imdb_id) != null) { out.imdbId = e.imdb_id out.filled = out.filled + 1 }if (!isSeries) { return out }if (!hasId(&out.tvdbId) && numIdOr(&e.tvdb_id) != null) { out.tvdbId = e.tvdb_id out.filled = out.filled + 1 }if (hasId(&out.tvmzId) || !askTvmaze) { return out }tries = []if (imdbOr('' + out.imdbId) != null) { tries.push({ param = 'imdb' value = '' + out.imdbId }) }if (numIdOr(&out.tvdbId) != null) { tries.push({ param = 'thetvdb' value = '' + out.tvdbId }) }for (t of tries) {if (!hasId(&out.tvmzId) && out.failed == null) {out.tvmazeRequests = out.tvmazeRequests + 1found = tvmazeLookup(&t.param, &t.value)if (found.failed != null) { out.failed = found.failed } else if (found.id != null) { out.tvmzId = found.id out.filled = out.filled + 1 }}}return out}// TVmaze's show WITH its episode list, one request (tracker#18: `?embed=episodes` brings the show's own summary too — before// it was `/shows/<id>/episodes`): { list, summary (HTML stripped, or null), status (tracker#35: Running / Ended / To Be// Determined / In Development, or null) } or { failed }static tvmazeEpisodes = (&tvmzId) => {r = fetch(tvmazeBase + '/shows/' + tvmzId + '?embed=episodes', { headers = { 'accept' = 'application/json' 'user-agent' = 'tracker.worldapi.org (tmdb sync)' } timeoutMs = 20000 })if (r == null || r.status == null || r.status == 0) { return { failed = 'TVmaze: no answer for episodes of ' + tvmzId } }if (r.status != 200) { return { failed = 'TVmaze: HTTP ' + r.status + ' for episodes of ' + tvmzId } }j = r.json()if (j == null || j._embedded == null || j._embedded.episodes == null || j._embedded.episodes.length == null) { return { failed = 'TVmaze: no episode list for ' + tvmzId } }return { list = j._embedded.episodes summary = stripHtml(&j.summary) status = textOr(&j.status) type = textOr(&j.type) }}// tracker#35: TVmaze's show alone (`GET /shows/<id>`, one request) — { status, type (#43) (or null) }, { notFound = true } (404) or { failed }// (no answer, 429, 5xx: ask again later). The status backfill (backfill.hl) asks it for a series the sync has not brought one for.static tvmazeShow = (&tvmzId) => {r = fetch(tvmazeBase + '/shows/' + tvmzId, { headers = { 'accept' = 'application/json' 'user-agent' = 'tracker.worldapi.org (tmdb sync)' } timeoutMs = 20000 })if (r == null || r.status == null || r.status == 0) { return { failed = 'TVmaze: no answer for show ' + tvmzId } }if (r.status == 404) { return { notFound = true failed = 'TVmaze: HTTP 404 for show ' + tvmzId } }if (r.status != 200) { return { failed = 'TVmaze: HTTP ' + r.status + ' for show ' + tvmzId } }j = r.json()if (j == null) { return { failed = 'TVmaze: no JSON for show ' + tvmzId } }return { status = textOr(&j.status) type = textOr(&j.type) }}// ---- posters ----------------------------------------------------------------------------------// the extension of a TMDB poster_path ('/abc.jpg' → 'jpg'); only the ones the poster route knowsstatic posterExt = (&posterPath) => {parts = posterPath.split('.')ext = parts[parts.length - 1].toLowerCase()if (ext == 'jpg' || ext == 'jpeg' || ext == 'png' || ext == 'webp') { return ext }return null}// downloads the w342 poster when the file is missing or TMDB has a new one; answers { requests, saved, failed, image, tmdbPoster,// fromBackdrop } — the caller stores image / tmdbPoster / posterFromBackdrop on its record. The file is <oldId>.<ext>, the name// shows.hl posterName() already builds, served by images.hl (/posters/:name).// tracker#33 (mission 032, The Remaining): a title TMDB has NO poster for gets its BACKDROP (w780, 16:9) as the poster file// instead (`fromBackdrop`); every poster box shows it centre-cropped to 2:3 (CSS aspect-ratio + object-fit: cover). When TMDB// later has a poster, it replaces the backdrop (a new path).static syncPoster = (&show, posterPath, backdropPath) => {out = { requests = 0 saved = 0 failed = null image = show.image tmdbPoster = show.tmdbPoster fromBackdrop = show.posterFromBackdrop == true }let path = textOr(&posterPath)let size = '/w342'let fromBackdrop = falseif (path == null && textOr(&backdropPath) != null) {path = backdropPathsize = '/w780'fromBackdrop = true}if (path == null || show.oldId == null) { return out }ext = posterExt(&path)if (ext == null) { return out }file = postersDir + '/' + show.oldId + '.' + extif (exists(file) && show.tmdbPoster == path && show.image == ext) { return out }out.requests = 1r = fetch(tmdbImageBase + size + path, { headers = { 'user-agent' = 'tracker.worldapi.org (tmdb sync)' } timeoutMs = 20000 })if (r == null || r.status != 200 || r.body == null || r.body.length == 0) {out.failed = (fromBackdrop ? 'backdrop ' : 'poster ') + path + ': ' + (r == null ? 'no answer' : 'HTTP ' + r.status)return out}mkDir(postersDir)writeFile(file, toBytes(r.body), 420)out.saved = 1out.image = extout.tmdbPoster = pathout.fromBackdrop = fromBackdropreturn out}// TMDB's adult flag of a details/search answer: true / false, null when it has none (mission 054)static adultOf = (&j) => {if (j == null) { return null }if (j.adult == true) { return true }if (j.adult == false) { return false }return null}
Branches
Latest commits
- 8e174918Merge branch 'records-hm55b' into mainTracker
- b1ecee57|||PR records: HM55 stateTracker
- 28eab08cMerge branch 'records-hm55' into mainTracker
- d3edaa05|||PR records: HM55 start, PROCEDURES.mdTracker
- 3059f86cMerge branch 'records-design-rule' into mainTracker
- 421a4809|||PR records: his-design rule (Apollo 2026-11-13)Tracker
- d1390692Merge branch 'records-dp75b' into mainTracker
- a4d1a7d2|||PR records: later steps checked, branch auditTracker
- e070e16fMerge branch 'records-dp75' into mainTracker
- 642b4fa8|||PR records: DP75 day62 live, AP62 briefTracker
- bfc4ea4dMerge branch 'ap62' into mainTracker
- 2c95ac46|||PR AP62: re-vendor Hybriel set day62 (source 70341ad2, bin a54f5f08)Tracker
- b9c7c3c8Merge branch 'records-dp74' into mainTracker
- 85cfb4ae|||PR records: AP61 day61, DP74 liveTracker
- b1496147Merge branch 'ap61' into mainTracker
- 2047955bMerge branch 'u12-gateports' into mainTracker
- c8da40f3|||PR AP61: re-vendor Hybriel set day61 (source a05af47c, bin a11042e5)Tracker
- 4e018678|||PR deploy.sh: gates use Tracker's ports 8716-8719 and LC_ALL=C; README: 2 backups keptTracker
- 0b86324aMerge branch 'records-dp73' into mainTracker
- 9ce5f59b|||PR records: DP73 live 613ae0e; deploy.sh keeps 2 backupsTracker