gitoriaLog in with ident

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmain5cb85d75deploy.sh: a backup taken while a background job writes (tar exit 1) is a warning; archive checked with gzip -tmremain/lib/sync.hl

26.8 KB

  1. // lib/sync.hl — THE TMDB SYNC OF ONE TITLE (tracker.worldapi.org#9; tmdbsync.hl before mission 028 — its requests are in tmdb.hl,
  2. // its small helpers in sync-helpers.hl, the backfills in backfill.hl): for every show SOMEONE follows, fetch the show's
  3. // details, seasons and episodes from TMDB and bring storage/mpackdb/ up to date, plus the show's poster.
  4. // Used by BOTH runners: the daily run inside the app (jobs.hl `syncTick`, one show per step) and the
  5. // command-line tool (tools/sync-tmdb.hl). Reference for the API use: the old tracker's
  6. // crates/sync/commands/sync.js (read-only).
  7. //
  8. // GET <TMDB_BASE_URL>/tv/<tmdbId>?append_to_response=external_ids,aggregate_credits the details (status,
  9. // counts, overview, poster_path, seasons[])
  10. // + external_ids (imdb_id, tvdb_id) + the cast/crew
  11. // of every season (tracker#28: the answer goes back
  12. // as `res.details`, the caller stores the credits —
  13. // credits.hl applyCredits; no extra request)
  14. // GET <TMDB_BASE_URL>/tv/<tmdbId>?append_to_response=season/1,season/2… up to 20 seasons with their
  15. // episodes in ONE request
  16. // GET <TMDB_IMAGE_URL>/w342<poster_path> the poster → storage/mpackdb/posters/
  17. // Authorization: Bearer $TMDB_READ_TOKEN
  18. // GET <TVMAZE_BASE_URL>/lookup/shows?imdb=tt… (else ?thetvdb=<id>) the TVmaze id, ONLY when the show
  19. // has none (301 → /shows/<id>, 404 = unknown)
  20. // GET <TVMAZE_BASE_URL>/shows/<tvmzId>?embed=episodes a SERIES on TVmaze with its episodes (#15), see
  21. // below; its summary → `tvmazeSummary` (tracker#18),
  22. // its status → `tvmazeStatus` (tracker#35)
  23. // A MOVIE (`type = 'movie'`, 7814 of the migrated rows, 1 followed): only
  24. // GET <TMDB_BASE_URL>/movie/<tmdbId>?append_to_response=external_ids,credits its imdb id + poster (+ credits); no seasons,
  25. // status, overview (it is no TV show)
  26. //
  27. // EXTERNAL IDS (tracker.worldapi.org#12, the show page's link icons): a MISSING imdbId/tvdbId is filled from TMDB's
  28. // external_ids, a missing tvmzId from TVmaze's lookup — an id the show already has is NEVER overwritten.
  29. // RULES (ticket): new seasons/episodes are ADDED, existing ones UPDATED (title, summary, release) —
  30. // matched by tmdbId, never duplicated; NOTHING is deleted. An episode without a tmdbId (161 migrated ones)
  31. // is matched by its S/E number instead and gets the tmdbId. Tracker#39 (mission 032): so is an episode whose tmdbId TMDB no longer
  32. // lists in that season — TMDB re-creates episodes now and then (Scrubs S1E1: 326465 → 6992247); the old id is kept as
  33. // `migratedTmdbId`. Before, such a migrated episode stayed and a SECOND one of the same number was added (850 on the live copy,
  34. // merged by merge.hl). A season (no tmdbId in the migrated data) is
  35. // matched by (show, seasonNumber). A TMDB value that is empty/null never overwrites a stored one.
  36. // Season 0 ("Specials") is only synced for a show that already has a season 0 (else every followed show
  37. // would suddenly flood /my/unwatched with specials); a TMDB season with no episodes is not created.
  38. //
  39. // TVMAZE MERGE (tracker.worldapi.org#15): AFTER TMDB, a series with a tvmzId gets TVmaze's episode list (one request)
  40. // and TVmaze FILLS GAPS only — matched by season/episode number: a missing episode (and its season) is ADDED, an empty
  41. // title or air date of an existing one is FILLED; a value TMDB gave is never overwritten. A placeholder title
  42. // ("Episode 4", "TBA") counts as empty — and TMDB's own placeholder never overwrites a real title either (else the
  43. // two would flip it every day). When TMDB later lists the episode, the TMDB step adopts it by number (it has no tmdbId)
  44. // and TMDB's values win. TVmaze specials (no episode number) are skipped, season 0 only if the show has one. SAFETY:
  45. // when the two number a show differently (fewer than 80 % of the episodes both know air within a day of each other or
  46. // share the title — `numberingAgrees`), TVmaze is ignored for that show (`tvmazeSkipped`). Summaries are not taken
  47. // (TVmaze's are HTML; the ticket asks for episodes, air dates, titles). The SHOW's TVmaze summary is kept, HTML stripped, as
  48. // `tvmazeSummary` (tracker#18; the page shows summary > tmdbSummary > tvmazeSummary, shows.hl summaryOf).
  49. //
  50. // WHICH TITLES, WHEN (tracker#18): the daily run asks TMDB's and TVmaze's change lists first and syncs only the titles of ours
  51. // that changed — dailysync.hl. THE SHOW RECORD (title, tagline, homepage, status, genres …) is refreshed from the same details
  52. // answer by details.hl `applySynced`.
  53. //
  54. // IDS ARE SELF-ASSIGNED (randomBytes, like watches.hl): hl:mpackdb's own generated @id can repeat within
  55. // one millisecond, which is what made update() lose rows (hybriel#113, fixed upstream, not in the vendored
  56. // build) — a table holding only unique ids is safe to update().
  57. //
  58. // ONE PROCESS PER STORAGE: hl:mpackdb does not coordinate two processes (hybriel#21) — the in-app run uses
  59. // the app's own open tables (imported from shows.hl); the tool only ever runs against a stopped app or a copy.
  60. import { now } from 'hl:time'
  61. import { newId, textOr } from './util.hl'
  62. import { showsTable, seasonsTable, episodesTable, kindOfTitle } from './shows.hl'
  63. import { tmdbGet, numIdOr, externalIdsFor, tvmazeEpisodes, tvmazeShow, syncPoster, adultOf } from './tmdb.hl'
  64. import { isPlaceholderTitle, numberingAgrees, wantedSeasonNumbers, batchesOf, changedText, isMigratedRow } from './sync-helpers.hl'
  65. // Merge TVmaze's episodes of a series into what we have AFTER the TMDB step (gaps only, rules at the top).
  66. // `seasonsByNumber` / `epByNumber` = the show's seasons by number and episodes by '<s>x<e>', as the TMDB step left them.
  67. // Answers { tvmazeRequests, newSeasons, newEpisodes, filledEpisodes, skipped, errors, error, showSeasons }.
  68. static mergeTvmaze = (title, &showKey, tvmzId, seasonsIn, episodesIn, &showSeasonsIn) => {
  69. seasonsByNumber = seasonsIn
  70. epByNumber = episodesIn
  71. out = { tvmazeRequests = 1 newSeasons = 0 newEpisodes = 0 filledEpisodes = 0 skipped = 0 errors = 0 error = '' showSeasons = showSeasonsIn summary = null status = null }
  72. got = tvmazeEpisodes(tvmzId)
  73. if (got.failed != null) { out.errors = 1 out.error = title + ': ' + got.failed return out }
  74. out.summary = got.summary
  75. out.status = got.status
  76. // the numbered episodes TVmaze has in seasons we take (no specials; season 0 only when the show has one)
  77. list = []
  78. for (te of got.list) {
  79. if (numIdOr(te.season) != null && numIdOr(te.number) != null) { list.push(te) }
  80. else if (te.season == 0 && numIdOr(te.number) != null && seasonsByNumber['0'] != null) { list.push(te) }
  81. }
  82. if (!numberingAgrees(epByNumber, list).ok) { out.skipped = 1 return out }
  83. added = {}
  84. addedFirstDate = {}
  85. for (te of list) {
  86. let key = te.season + 'x' + te.number
  87. let e = epByNumber[key]
  88. if (e == null) {
  89. let seasonKey = seasonsByNumber['' + te.season] != null ? seasonsByNumber['' + te.season].id : null
  90. if (seasonKey == null) {
  91. if (added['' + te.season] == null) { added['' + te.season] = [] addedFirstDate['' + te.season] = textOr(te.airdate) seasonsByNumber['' + te.season] = { id = newId() isNew = true } }
  92. seasonKey = seasonsByNumber['' + te.season].id
  93. }
  94. let fresh = { id = newId() episodeNumber = te.number seasonNumber = te.season
  95. title = textOr(te.name) summary = '' release = textOr(te.airdate)
  96. show = showKey season = seasonKey tmdbId = null tvmzId = te.id oldId = 'tvmaze-episode:' + te.id }
  97. if (episodesTable.put(fresh) == null) {
  98. out.errors = out.errors + 1
  99. out.error = title + ': TVmaze episode ' + te.id + ' not stored: ' + episodesTable.lastError()
  100. } else {
  101. if (added['' + te.season] == null) { added['' + te.season] = [] }
  102. let l = added['' + te.season]
  103. l.push(fresh.id)
  104. added['' + te.season] = l
  105. epByNumber[key] = fresh
  106. out.newEpisodes = out.newEpisodes + 1
  107. }
  108. } else {
  109. let changed = false
  110. if (isPlaceholderTitle(e.title) && !isPlaceholderTitle(te.name)) { e.title = te.name changed = true }
  111. if (textOr(e.release) == null && textOr(te.airdate) != null) { e.release = te.airdate changed = true }
  112. if (changed) {
  113. if (episodesTable.update(e.id, e) == null) {
  114. out.errors = out.errors + 1
  115. out.error = title + ': episode ' + e.id + ' not filled from TVmaze: ' + episodesTable.lastError()
  116. } else {
  117. epByNumber[key] = e
  118. out.filledEpisodes = out.filledEpisodes + 1
  119. }
  120. }
  121. }
  122. }
  123. // the seasons that got episodes: an existing one is updated, a new one stored and linked to the show
  124. showSeasons = []
  125. for (sid of showSeasonsIn) { showSeasons.push(sid) }
  126. for (n of added.keys()) {
  127. let ids = added[n]
  128. let s = seasonsByNumber[n]
  129. if (ids.length > 0 && s.isNew == true) {
  130. let season = { id = s.id seasonNumber = toNumber(n) title = 'Season ' + n release = addedFirstDate[n] summary = ''
  131. show = showKey episodes = ids oldId = 'tvmaze-season:' + tvmzId + ':' + n }
  132. if (seasonsTable.put(season) == null) {
  133. out.errors = out.errors + 1
  134. out.error = title + ': TVmaze season ' + n + ' not stored: ' + seasonsTable.lastError()
  135. } else {
  136. showSeasons.push(season.id)
  137. out.newSeasons = out.newSeasons + 1
  138. }
  139. } else if (ids.length > 0) {
  140. let season = seasonsTable.fetch(s.id)
  141. let eps = []
  142. for (eid of season.episodes) { eps.push(eid) }
  143. for (eid of ids) { eps.push(eid) }
  144. season.episodes = eps
  145. if (seasonsTable.update(season.id, season) == null) {
  146. out.errors = out.errors + 1
  147. out.error = title + ': season ' + season.id + ' not updated: ' + seasonsTable.lastError()
  148. }
  149. }
  150. }
  151. out.showSeasons = showSeasons
  152. return out
  153. }
  154. // a MOVIE: one request — its imdb id (if missing) and its poster; nothing else of the record is touched. `pre` = that
  155. // request's answer when the caller already made it (details.hl firstStep, tracker#26), else null
  156. static syncMovie = (show, res, &pre) => {
  157. out = res
  158. out.requests = 1
  159. details = pre != null ? pre : tmdbGet('/movie/' + show.tmdbId + '?append_to_response=external_ids,credits')
  160. if (details.failed != null) { out.errors = 1 out.error = show.title + ': ' + details.failed return out }
  161. if (pre == null) { out.details = details }
  162. ids = externalIdsFor(show, details.external_ids, false, false)
  163. out.ids = ids.filled
  164. poster = syncPoster(show, details.poster_path, details.backdrop_path)
  165. out.requests = out.requests + poster.requests
  166. out.posters = poster.saved
  167. if (poster.failed != null) { out.errors = out.errors + 1 out.error = show.title + ': ' + poster.failed }
  168. m = show
  169. m.imdbId = ids.imdbId
  170. m.image = poster.image
  171. m.tmdbPoster = poster.tmdbPoster
  172. m.posterFromBackdrop = poster.fromBackdrop
  173. // tracker#33 (mission 032): the runtime in minutes (TMDB's 0 = unknown, not stored)
  174. if (numIdOr(details.runtime) != null && details.runtime > 0) { m.runtime = details.runtime }
  175. if (adultOf(details) != null) { m.adult = adultOf(details) }
  176. m.tmdbSync = now()
  177. if (showsTable.update(m.id, m) == null) { out.errors = out.errors + 1 out.error = show.title + ': movie not updated: ' + showsTable.lastError() }
  178. return out
  179. }
  180. // Sync ONE show (by our id). Answers counts: { requests, tvmazeRequests, newSeasons, newEpisodes, updatedEpisodes,
  181. // updatedSeasons, posters, ids, tvmazeEpisodes, tvmazeSeasons, tvmazeFilled, tvmazeSkipped, errors, error } + `details` (the
  182. // details answer with its credits when this call asked for it, else null — tracker#28: the caller stores the credits). Never throws for a TMDB failure — it is counted and the show left as is.
  183. static syncShow = (&showId) => { return syncShowPart(showId, null, null) }
  184. // the same with the details request (`/tv|movie/<id>?append_to_response=external_ids…`) already answered — `pre`, counted as
  185. // this show's first request — so details.hl (tracker#26) asks TMDB once for details, ids AND the cast
  186. static syncShowWith = (&showId, &pre) => { return syncShowPart(showId, pre, null) }
  187. // ONE PART of a series' sync (tracker#26: a talk show with 4,000 episodes took 10 s in one step, blocking every request):
  188. // `only` = { seasons (the TMDB season numbers of this part), finish (false: only those seasons — new ones linked to the show,
  189. // nothing else; true: the ids, TVmaze, poster and the show record), tvmaze (false: no TVmaze lookup/merge — tracker#18's light
  190. // step) }; null = everything at once. `pre` is required with `only` (its request is the caller's: a part counts only its own
  191. // requests).
  192. static emptyResult = () => { return { requests = 0 tvmazeRequests = 0 newSeasons = 0 newEpisodes = 0 updatedEpisodes = 0 updatedSeasons = 0 posters = 0 ids = 0 tvmazeEpisodes = 0 tvmazeSeasons = 0 tvmazeFilled = 0 tvmazeSkipped = 0 errors = 0 error = '' details = null } }
  193. static syncShowPart = (&showId, &pre, &only) => {
  194. let res = emptyResult()
  195. show = showsTable.fetch(showId)
  196. if (show == null) { res.errors = 1 res.error = 'no show ' + showId return res }
  197. if (show.tmdbId == null) { res.errors = 1 res.error = show.title + ': no tmdbId' return res }
  198. if (show.type == 'movie') { return syncMovie(show, res, pre) }
  199. res.requests = only != null ? 0 : 1
  200. details = pre != null ? pre : tmdbGet('/tv/' + show.tmdbId + '?append_to_response=external_ids,aggregate_credits')
  201. if (details.failed != null) { res.errors = 1 res.error = show.title + ': ' + details.failed return res }
  202. if (pre == null) { res.details = details }
  203. // what we have: seasons by number, episodes by tmdbId and by S/E number
  204. // (tracker#18: a part reads only ITS seasons' episodes, unless it finishes with the TVmaze merge, which compares them all —
  205. // Today, 15,716 episodes, read them all for a one-season change: 20 s and GBs of memory)
  206. tvmazeToo = only == null || only.tvmaze != false
  207. allEpisodes = only == null || (only.finish && tvmazeToo)
  208. seasonsByNumber = {}
  209. epByTmdb = {}
  210. epByNumber = {}
  211. for (sid of show.seasons) {
  212. let s = seasonsTable.fetch(sid)
  213. if (s != null) {
  214. seasonsByNumber['' + s.seasonNumber] = s
  215. let epIds = allEpisodes || only.seasons.includes(s.seasonNumber) ? s.episodes : []
  216. for (eid of epIds) {
  217. let e = episodesTable.fetch(eid)
  218. if (e != null) {
  219. if (e.tmdbId != null) { epByTmdb['' + e.tmdbId] = e }
  220. epByNumber[e.seasonNumber + 'x' + e.episodeNumber] = e
  221. }
  222. }
  223. }
  224. }
  225. // plain locals for the record literals below: inside a literal, an entry named `show`/`season` would shadow
  226. // the variable for every later entry (SPEC "A literal's entries are evaluated in order")
  227. showKey = show.id
  228. showTmdb = show.tmdbId
  229. let showSeasons = []
  230. for (sid of show.seasons) { showSeasons.push(sid) }
  231. let showChanged = false
  232. let wanted = wantedSeasonNumbers(details, seasonsByNumber)
  233. if (only != null) {
  234. part = []
  235. for (n of wanted) { if (only.seasons.includes(n)) { part.push(n) } }
  236. wanted = part
  237. }
  238. for (batch of batchesOf(wanted, 20)) {
  239. let names = []
  240. for (n of batch) { names.push('season/' + n) }
  241. res.requests = res.requests + 1
  242. let withSeasons = tmdbGet('/tv/' + show.tmdbId + '?append_to_response=' + names.join(','))
  243. if (withSeasons.failed != null) {
  244. res.errors = res.errors + 1
  245. res.error = show.title + ': ' + withSeasons.failed
  246. } else {
  247. for (n of batch) {
  248. let ts = withSeasons['season/' + n]
  249. if (ts != null && ts.episodes != null) {
  250. let season = seasonsByNumber['' + n]
  251. let seasonIsNew = season == null
  252. let seasonChanged = false
  253. if (seasonIsNew) {
  254. season = { id = newId() seasonNumber = n title = textOr(ts.name) != null ? ts.name : 'Season ' + n
  255. release = textOr(ts.air_date) summary = textOr(ts.overview) != null ? ts.overview : ''
  256. show = showKey episodes = [] oldId = 'tmdb-season:' + showTmdb + ':' + n }
  257. } else {
  258. let seasonMigrated = isMigratedRow(season)
  259. if (changedText(season.title, textOr(ts.name))) {
  260. if (seasonMigrated && season.migratedTitle == null && textOr(season.title) != null) { season.migratedTitle = season.title }
  261. season.title = ts.name seasonChanged = true
  262. }
  263. if (changedText(season.summary, textOr(ts.overview))) {
  264. if (seasonMigrated && season.migratedSummary == null && textOr(season.summary) != null) { season.migratedSummary = season.summary }
  265. season.summary = ts.overview seasonChanged = true
  266. }
  267. if (changedText(season.release, textOr(ts.air_date))) { season.release = ts.air_date seasonChanged = true }
  268. }
  269. let seasonKey = season.id
  270. let seasonEpisodes = []
  271. for (eid of season.episodes) { seasonEpisodes.push(eid) }
  272. // tracker#39: the episode ids TMDB lists in this season now — a stored one with an id not among them is stale
  273. let listed = {}
  274. for (te of ts.episodes) { if (te.id != null) { listed['' + te.id] = true } }
  275. for (te of ts.episodes) {
  276. if (te.id != null && te.episode_number != null) {
  277. let e = epByTmdb['' + te.id]
  278. if (e == null) {
  279. let byNum = epByNumber[n + 'x' + te.episode_number]
  280. if (byNum != null && (byNum.tmdbId == null || listed['' + byNum.tmdbId] != true)) { e = byNum }
  281. }
  282. if (e == null) {
  283. let fresh = { id = newId() episodeNumber = te.episode_number seasonNumber = n
  284. title = textOr(te.name) summary = textOr(te.overview) != null ? te.overview : '' release = textOr(te.air_date)
  285. show = showKey season = seasonKey tmdbId = te.id oldId = 'tmdb-episode:' + te.id }
  286. let putId = episodesTable.put(fresh)
  287. if (putId == null) {
  288. res.errors = res.errors + 1
  289. res.error = show.title + ': episode ' + te.id + ' not stored: ' + episodesTable.lastError()
  290. } else {
  291. seasonEpisodes.push(fresh.id)
  292. epByTmdb['' + te.id] = fresh
  293. epByNumber[n + 'x' + te.episode_number] = fresh
  294. res.newEpisodes = res.newEpisodes + 1
  295. seasonChanged = true
  296. }
  297. } else {
  298. let changed = false
  299. if (e.tmdbId != te.id) {
  300. if (e.tmdbId != null && e.migratedTmdbId == null) { e.migratedTmdbId = e.tmdbId }
  301. if (e.tmdbId != null) { epByTmdb['' + e.tmdbId] = null }
  302. e.tmdbId = te.id
  303. changed = true
  304. }
  305. // #15: TMDB's "Episode 4" never replaces a real title (one TVmaze filled, or the migrated one)
  306. let epMigrated = isMigratedRow(e)
  307. if (changedText(e.title, textOr(te.name)) && !(isPlaceholderTitle(te.name) && !isPlaceholderTitle(e.title))) {
  308. if (epMigrated && e.migratedTitle == null && textOr(e.title) != null) { e.migratedTitle = e.title }
  309. e.title = te.name changed = true
  310. }
  311. if (changedText(e.summary, textOr(te.overview))) {
  312. if (epMigrated && e.migratedSummary == null && textOr(e.summary) != null) { e.migratedSummary = e.summary }
  313. e.summary = te.overview changed = true
  314. }
  315. if (changedText(e.release, textOr(te.air_date))) { e.release = te.air_date changed = true }
  316. if (changed) {
  317. if (episodesTable.update(e.id, e) == null) {
  318. res.errors = res.errors + 1
  319. res.error = show.title + ': episode ' + e.id + ' not updated: ' + episodesTable.lastError()
  320. } else {
  321. epByTmdb['' + e.tmdbId] = e
  322. epByNumber[e.seasonNumber + 'x' + e.episodeNumber] = e
  323. res.updatedEpisodes = res.updatedEpisodes + 1
  324. }
  325. }
  326. }
  327. }
  328. }
  329. season.episodes = seasonEpisodes
  330. if (seasonIsNew) {
  331. if (seasonsTable.put(season) == null) {
  332. res.errors = res.errors + 1
  333. res.error = show.title + ': season ' + n + ' not stored: ' + seasonsTable.lastError()
  334. } else {
  335. seasonsByNumber['' + n] = season
  336. showSeasons.push(season.id)
  337. showChanged = true
  338. res.newSeasons = res.newSeasons + 1
  339. }
  340. } else if (seasonChanged) {
  341. if (seasonsTable.update(season.id, season) == null) {
  342. res.errors = res.errors + 1
  343. res.error = show.title + ': season ' + season.id + ' not updated: ' + seasonsTable.lastError()
  344. } else {
  345. res.updatedSeasons = res.updatedSeasons + 1
  346. }
  347. }
  348. }
  349. }
  350. }
  351. }
  352. // a part that is not the last: only the new seasons are linked to the show (the next part reads them)
  353. if (only != null && !only.finish) {
  354. if (showChanged) {
  355. show.seasons = showSeasons
  356. if (showsTable.update(show.id, show) == null) { res.errors = res.errors + 1 res.error = show.title + ': show not updated: ' + showsTable.lastError() }
  357. }
  358. return res
  359. }
  360. // the external ids (tracker.worldapi.org#12): missing ones from TMDB's external_ids, a missing TVmaze id from TVmaze
  361. ids = externalIdsFor(show, details.external_ids, true, tvmazeToo)
  362. res.ids = ids.filled
  363. res.tvmazeRequests = ids.tvmazeRequests
  364. if (ids.failed != null) { res.errors = res.errors + 1 res.error = show.title + ': ' + ids.failed }
  365. // tracker.worldapi.org#15: what TVmaze has and TMDB not yet (new episodes/seasons, empty titles and air dates)
  366. let tvmazeSummary = null
  367. let tvmazeStatus = null
  368. if (numIdOr(ids.tvmzId) != null && tvmazeToo) {
  369. tvm = mergeTvmaze(show.title, showKey, ids.tvmzId, seasonsByNumber, epByNumber, showSeasons)
  370. res.tvmazeRequests = res.tvmazeRequests + tvm.tvmazeRequests
  371. res.tvmazeEpisodes = tvm.newEpisodes
  372. res.tvmazeSeasons = tvm.newSeasons
  373. res.tvmazeFilled = tvm.filledEpisodes
  374. res.tvmazeSkipped = tvm.skipped
  375. if (tvm.errors > 0) { res.errors = res.errors + tvm.errors res.error = tvm.error }
  376. showSeasons = tvm.showSeasons
  377. tvmazeSummary = tvm.summary
  378. tvmazeStatus = tvm.status
  379. }
  380. // the show itself: status, counts, TMDB's overview, the poster, when it was synced
  381. poster = syncPoster(show, details.poster_path, details.backdrop_path)
  382. res.requests = res.requests + poster.requests
  383. res.posters = poster.saved
  384. if (poster.failed != null) { res.errors = res.errors + 1 res.error = show.title + ': ' + poster.failed }
  385. show.seasons = showSeasons
  386. if (textOr(details.status) != null) { show.status = details.status }
  387. if (details.number_of_seasons != null) { show.seasonsCount = details.number_of_seasons }
  388. if (details.number_of_episodes != null) { show.episodesCount = details.number_of_episodes }
  389. // tracker#18: TMDB's text → tmdbSummary, TVmaze's → tvmazeSummary; `summary` is the creator's own text, never written here
  390. if (textOr(details.overview) != null) { show.tmdbSummary = details.overview }
  391. if (tvmazeSummary != null) { show.tvmazeSummary = tvmazeSummary }
  392. // tracker#35: TVmaze's status beside TMDB's (`status`) — the page's pill shows TVmaze's when there is one (fresher)
  393. if (tvmazeStatus != null) { show.tvmazeStatus = tvmazeStatus }
  394. show.image = poster.image
  395. show.tmdbPoster = poster.tmdbPoster
  396. show.posterFromBackdrop = poster.fromBackdrop
  397. show.imdbId = ids.imdbId
  398. show.tvdbId = ids.tvdbId
  399. show.tvmzId = ids.tvmzId
  400. if (adultOf(details) != null) { show.adult = adultOf(details) }
  401. if (textOr(details.type) != null) { show.tmdbType = details.type show.kind = kindOfTitle(details.type, show.genres) }
  402. show.tmdbSync = now()
  403. if (showsTable.update(show.id, show) == null) { res.errors = res.errors + 1 res.error = show.title + ': show not updated: ' + showsTable.lastError() }
  404. return res
  405. }
  406. // ---- the LIGHT step of the daily delta run (tracker#18) ---------------------------------------------------------------
  407. // A changed title NOBODY follows (dailysync.hl): its details (+ ids + credits: the record, applySynced) and only the seasons
  408. // that can have changed — the ones we don't have, the ones where TMDB counts MORE episodes than we have (fewer: TVmaze's
  409. // extras, nothing to fetch), and the newest one (new or renamed episodes land there). No TVmaze (its merge only matters for the followed shows' lists; their full step still does
  410. // it). Real data (2026-10-01): Today (67 seasons / 15,716 episodes) and EastEnders (43 / 7,349) change every day; their full
  411. // sync blocked the server 20.8 s each and grew the process by ~5 GB each.
  412. static changedSeasonNumbers = (&details, &show) => {
  413. ours = {}
  414. for (sid of show.seasons) {
  415. let s = seasonsTable.fetch(sid)
  416. if (s != null) { ours['' + s.seasonNumber] = s.episodes.length }
  417. }
  418. let latest = -1
  419. for (x of (details.seasons != null ? details.seasons : [])) {
  420. if (x != null && x.season_number != null && x.season_number > latest && (x.episode_count == null || x.episode_count > 0)) { latest = x.season_number }
  421. }
  422. out = []
  423. for (x of (details.seasons != null ? details.seasons : [])) {
  424. if (x != null && x.season_number != null) {
  425. let n = x.season_number
  426. if (n == latest || ours['' + n] == null || (x.episode_count != null && x.episode_count > ours['' + n])) { out.push(n) }
  427. }
  428. }
  429. return out
  430. }
  431. // tracker#35: `withStatus` — TVmaze's change list named this series too: its TVmaze status as well (one more TVmaze request)
  432. static syncShowDelta = (&showId, &withStatus) => {
  433. show = showsTable.fetch(showId)
  434. if (show == null || show.tmdbId == null || show.type == 'movie') { return syncShow(showId) }
  435. details = tmdbGet('/tv/' + show.tmdbId + '?append_to_response=external_ids,aggregate_credits')
  436. if (details.failed != null) {
  437. let res = emptyResult()
  438. res.requests = 1
  439. res.errors = 1
  440. res.error = show.title + ': ' + details.failed
  441. return res
  442. }
  443. r = syncShowPart(showId, details, { seasons = changedSeasonNumbers(details, show) finish = true tvmaze = false })
  444. r.requests = r.requests + 1
  445. r.details = details
  446. if (withStatus) {
  447. st = syncTvmazeStatus(showId)
  448. r.tvmazeRequests = r.tvmazeRequests + st.tvmazeRequests
  449. if (st.error != '') { r.errors = r.errors + 1 r.error = st.error }
  450. }
  451. return r
  452. }
  453. // tracker#35: TVmaze's status of ONE series (`GET /shows/<tvmzId>`, one TVmaze request) → `tvmazeStatus` when it changed — the
  454. // daily delta run for a series nobody follows that TVmaze's change list names (a followed one gets TVmaze's merge, which
  455. // brings it). Answers the sync's counts (emptyResult: tvmazeRequests, errors, error; no `details`).
  456. static syncTvmazeStatus = (&showId) => {
  457. let res = emptyResult()
  458. show = showsTable.fetch(showId)
  459. if (show == null || show.tvmzId == null || ('' + show.tvmzId).trim() == '') { return res }
  460. res.tvmazeRequests = 1
  461. got = tvmazeShow(show.tvmzId)
  462. if (got.failed != null) { res.errors = 1 res.error = show.title + ': ' + got.failed return res }
  463. if (got.status != null && got.status != show.tvmazeStatus) {
  464. show.tvmazeStatus = got.status
  465. if (showsTable.update(show.id, show) == null) { res.errors = 1 res.error = show.title + ': show not updated: ' + showsTable.lastError() }
  466. }
  467. return res
  468. }
  469. // flush the three tables' index/meta state to disk (after a run, and every few shows)
  470. static persistSync = () => {
  471. showsTable.persist()
  472. seasonsTable.persist()
  473. episodesTable.persist()
  474. return true
  475. }

