tracker
All repositories: gitoria
11.9 KB
// lib/dailysync.hl — WHICH TITLES THE DAILY SYNC ASKS FOR (tracker.worldapi.org#18; deltasync.hl before mission 028). Creator:// "it checks every show in a request to the api? cause they have an endpoint that gives a delta on bulk". Before: 2 requests// per FOLLOWED show every day (256 for 126 shows), the other titles never. Now the daily run (jobs.hl syncTick,// tools/sync-tmdb.hl --delta) first asks what changed since the last run and syncs ONLY those titles of ours — followed or not://// GET <TMDB_BASE_URL>/tv/changes?start_date=<last run day>&end_date=<today>&page=<n> changed TV ids, 100 per page// GET <TMDB_BASE_URL>/movie/changes?start_date=…&end_date=…&page=<n> changed movie ids, 100 per page// GET <TVMAZE_BASE_URL>/updates/shows?since=day|week|month { "<tvmaze id>": <updated at> }//// → intersected with our titles (search.hl's index: TMDB id by kind, TVmaze id — no table scan) → the sync's queue, FOLLOWED// titles first. One change-list request per clock step (jobs.hl), 260 ms per TMDB / 550 ms per TVmaze request between them.// A FOLLOWED title gets the full step (all seasons, TVmaze — as before #18); one NOBODY follows the light step (sync.hl// syncShowDelta: details, ids, poster, record, cast + only the seasons that can have changed, no TVmaze) — so TVmaze's list// only brings followed series — tracker#35 (mission 030): and, for a series NOBODY follows, its TVmaze STATUS (the page's pill// prefers TVmaze's): named by TVmaze's list alone → a status-only step (sync.hl syncTvmazeStatus, one TVmaze request), named by// both lists → the light step asks it too. `syncRunStep` picks the step.// Left out: titles without a TMDB id, incomplete ones (shows.hl isIncomplete — the details repair completes them) and ADULT// ones nobody follows (never public, their posters are not kept — mission 056).//// THE LAST RUN: `<storage>/sync-state.txt` = the day ('YYYY-MM-DD', UTC) the last finished run STARTED (its change window began// at 00:00 of that day, so two runs overlap by a few hours, never leave a gap). FULL WALK (every followed show, as before #18)// when there is no such day (the first run), when it is more than `maxDeltaDays` (14: TMDB's change lists reach 14 days back)// ago, or when a change list cannot be read (then the day is NOT advanced: the next run asks for the missed days too).import { fetch } from 'hl:fetch'import { now } from 'hl:time'import { readFile, writeFile, exists } from 'hl:fs'import { storageDir, textOr, hasId } from './util.hl'import { showsTable, isIncomplete } from './shows.hl'import { tmdbGet, tvmazeBase, numIdOr } from './tmdb.hl'import { syncShow, syncShowDelta, syncTvmazeStatus } from './sync.hl'import { dayNumber } from './sync-helpers.hl'import { allFollowedShowIds } from './follows.hl'import { titleIdByTmdb, titleIdByTvmaze } from './search.hl'static maxDeltaDays = 14// TMDB answers at most 500 pages of a liststatic maxPages = 500static stateFile = storageDir + '/sync-state.txt'// the day of the last finished run, or nullstatic lastSyncDay = () => {if (!exists(stateFile)) { return null }t = readFile(stateFile)if (t == null || hlTypeName(t) != 'String') { return null }d = t.trim()return dayNumber(&d) != null ? d.slice(0, 10) : null}static markSynced = (&day) => {writeFile(stateFile, day, 420)return true}// ---- the plan of one run (one run at a time: the app's clock or the tool) --------------------------------------------------// flat statics written key by key (like people.hl's maps); `run.no` tells this run's entries from older onesstatic run = { no = 0 mode = '' why = '' since = '' until = '' tvmazeSince = 'day' phase = 'done' page = 1 pages = 1requests = 0 tvmazeRequests = 0 tvChanged = 0 movieChanged = 0 tvmazeChanged = 0 count = 0 followed = 0 statusOnly = 0 failed = '' }static followedRun = {}static pickedRun = {}static pickedAt = {}// tracker#35: named by TVmaze's change list (this run) · picked for its TVmaze status alonestatic mazeRun = {}static statusOnlyRun = {}static followedList = { ids = [] }// a run starts: { mode ('delta' | 'full'), why, since, days }. 'delta' → call planStep() until it answers done, then planIds().// `sinceDay` (the tool's --since) stands in for the stored last day; null = the stored onestatic planStart = (today, &sinceDay) => {run.no = run.no + 1run.since = ''run.until = todayrun.phase = 'done'run.page = 1run.pages = 1run.requests = 0run.tvmazeRequests = 0run.tvChanged = 0run.movieChanged = 0run.tvmazeChanged = 0run.count = 0run.followed = 0run.statusOnly = 0run.failed = ''last = sinceDay != null ? sinceDay : lastSyncDay()days = last != null && dayNumber(&today) != null ? dayNumber(&today) - dayNumber(&last) : nullif (last == null) {run.mode = 'full'run.why = 'no last run'} else if (days == null || days > maxDeltaDays || days < 0) {run.mode = 'full'run.why = 'last run ' + last + ', ' + days + ' days ago > ' + maxDeltaDays} else {run.mode = 'delta'run.why = 'since ' + last + ' (' + days + (days == 1 ? ' day' : ' days') + ')'run.since = lastrun.tvmazeSince = days <= 1 ? 'day' : (days <= 7 ? 'week' : 'month')run.phase = 'tv'ids = allFollowedShowIds()for (id of ids) { followedRun[id] = run.no }followedList.ids = ids}return { mode = run.mode why = run.why since = run.since days = days }}// is this title followed by anyone (as of this run's start)? — such a title gets the FULL step (all seasons + TVmaze, as// before #18), one nobody follows the LIGHT one (sync.hl syncShowDelta: changed seasons, no TVmaze)static isFollowedInRun = (&showId) => { return followedRun[showId] == run.no }// one of ours that changed → into the queue once, when the daily sync may touch it. `viaTvmaze`: found by TVmaze's list — only// a followed series counts there (TVmaze is merged for the followed ones only)static pick = (showId, &viaTvmaze) => {if (showId == null || pickedRun[showId] == run.no) { return false }s = showsTable.fetch(showId)followed = followedRun[showId] == run.noif (s == null || s.tmdbId == null || isIncomplete(&s) || (s.adult == true && !followed) || (viaTvmaze && !followed)) { return false }pickedRun[showId] = run.nopickedAt['' + run.count] = showIdrun.count = run.count + 1if (followed) { run.followed = run.followed + 1 }return true}// tracker#35: a series nobody follows, only on TVmaze's list → into the queue for its TVmaze status alone (one picked by TMDB's// list already gets it with its light step)static pickStatus = (showId) => {if (showId == null || pickedRun[showId] == run.no || followedRun[showId] == run.no) { return false }s = showsTable.fetch(showId)if (s == null || s.type != 'series' || !hasId(&s.tvmzId) || s.adult == true || s.mergedInto != null || isIncomplete(&s)) { return false }pickedRun[showId] = run.nostatusOnlyRun[showId] = run.nopickedAt['' + run.count] = showIdrun.count = run.count + 1run.statusOnly = run.statusOnly + 1return true}// ONE title of the run (jobs.hl syncTick, tools/sync-tmdb.hl): a full walk or a followed title → the full step; a series only on// TVmaze's list → its TVmaze status; any other → the light step (+ the TVmaze status when TVmaze's list named it too)static syncRunStep = (&showId, &delta) => {if (!delta || followedRun[showId] == run.no) { return syncShow(&showId) }if (statusOnlyRun[showId] == run.no) { return syncTvmazeStatus(&showId) }return syncShowDelta(&showId, mazeRun[showId] == run.no)}static isStatusOnlyInRun = (&showId) => { return statusOnlyRun[showId] == run.no }// ONE change-list request (TMDB tv page, TMDB movie page or TVmaze's list): { done, requests, tvmazeRequests, failed ('' = none) }static planStep = () => {out = { done = false requests = 0 tvmazeRequests = 0 failed = '' }if (run.phase == 'tv' || run.phase == 'movie') {kind = run.phaseout.requests = 1run.requests = run.requests + 1let j = tmdbGet('/' + kind + '/changes?start_date=' + run.since + '&end_date=' + run.until + '&page=' + run.page)if (j.failed != null || j.results == null) {run.failed = j.failed != null ? j.failed : 'no results in /' + kind + '/changes'run.phase = 'done'out.failed = run.failedout.done = truereturn out}type = kind == 'tv' ? 'series' : 'movie'for (c of j.results) {if (c != null && numIdOr(&c.id) != null) {if (kind == 'tv') { run.tvChanged = run.tvChanged + 1 } else { run.movieChanged = run.movieChanged + 1 }let sid = titleIdByTmdb(&type, &c.id)if (sid != null) { pick(sid, false) }}}run.pages = j.total_pages != null && hlTypeName(j.total_pages) == 'Number' ? j.total_pages : 1if (run.pages > maxPages) { run.pages = maxPages }if (run.page < run.pages) { run.page = run.page + 1 } else {run.phase = kind == 'tv' ? 'movie' : 'tvmaze'run.page = 1run.pages = 1}return out}if (run.phase == 'tvmaze') {out.tvmazeRequests = 1run.tvmazeRequests = run.tvmazeRequests + 1run.phase = 'done'f = fetch(tvmazeBase + '/updates/shows?since=' + run.tvmazeSince, { headers = { 'accept' = 'application/json' 'user-agent' = 'tracker.worldapi.org (tmdb sync)' } timeoutMs = 30000 })let j = f != null && f.status == 200 ? f.json() : nullif (j == null) {run.failed = 'TVmaze: ' + (f == null || f.status == null || f.status == 0 ? 'no answer' : 'HTTP ' + f.status) + ' for /updates/shows?since=' + run.tvmazeSinceout.failed = run.failedout.done = truereturn out}for (k of j.keys()) {run.tvmazeChanged = run.tvmazeChanged + 1let sid = titleIdByTvmaze(&k)if (sid != null) {mazeRun[sid] = run.noif (!pick(sid, true)) { pickStatus(sid) }}}}out.done = truereturn out}// the queue: the changed titles of ours, the followed ones first (in follow-table order), then the rest (in change-list order)static planIds = () => {out = []for (id of followedList.ids) { if (pickedRun[id] == run.no) { out.push(id) } }let i = 0while (i < run.count) {let id = pickedAt['' + i]if (followedRun[id] != run.no) { out.push(id) }i = i + 1}return out}// the log line of a finished planstatic planLine = () => {return 'changes ' + run.why + ': tmdb tv=' + run.tvChanged + ' movie=' + run.movieChanged + ' tvmaze(' + run.tvmazeSince + ')=' + run.tvmazeChanged+ ' → ' + run.count + ' of our titles (' + run.followed + ' followed' + (run.statusOnly > 0 ? ', ' + run.statusOnly + ' TVmaze status only' : '') + '), changeRequests=' + run.requests + ' tvmazeChangeRequests=' + run.tvmazeRequests+ (run.failed != '' ? ' FAILED: ' + run.failed : '')}// ---- the copied summaries (tracker#18, one time; mission 070: MOVED, never deleted) -------------------------------------------// The migration copied TMDB's text into `summary` too (9,648 of 10,011 titles on the live copy, 2026-10-01): such a `summary` is// no text of the creator's — it is MOVED to `migratedSummary` (kept in the record, shown nowhere) and `summary` is emptied, so the// CURRENT tmdbSummary shows (shows.hl summaryOf). Nothing is lost: the conductor's rule (mission 070) — no step destroys migrated// data. A `summary` that differs is the creator's own and stays. ONCE: `<storage>/summaries-moved.txt` records that it ran (and// the counts). → { ran, moved, kept, errors, ms }static movedFile = storageDir + '/summaries-moved.txt'static moveCopiedSummaries = () => {out = { ran = false moved = 0 kept = 0 errors = 0 ms = 0 }if (exists(movedFile)) { return out }out.ran = truet0 = now()for (s of showsTable.find(null, null)) {if (textOr(&s.summary) != null) {if (s.summary == s.tmdbSummary) {let x = sx.migratedSummary = s.summaryx.summary = nullif (showsTable.update(x.id, x) == null) { out.errors = out.errors + 1 } else { out.moved = out.moved + 1 }} else { out.kept = out.kept + 1 }}}showsTable.persist()out.ms = now() - t0writeFile(movedFile, 'moved=' + out.moved + ' kept=' + out.kept + ' errors=' + out.errors, 420)return out}
Branches
Latest commits
- fafddbf9Merge hm64-catalog: tracker#64 catalog from packed entriesmre
- b2e2206dlog: #63 livemre
- 5831e0e0log: HM64 donemre
- 4042b923Flatten catalog membership rows into paired slotsmre
- ea8a759elog: #61 #62 live, DP63 and HM64 startedmre
- 40439456Merge hm63-searchtext: tracker#63 packed search textmre
- b7e6b48ePack normalized search text behind numeric offsetsmre
- b35f53e1log: DP62 stale runtime decisionmre
- f9593602log: Apollo deploy rule correctionmre
- 3c95f7f3log: DP62 and HM63 startedmre
- 9c4397c9mission HM63mre
- 8dfe42c8mission DP62mre
- e347fef4Merge rv44: tracker#61 #62 on Hybriel day44mre
- 8274bcf1log: RV44 done, deploy held for the usage windowmre
- 1ef7a3b1RV44: report day44 gates and migration/restart proofsmre
- a5f62f6cRV44: re-vendor Hybriel set day44 (source 222edef7, bin 4f0f263e)mre
- 6159a248Merge branch 'hm62-personindex' into rv44mre
- a8923d93log: RV44 startedmre
- cf28cf77mission RV44mre
- 585782dfLog: tracker#60 Chrome look, STATUS rewrittenmre