gitoriaLog in with ident

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit20e09d8920e09d89Merge t18 (tracker#18 delta sync) into main (mission 070): conflicts README/STATUS/show.hl/project.hl/browser.mjs, both sides kept; pageShowOf summary = summaryOfmre20e09d89/deltasync.hl

9.8 KB

  1. // deltasync.hl — WHICH TITLES THE DAILY SYNC ASKS FOR (tracker.worldapi.org#18). Creator: "it checks every show in a request to
  2. // the api? cause they have an endpoint that gives a delta on bulk". Before: 2 requests per FOLLOWED show every day (256 for 126
  3. // shows), the other titles never. Now the daily run (project.hl syncTick, tools/sync-tmdb.hl --delta) first asks what changed
  4. // since the last run and syncs ONLY those titles of ours — followed or not:
  5. //
  6. // GET <TMDB_BASE_URL>/tv/changes?start_date=<last run day>&end_date=<today>&page=<n> changed TV ids, 100 per page
  7. // GET <TMDB_BASE_URL>/movie/changes?start_date=…&end_date=…&page=<n> changed movie ids, 100 per page
  8. // GET <TVMAZE_BASE_URL>/updates/shows?since=day|week|month { "<tvmaze id>": <updated at> }
  9. //
  10. // → intersected with our titles (search.hl's index: TMDB id by kind, TVmaze id — no table scan) → the sync's queue, FOLLOWED
  11. // titles first. One change-list request per clock step (project.hl), 260 ms per TMDB / 550 ms per TVmaze request between them.
  12. // A FOLLOWED title gets the full step (all seasons, TVmaze — as before #18); one NOBODY follows the light step (tmdbsync.hl
  13. // syncShowDelta: details, ids, poster, record, cast + only the seasons that can have changed, no TVmaze) — so TVmaze's list
  14. // only brings followed series.
  15. // Left out: titles without a TMDB id, incomplete ones (details.hl isIncomplete — the details repair completes them) and ADULT
  16. // ones nobody follows (never public, their posters are not kept — mission 056).
  17. //
  18. // THE LAST RUN: `<storage>/sync-state.txt` = the day ('YYYY-MM-DD', UTC) the last finished run STARTED (its change window began
  19. // 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)
  20. // when there is no such day (the first run), when it is more than `maxDeltaDays` (14: TMDB's change lists reach 14 days back)
  21. // ago, or when a change list cannot be read (then the day is NOT advanced: the next run asks for the missed days too).
  22. import { fetch } from 'hl:fetch'
  23. import { now } from 'hl:time'
  24. import { readFile, writeFile, exists } from 'hl:fs'
  25. import { showsTable, storageDir } from './shows.hl'
  26. import { tmdbGet, tvmazeBase, textOr, numIdOr, dayNumber } from './tmdbsync.hl'
  27. import { isIncomplete } from './details.hl'
  28. import { allFollowedShowIds } from './follows.hl'
  29. import { titleIdByTmdb, titleIdByTvmaze } from './search.hl'
  30. static maxDeltaDays = 14
  31. // TMDB answers at most 500 pages of a list
  32. static maxPages = 500
  33. static stateFile = storageDir + '/sync-state.txt'
  34. // the day of the last finished run, or null
  35. static lastSyncDay = () => {
  36. if (!exists(stateFile)) { return null }
  37. let t = readFile(stateFile)
  38. if (t == null || hlTypeName(t) != 'String') { return null }
  39. let d = t.trim()
  40. return dayNumber(d) != null ? d.slice(0, 10) : null
  41. }
  42. static markSynced = (day) => {
  43. writeFile(stateFile, day, 420)
  44. return true
  45. }
  46. // ---- the plan of one run (one run at a time: the app's clock or the tool) --------------------------------------------------
  47. // flat statics written key by key (like people.hl's maps); `run.no` tells this run's entries from older ones
  48. static run = { no = 0 mode = '' why = '' since = '' until = '' tvmazeSince = 'day' phase = 'done' page = 1 pages = 1
  49. requests = 0 tvmazeRequests = 0 tvChanged = 0 movieChanged = 0 tvmazeChanged = 0 count = 0 followed = 0 failed = '' }
  50. static followedRun = {}
  51. static pickedRun = {}
  52. static pickedAt = {}
  53. static followedList = { ids = [] }
  54. // a run starts: { mode ('delta' | 'full'), why, since, days }. 'delta' → call planStep() until it answers done, then planIds().
  55. // `sinceDay` (the tool's --since) stands in for the stored last day; null = the stored one
  56. static planStart = (today, sinceDay) => {
  57. run.no = run.no + 1
  58. run.since = ''
  59. run.until = today
  60. run.phase = 'done'
  61. run.page = 1
  62. run.pages = 1
  63. run.requests = 0
  64. run.tvmazeRequests = 0
  65. run.tvChanged = 0
  66. run.movieChanged = 0
  67. run.tvmazeChanged = 0
  68. run.count = 0
  69. run.followed = 0
  70. run.failed = ''
  71. let last = sinceDay != null ? sinceDay : lastSyncDay()
  72. let days = last != null && dayNumber(today) != null ? dayNumber(today) - dayNumber(last) : null
  73. if (last == null) {
  74. run.mode = 'full'
  75. run.why = 'no last run'
  76. } else if (days == null || days > maxDeltaDays || days < 0) {
  77. run.mode = 'full'
  78. run.why = 'last run ' + last + ', ' + days + ' days ago > ' + maxDeltaDays
  79. } else {
  80. run.mode = 'delta'
  81. run.why = 'since ' + last + ' (' + days + (days == 1 ? ' day' : ' days') + ')'
  82. run.since = last
  83. run.tvmazeSince = days <= 1 ? 'day' : (days <= 7 ? 'week' : 'month')
  84. run.phase = 'tv'
  85. let ids = allFollowedShowIds()
  86. for (id of ids) { followedRun[id] = run.no }
  87. followedList.ids = ids
  88. }
  89. return { mode = run.mode why = run.why since = run.since days = days }
  90. }
  91. // is this title followed by anyone (as of this run's start)? — such a title gets the FULL step (all seasons + TVmaze, as
  92. // before #18), one nobody follows the LIGHT one (tmdbsync.hl syncShowDelta: changed seasons, no TVmaze)
  93. static isFollowedInRun = (showId) => { return followedRun[showId] == run.no }
  94. // one of ours that changed → into the queue once, when the daily sync may touch it. `viaTvmaze`: found by TVmaze's list — only
  95. // a followed series counts there (TVmaze is merged for the followed ones only)
  96. static pick = (showId, viaTvmaze) => {
  97. if (showId == null || pickedRun[showId] == run.no) { return false }
  98. let s = showsTable.fetch(showId)
  99. let followed = followedRun[showId] == run.no
  100. if (s == null || s.tmdbId == null || isIncomplete(s) || (s.adult == true && !followed) || (viaTvmaze && !followed)) { return false }
  101. pickedRun[showId] = run.no
  102. pickedAt['' + run.count] = showId
  103. run.count = run.count + 1
  104. if (followed) { run.followed = run.followed + 1 }
  105. return true
  106. }
  107. // ONE change-list request (TMDB tv page, TMDB movie page or TVmaze's list): { done, requests, tvmazeRequests, failed ('' = none) }
  108. static planStep = () => {
  109. let out = { done = false requests = 0 tvmazeRequests = 0 failed = '' }
  110. if (run.phase == 'tv' || run.phase == 'movie') {
  111. let kind = run.phase
  112. out.requests = 1
  113. run.requests = run.requests + 1
  114. let j = tmdbGet('/' + kind + '/changes?start_date=' + run.since + '&end_date=' + run.until + '&page=' + run.page)
  115. if (j.failed != null || j.results == null) {
  116. run.failed = j.failed != null ? j.failed : 'no results in /' + kind + '/changes'
  117. run.phase = 'done'
  118. out.failed = run.failed
  119. out.done = true
  120. return out
  121. }
  122. let type = kind == 'tv' ? 'series' : 'movie'
  123. for (c of j.results) {
  124. if (c != null && numIdOr(c.id) != null) {
  125. if (kind == 'tv') { run.tvChanged = run.tvChanged + 1 } else { run.movieChanged = run.movieChanged + 1 }
  126. let sid = titleIdByTmdb(type, c.id)
  127. if (sid != null) { pick(sid, false) }
  128. }
  129. }
  130. run.pages = j.total_pages != null && hlTypeName(j.total_pages) == 'Number' ? j.total_pages : 1
  131. if (run.pages > maxPages) { run.pages = maxPages }
  132. if (run.page < run.pages) { run.page = run.page + 1 } else {
  133. run.phase = kind == 'tv' ? 'movie' : 'tvmaze'
  134. run.page = 1
  135. run.pages = 1
  136. }
  137. return out
  138. }
  139. if (run.phase == 'tvmaze') {
  140. out.tvmazeRequests = 1
  141. run.tvmazeRequests = run.tvmazeRequests + 1
  142. run.phase = 'done'
  143. let f = fetch(tvmazeBase + '/updates/shows?since=' + run.tvmazeSince, { headers = { 'accept' = 'application/json' 'user-agent' = 'tracker.worldapi.org (tmdb sync)' } timeoutMs = 30000 })
  144. let j = f != null && f.status == 200 ? f.json() : null
  145. if (j == null) {
  146. run.failed = 'TVmaze: ' + (f == null || f.status == null || f.status == 0 ? 'no answer' : 'HTTP ' + f.status) + ' for /updates/shows?since=' + run.tvmazeSince
  147. out.failed = run.failed
  148. out.done = true
  149. return out
  150. }
  151. for (k of j.keys()) {
  152. run.tvmazeChanged = run.tvmazeChanged + 1
  153. let sid = titleIdByTvmaze(k)
  154. if (sid != null) { pick(sid, true) }
  155. }
  156. }
  157. out.done = true
  158. return out
  159. }
  160. // the queue: the changed titles of ours, the followed ones first (in follow-table order), then the rest (in change-list order)
  161. static planIds = () => {
  162. let out = []
  163. for (id of followedList.ids) { if (pickedRun[id] == run.no) { out.push(id) } }
  164. let i = 0
  165. while (i < run.count) {
  166. let id = pickedAt['' + i]
  167. if (followedRun[id] != run.no) { out.push(id) }
  168. i = i + 1
  169. }
  170. return out
  171. }
  172. // the log line of a finished plan
  173. static planLine = () => {
  174. return 'changes ' + run.why + ': tmdb tv=' + run.tvChanged + ' movie=' + run.movieChanged + ' tvmaze(' + run.tvmazeSince + ')=' + run.tvmazeChanged
  175. + ' → ' + run.count + ' of our titles (' + run.followed + ' followed), changeRequests=' + run.requests + ' tvmazeChangeRequests=' + run.tvmazeRequests
  176. + (run.failed != '' ? ' FAILED: ' + run.failed : '')
  177. }
  178. // ---- the copied summaries (tracker#18, one time) ---------------------------------------------------------------------------
  179. // 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
  180. // no text of the creator's — it is cleared, so the CURRENT tmdbSummary shows (shows.hl summaryOf). A `summary` that differs is
  181. // the creator's own and stays. ONCE: `<storage>/summaries-cleared.txt` records that it ran (and the counts). → { ran, cleared, kept }
  182. static clearedFile = storageDir + '/summaries-cleared.txt'
  183. static clearCopiedSummaries = () => {
  184. let out = { ran = false cleared = 0 kept = 0 errors = 0 ms = 0 }
  185. if (exists(clearedFile)) { return out }
  186. out.ran = true
  187. let t0 = now()
  188. for (s of showsTable.find(null, null)) {
  189. if (textOr(s.summary) != null) {
  190. if (s.summary == s.tmdbSummary) {
  191. let x = s
  192. x.summary = null
  193. if (showsTable.update(x.id, x) == null) { out.errors = out.errors + 1 } else { out.cleared = out.cleared + 1 }
  194. } else { out.kept = out.kept + 1 }
  195. }
  196. }
  197. showsTable.persist()
  198. out.ms = now() - t0
  199. writeFile(clearedFile, 'cleared=' + out.cleared + ' kept=' + out.kept + ' errors=' + out.errors, 420)
  200. return out
  201. }

Branches

Latest commits

  • 20e09d89Merge t18 (tracker#18 delta sync) into main (mission 070): conflicts README/STATUS/show.hl/project.hl/browser.mjs, both sides kept; pageShowOf summary = summaryOfmre
  • f83571c3tracker#18 (mission 067): daily sync by change lists — TMDB /tv|movie/changes (since the stored day, paged) + TVmaze /updates/shows → only our changed titles (followed: full step, unfollowed: light step — changed seasons, no TVmaze), full walk on first run / gap > 14 days / failed list; show record refreshed (title, tmdbSummary, tagline, status, genres …; renamed titles re-indexed); summary = the creator's own text (page: summary > tmdbSummary > tvmazeSummary), one-time clear of copied summaries (9,647 on the live copy); gate 261, tests/realdata-018*.mjs, README + STATUSmre
  • f2b00674Merge t26 (tracker#26 + #28) into main (mission 062): short ids for every new person (castPersonId, guest route), guest stars stored on the title and created as people only when opened (/person/tmdb/<id>?show=<id> → 302), lean watch/follow clicks (showRow a small object, cast/crew from the slug, watches cached per user, face rows only after a season toggle); gate 311, tests/realdata-062.mjs, README + STATUSmre
  • fa1f9dfatracker#29 (mission 063): unwatched check muted grey outline + check (accent only on hover), watched stays solid — no code regression, the accent outline read as ticked; gate checks real checks visibly (computed style + screenshot pixel) on /my/unwatched, show, movie, /my/movies; gate 266, tests/realdata-063.mjs, README + STATUSmre
  • 10bb3f93tracker#28 (mission 061): full cast (all seasons, main cast by episodes, guest stars) + crew (created by, directed by, written by, screenplay, story, music) — stored by the details completion, the daily sync, the search import (one details request) and a background credits job (resumes, RSS limit); show page collapsed after 20 with client-side Show all; showBySlug via a slug map; gate 249, tests/realdata-028.mjs, README + STATUSmre
  • d8b12d67tracker#27 (mission 060): short ids for movies, series and persons — old 702 kept (data/old-short-ids.json), new random [a-z0-9]{5} unique across both, claimed at creation, background backfill (resumes), shown under poster/photo, /<shortId> → 301; gate 259, tests/realdata-060*, README + STATUSmre
  • 25a50bc4tracker#26 (mission 059): titles from a filmography are completed — on open (skeleton, step-wise face showComplete, no reload) and by the in-app details repair (resumes, TMDB-paced, series in parts); cast from TMDB credits; gate 231, tests/realdata-026.mjs, README + STATUSmre
  • f14db671tracker#22-#25 (mission 058): episode air dates, season check = all episodes watched, movie watched check (+ /my/movies count), /genres/<genre> pages (movies + series, newest first, paginated); gate 238, tests/realdata-058.mjs, README + STATUSmre
  • 1704ec45tracker#17 (mission 057): season caret down/up, skeleton rows while a season loads, sessionless showSeasonEpisodes face (no page re-mount), client-only close; gate 214, tests/realdata-057.mjs, README + STATUSmre
  • f2fe3e36mission 056: README + STATUS (merge, fixes, Hybriel 8590df63, real-data check), tests/realdata-056.mjs, tools/check-public-slugs.hlmre
  • f40c250emission 056: re-vendor hybriel master 8590df63 (#121, #122); an adult title's page is Not found for non-followers; gate: leave the page before stopping the servermre
  • 2b7fdd6cmission 056: signed-out header one row on phones ("Log in", nowrap), backfill skips adult titles' posters, gate checksmre
  • 2c53d5efMerge branch 't16-person' (tracker#16 person pages) into main; filmography shows only public titles (054 adult flag), gate race fix (backfill start line)mre
  • c171227emission 054: hide adult/unknown titles from the public lists and the search; in-app adult-flag backfill (TMDB details + poster per title, resumes), gate + real-data proofmre
  • 139fafd8tracker#16: short bio (4 lines, click = all), real-data check script, README + STATUSmre
  • 93be9476tracker#16: person pages /person/<slug> with the filmography fetched from TMDB on the first visit (step by step), gatemre
  • 47a3cae6STATUS: mission 053 merge commit idsmre
  • dcc5eecaMerge branch 't14-search'mre
  • 03edc783Merge branch 't15-tvmaze'mre
  • 71b46345tracker#15: numbering check by date or title, placeholder titles in other languages, docs + real-data proofmre