Branches

Latest commits

  • 5cb85d75deploy.sh: a backup taken while a background job writes (tar exit 1) is a warning; archive checked with gzip -tmre
  • 9abda75dtracker: report 035mre
  • 12595e47mission 035: theme re-vendored from layouts.worldapi.org 0222f67 (two corner radii: radiusSmall 5px, radiusLarge 10px); the tracker's 18 own radii -> radiusSmall/radiusLarge (--layout-radius is gone); check-theme 0; gates 379/0, 32/0, 53/0, 229/0, 26/0; real copy: every computed radius in {0, 5px, 10px, 50%}mre
  • e7305014tracker: report 032 (art + photos)mre
  • fbb903cctracker#33/#37 (mission 032): a title without a TMDB poster gets its backdrop (w780, posterFromBackdrop, shown 2:3 centre-cropped); movies store runtime, the page shows release date + runtime; art backfill (public titles without poster file / movies without runtime) and person photo backfill (tmdbProfile / photoCheck) as the last start jobs (TRACKER_ART, TRACKER_PHOTOS; off in every gate start); gates 379/0, 32/0, 53/0, 229/0, 26/0, check-theme 0; live copy: art 4977 titles in 42 min (115 posters, 15 backdrops, 4609 runtimes), The Remaining shows its backdrop + 7 minmre
  • 4ecc67b2tracker: STATUS/LOG for the t38 + t40 merge (gates 374/0, 32/0, 53/0, 229/0, 26/0, check-theme 0; live copy checks)mre
  • e5945d2fMerge t40 (tracker#40 curated franchises, Franchises menu, superseded collections) into main: deploy.sh lists all six gates (browser, kinds, franchises, pager, franchiseseed, check-theme); search.hl keeps #37's personPhotoOf + #40's importWithCredits; pager gate runs with TRACKER_FRANCHISE_SEED=0; gates 374/0, 32/0, 53/0, 229/0, 26/0, check-theme 0mre
  • b638e99dMerge t38 (tracker#38 pagination, #35 Returning/Airing label) into main: LOG/STATUS keep both sides; pager gate follows #37's /people (everyone, last updated first: seed updatedAt); gates pager 229/0, browser 374/0mre
  • f8ffa4dbtracker: report 032 + The Remaining + #39mre
  • 7565a863tracker: LOG timemre
  • b10f00c8tracker#39: double episodes — migrated episodes whose TMDB id TMDB replaced are adopted by their number in the sync (old id -> migratedTmdbId); merge.hl step 3 merges each season's doubles at start (keeper: most watches > synced > first; watches moved/parked; tombstones into mergedEpisodes, nothing deleted); tools/count-duplicate-episodes.hl; gate fixture + paths-m039; live copy 850 -> 0 in 64 s; gates 373/0, 32/0, 52/0mre
  • 8f1d4542tracker#40: superseded collections — a TMDB collection timeline whose titles are all in one curated timeline is hidden (supersededBy; kept: own page + editor finder), set by the collection seed when it makes one and by the curated build (lifted when the cover is gone); partly covered ones join that franchise; no second widget (First Contact: only Star Trek — Prime); gate franchiseseed 26/0, browser 365/0, kinds 32/0, franchises 53/0, check-theme 0; real copy 24 supersededmre
  • 9448d643tracker#35 follow-up (mission 033): TVmaze 'Running' is labelled 'Returning', or 'Airing' while a non-special episode of the two newest seasons is released within today +-7 days (data unchanged); gate fixtures Running/Airing/Aired + a special; browser 366/0, kinds 32/0, franchises 52/0, pager 229/0, check-theme 0mre
  • 7d4b293dtracker#40: "Franchises" in the main menu (desktop header after People, phone sidebar) → /franchises, marked on franchise and timeline pages; gates 365/0, 32/0, 53/0, 24/0, check-theme 0mre
  • 8751adb8tracker: report 032mre
  • 9bce1f65tracker mission 032: STATUS gate files + the hour-boundary flakemre
  • 718bfb89tracker#37 (mission 032): /people = everyone, last updated first (updatedAt stamped by the person fill; view built at boot, touched people first at once), photo + name tiles (person colour) with the /movies pagination, /people/<letter> removed; photo = our file, tmdbProfile, a cast/crew entry's profile (in-memory map at boot), else the new 'no photo' placeholder; new cast/crew/created_by people keep tmdbProfile; search people rows with the photo; /settings = the heading only; util.hl sortDesc starts from sorted runs (same result, 105k: 1.6 s -> 0.15 s); gates 369/0, 32/0, 52/0, check-theme 0; README/STATUS/LOGmre
  • 93dfb0batracker#40 (mission 034): the curated franchises — data/franchises.json (17 franchises, 31 timelines, 285 TMDB titles, movies + series, in-universe/release order, 12 TMDB collections attached); lib/franchiseseed.hl + jobs.hl franchiseSeedTick (last start job, imports missing titles via details.hl importWithCredits = the search's Add, one per step paced, then one build; franchiseseed.db: editor changes win, the creator's same-name franchise adopted / timeline left alone, 404 remembered, resumable, idempotent); timeline heads 'N titles · in-universe order' (orderKind) and wrap on a phone; series pages show the widget; new gate tests/franchiseseed.mjs (5th in deploy.sh), the others run with TRACKER_FRANCHISE_SEED=0; gates 365/0, 32/0, 52/0, 24/0, check-theme 0; real copy 196 imported, 0 failed, 7 min, restart unchanged=31mre
  • 03ec792ftracker#38 (mission 033): pagination goes exactly to the clicked page — tilelist read the clicked button's text after pagination.hl's own handler had rebuilt the buttons (real clicks only); now li.current, else the button's own text; new gate tests/pager.mjs (5 lists x 11 pages, 390/1280, real + script clicks, Back/Forward) 229/0; browser 365/0, kinds 32/0, franchises 52/0, check-theme 0mre
  • 96ba683adeploy.sh: a gate without a 'passed,' line (check-theme) no longer ends the scriptmre