gitoriaLog in with ident

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitb10f00c8b10f00c8tracker#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/0mreb10f00c8/lib/sync.hl

26.4 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)
  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. if (adultOf(details) != null) { m.adult = adultOf(details) }
  173. m.tmdbSync = now()
  174. if (showsTable.update(m.id, m) == null) { out.errors = out.errors + 1 out.error = show.title + ': movie not updated: ' + showsTable.lastError() }
  175. return out
  176. }
  177. // Sync ONE show (by our id). Answers counts: { requests, tvmazeRequests, newSeasons, newEpisodes, updatedEpisodes,
  178. // updatedSeasons, posters, ids, tvmazeEpisodes, tvmazeSeasons, tvmazeFilled, tvmazeSkipped, errors, error } + `details` (the
  179. // 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.
  180. static syncShow = (&showId) => { return syncShowPart(showId, null, null) }
  181. // the same with the details request (`/tv|movie/<id>?append_to_response=external_ids…`) already answered — `pre`, counted as
  182. // this show's first request — so details.hl (tracker#26) asks TMDB once for details, ids AND the cast
  183. static syncShowWith = (&showId, &pre) => { return syncShowPart(showId, pre, null) }
  184. // ONE PART of a series' sync (tracker#26: a talk show with 4,000 episodes took 10 s in one step, blocking every request):
  185. // `only` = { seasons (the TMDB season numbers of this part), finish (false: only those seasons — new ones linked to the show,
  186. // nothing else; true: the ids, TVmaze, poster and the show record), tvmaze (false: no TVmaze lookup/merge — tracker#18's light
  187. // step) }; null = everything at once. `pre` is required with `only` (its request is the caller's: a part counts only its own
  188. // requests).
  189. 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 } }
  190. static syncShowPart = (&showId, &pre, &only) => {
  191. let res = emptyResult()
  192. show = showsTable.fetch(showId)
  193. if (show == null) { res.errors = 1 res.error = 'no show ' + showId return res }
  194. if (show.tmdbId == null) { res.errors = 1 res.error = show.title + ': no tmdbId' return res }
  195. if (show.type == 'movie') { return syncMovie(show, res, pre) }
  196. res.requests = only != null ? 0 : 1
  197. details = pre != null ? pre : tmdbGet('/tv/' + show.tmdbId + '?append_to_response=external_ids,aggregate_credits')
  198. if (details.failed != null) { res.errors = 1 res.error = show.title + ': ' + details.failed return res }
  199. if (pre == null) { res.details = details }
  200. // what we have: seasons by number, episodes by tmdbId and by S/E number
  201. // (tracker#18: a part reads only ITS seasons' episodes, unless it finishes with the TVmaze merge, which compares them all —
  202. // Today, 15,716 episodes, read them all for a one-season change: 20 s and GBs of memory)
  203. tvmazeToo = only == null || only.tvmaze != false
  204. allEpisodes = only == null || (only.finish && tvmazeToo)
  205. seasonsByNumber = {}
  206. epByTmdb = {}
  207. epByNumber = {}
  208. for (sid of show.seasons) {
  209. let s = seasonsTable.fetch(sid)
  210. if (s != null) {
  211. seasonsByNumber['' + s.seasonNumber] = s
  212. let epIds = allEpisodes || only.seasons.includes(s.seasonNumber) ? s.episodes : []
  213. for (eid of epIds) {
  214. let e = episodesTable.fetch(eid)
  215. if (e != null) {
  216. if (e.tmdbId != null) { epByTmdb['' + e.tmdbId] = e }
  217. epByNumber[e.seasonNumber + 'x' + e.episodeNumber] = e
  218. }
  219. }
  220. }
  221. }
  222. // plain locals for the record literals below: inside a literal, an entry named `show`/`season` would shadow
  223. // the variable for every later entry (SPEC "A literal's entries are evaluated in order")
  224. showKey = show.id
  225. showTmdb = show.tmdbId
  226. let showSeasons = []
  227. for (sid of show.seasons) { showSeasons.push(sid) }
  228. let showChanged = false
  229. let wanted = wantedSeasonNumbers(details, seasonsByNumber)
  230. if (only != null) {
  231. part = []
  232. for (n of wanted) { if (only.seasons.includes(n)) { part.push(n) } }
  233. wanted = part
  234. }
  235. for (batch of batchesOf(wanted, 20)) {
  236. let names = []
  237. for (n of batch) { names.push('season/' + n) }
  238. res.requests = res.requests + 1
  239. let withSeasons = tmdbGet('/tv/' + show.tmdbId + '?append_to_response=' + names.join(','))
  240. if (withSeasons.failed != null) {
  241. res.errors = res.errors + 1
  242. res.error = show.title + ': ' + withSeasons.failed
  243. } else {
  244. for (n of batch) {
  245. let ts = withSeasons['season/' + n]
  246. if (ts != null && ts.episodes != null) {
  247. let season = seasonsByNumber['' + n]
  248. let seasonIsNew = season == null
  249. let seasonChanged = false
  250. if (seasonIsNew) {
  251. season = { id = newId() seasonNumber = n title = textOr(ts.name) != null ? ts.name : 'Season ' + n
  252. release = textOr(ts.air_date) summary = textOr(ts.overview) != null ? ts.overview : ''
  253. show = showKey episodes = [] oldId = 'tmdb-season:' + showTmdb + ':' + n }
  254. } else {
  255. let seasonMigrated = isMigratedRow(season)
  256. if (changedText(season.title, textOr(ts.name))) {
  257. if (seasonMigrated && season.migratedTitle == null && textOr(season.title) != null) { season.migratedTitle = season.title }
  258. season.title = ts.name seasonChanged = true
  259. }
  260. if (changedText(season.summary, textOr(ts.overview))) {
  261. if (seasonMigrated && season.migratedSummary == null && textOr(season.summary) != null) { season.migratedSummary = season.summary }
  262. season.summary = ts.overview seasonChanged = true
  263. }
  264. if (changedText(season.release, textOr(ts.air_date))) { season.release = ts.air_date seasonChanged = true }
  265. }
  266. let seasonKey = season.id
  267. let seasonEpisodes = []
  268. for (eid of season.episodes) { seasonEpisodes.push(eid) }
  269. // tracker#39: the episode ids TMDB lists in this season now — a stored one with an id not among them is stale
  270. let listed = {}
  271. for (te of ts.episodes) { if (te.id != null) { listed['' + te.id] = true } }
  272. for (te of ts.episodes) {
  273. if (te.id != null && te.episode_number != null) {
  274. let e = epByTmdb['' + te.id]
  275. if (e == null) {
  276. let byNum = epByNumber[n + 'x' + te.episode_number]
  277. if (byNum != null && (byNum.tmdbId == null || listed['' + byNum.tmdbId] != true)) { e = byNum }
  278. }
  279. if (e == null) {
  280. let fresh = { id = newId() episodeNumber = te.episode_number seasonNumber = n
  281. title = textOr(te.name) summary = textOr(te.overview) != null ? te.overview : '' release = textOr(te.air_date)
  282. show = showKey season = seasonKey tmdbId = te.id oldId = 'tmdb-episode:' + te.id }
  283. let putId = episodesTable.put(fresh)
  284. if (putId == null) {
  285. res.errors = res.errors + 1
  286. res.error = show.title + ': episode ' + te.id + ' not stored: ' + episodesTable.lastError()
  287. } else {
  288. seasonEpisodes.push(fresh.id)
  289. epByTmdb['' + te.id] = fresh
  290. epByNumber[n + 'x' + te.episode_number] = fresh
  291. res.newEpisodes = res.newEpisodes + 1
  292. seasonChanged = true
  293. }
  294. } else {
  295. let changed = false
  296. if (e.tmdbId != te.id) {
  297. if (e.tmdbId != null && e.migratedTmdbId == null) { e.migratedTmdbId = e.tmdbId }
  298. if (e.tmdbId != null) { epByTmdb['' + e.tmdbId] = null }
  299. e.tmdbId = te.id
  300. changed = true
  301. }
  302. // #15: TMDB's "Episode 4" never replaces a real title (one TVmaze filled, or the migrated one)
  303. let epMigrated = isMigratedRow(e)
  304. if (changedText(e.title, textOr(te.name)) && !(isPlaceholderTitle(te.name) && !isPlaceholderTitle(e.title))) {
  305. if (epMigrated && e.migratedTitle == null && textOr(e.title) != null) { e.migratedTitle = e.title }
  306. e.title = te.name changed = true
  307. }
  308. if (changedText(e.summary, textOr(te.overview))) {
  309. if (epMigrated && e.migratedSummary == null && textOr(e.summary) != null) { e.migratedSummary = e.summary }
  310. e.summary = te.overview changed = true
  311. }
  312. if (changedText(e.release, textOr(te.air_date))) { e.release = te.air_date changed = true }
  313. if (changed) {
  314. if (episodesTable.update(e.id, e) == null) {
  315. res.errors = res.errors + 1
  316. res.error = show.title + ': episode ' + e.id + ' not updated: ' + episodesTable.lastError()
  317. } else {
  318. epByTmdb['' + e.tmdbId] = e
  319. epByNumber[e.seasonNumber + 'x' + e.episodeNumber] = e
  320. res.updatedEpisodes = res.updatedEpisodes + 1
  321. }
  322. }
  323. }
  324. }
  325. }
  326. season.episodes = seasonEpisodes
  327. if (seasonIsNew) {
  328. if (seasonsTable.put(season) == null) {
  329. res.errors = res.errors + 1
  330. res.error = show.title + ': season ' + n + ' not stored: ' + seasonsTable.lastError()
  331. } else {
  332. seasonsByNumber['' + n] = season
  333. showSeasons.push(season.id)
  334. showChanged = true
  335. res.newSeasons = res.newSeasons + 1
  336. }
  337. } else if (seasonChanged) {
  338. if (seasonsTable.update(season.id, season) == null) {
  339. res.errors = res.errors + 1
  340. res.error = show.title + ': season ' + season.id + ' not updated: ' + seasonsTable.lastError()
  341. } else {
  342. res.updatedSeasons = res.updatedSeasons + 1
  343. }
  344. }
  345. }
  346. }
  347. }
  348. }
  349. // a part that is not the last: only the new seasons are linked to the show (the next part reads them)
  350. if (only != null && !only.finish) {
  351. if (showChanged) {
  352. show.seasons = showSeasons
  353. if (showsTable.update(show.id, show) == null) { res.errors = res.errors + 1 res.error = show.title + ': show not updated: ' + showsTable.lastError() }
  354. }
  355. return res
  356. }
  357. // the external ids (tracker.worldapi.org#12): missing ones from TMDB's external_ids, a missing TVmaze id from TVmaze
  358. ids = externalIdsFor(show, details.external_ids, true, tvmazeToo)
  359. res.ids = ids.filled
  360. res.tvmazeRequests = ids.tvmazeRequests
  361. if (ids.failed != null) { res.errors = res.errors + 1 res.error = show.title + ': ' + ids.failed }
  362. // tracker.worldapi.org#15: what TVmaze has and TMDB not yet (new episodes/seasons, empty titles and air dates)
  363. let tvmazeSummary = null
  364. let tvmazeStatus = null
  365. if (numIdOr(ids.tvmzId) != null && tvmazeToo) {
  366. tvm = mergeTvmaze(show.title, showKey, ids.tvmzId, seasonsByNumber, epByNumber, showSeasons)
  367. res.tvmazeRequests = res.tvmazeRequests + tvm.tvmazeRequests
  368. res.tvmazeEpisodes = tvm.newEpisodes
  369. res.tvmazeSeasons = tvm.newSeasons
  370. res.tvmazeFilled = tvm.filledEpisodes
  371. res.tvmazeSkipped = tvm.skipped
  372. if (tvm.errors > 0) { res.errors = res.errors + tvm.errors res.error = tvm.error }
  373. showSeasons = tvm.showSeasons
  374. tvmazeSummary = tvm.summary
  375. tvmazeStatus = tvm.status
  376. }
  377. // the show itself: status, counts, TMDB's overview, the poster, when it was synced
  378. poster = syncPoster(show, details.poster_path)
  379. res.requests = res.requests + poster.requests
  380. res.posters = poster.saved
  381. if (poster.failed != null) { res.errors = res.errors + 1 res.error = show.title + ': ' + poster.failed }
  382. show.seasons = showSeasons
  383. if (textOr(details.status) != null) { show.status = details.status }
  384. if (details.number_of_seasons != null) { show.seasonsCount = details.number_of_seasons }
  385. if (details.number_of_episodes != null) { show.episodesCount = details.number_of_episodes }
  386. // tracker#18: TMDB's text → tmdbSummary, TVmaze's → tvmazeSummary; `summary` is the creator's own text, never written here
  387. if (textOr(details.overview) != null) { show.tmdbSummary = details.overview }
  388. if (tvmazeSummary != null) { show.tvmazeSummary = tvmazeSummary }
  389. // tracker#35: TVmaze's status beside TMDB's (`status`) — the page's pill shows TVmaze's when there is one (fresher)
  390. if (tvmazeStatus != null) { show.tvmazeStatus = tvmazeStatus }
  391. show.image = poster.image
  392. show.tmdbPoster = poster.tmdbPoster
  393. show.imdbId = ids.imdbId
  394. show.tvdbId = ids.tvdbId
  395. show.tvmzId = ids.tvmzId
  396. if (adultOf(details) != null) { show.adult = adultOf(details) }
  397. if (textOr(details.type) != null) { show.tmdbType = details.type show.kind = kindOfTitle(details.type, show.genres) }
  398. show.tmdbSync = now()
  399. if (showsTable.update(show.id, show) == null) { res.errors = res.errors + 1 res.error = show.title + ': show not updated: ' + showsTable.lastError() }
  400. return res
  401. }
  402. // ---- the LIGHT step of the daily delta run (tracker#18) ---------------------------------------------------------------
  403. // A changed title NOBODY follows (dailysync.hl): its details (+ ids + credits: the record, applySynced) and only the seasons
  404. // that can have changed — the ones we don't have, the ones where TMDB counts MORE episodes than we have (fewer: TVmaze's
  405. // 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
  406. // it). Real data (2026-10-01): Today (67 seasons / 15,716 episodes) and EastEnders (43 / 7,349) change every day; their full
  407. // sync blocked the server 20.8 s each and grew the process by ~5 GB each.
  408. static changedSeasonNumbers = (&details, &show) => {
  409. ours = {}
  410. for (sid of show.seasons) {
  411. let s = seasonsTable.fetch(sid)
  412. if (s != null) { ours['' + s.seasonNumber] = s.episodes.length }
  413. }
  414. let latest = -1
  415. for (x of (details.seasons != null ? details.seasons : [])) {
  416. if (x != null && x.season_number != null && x.season_number > latest && (x.episode_count == null || x.episode_count > 0)) { latest = x.season_number }
  417. }
  418. out = []
  419. for (x of (details.seasons != null ? details.seasons : [])) {
  420. if (x != null && x.season_number != null) {
  421. let n = x.season_number
  422. if (n == latest || ours['' + n] == null || (x.episode_count != null && x.episode_count > ours['' + n])) { out.push(n) }
  423. }
  424. }
  425. return out
  426. }
  427. // tracker#35: `withStatus` — TVmaze's change list named this series too: its TVmaze status as well (one more TVmaze request)
  428. static syncShowDelta = (&showId, &withStatus) => {
  429. show = showsTable.fetch(showId)
  430. if (show == null || show.tmdbId == null || show.type == 'movie') { return syncShow(showId) }
  431. details = tmdbGet('/tv/' + show.tmdbId + '?append_to_response=external_ids,aggregate_credits')
  432. if (details.failed != null) {
  433. let res = emptyResult()
  434. res.requests = 1
  435. res.errors = 1
  436. res.error = show.title + ': ' + details.failed
  437. return res
  438. }
  439. r = syncShowPart(showId, details, { seasons = changedSeasonNumbers(details, show) finish = true tvmaze = false })
  440. r.requests = r.requests + 1
  441. r.details = details
  442. if (withStatus) {
  443. st = syncTvmazeStatus(showId)
  444. r.tvmazeRequests = r.tvmazeRequests + st.tvmazeRequests
  445. if (st.error != '') { r.errors = r.errors + 1 r.error = st.error }
  446. }
  447. return r
  448. }
  449. // tracker#35: TVmaze's status of ONE series (`GET /shows/<tvmzId>`, one TVmaze request) → `tvmazeStatus` when it changed — the
  450. // daily delta run for a series nobody follows that TVmaze's change list names (a followed one gets TVmaze's merge, which
  451. // brings it). Answers the sync's counts (emptyResult: tvmazeRequests, errors, error; no `details`).
  452. static syncTvmazeStatus = (&showId) => {
  453. let res = emptyResult()
  454. show = showsTable.fetch(showId)
  455. if (show == null || show.tvmzId == null || ('' + show.tvmzId).trim() == '') { return res }
  456. res.tvmazeRequests = 1
  457. got = tvmazeShow(show.tvmzId)
  458. if (got.failed != null) { res.errors = 1 res.error = show.title + ': ' + got.failed return res }
  459. if (got.status != null && got.status != show.tvmazeStatus) {
  460. show.tvmazeStatus = got.status
  461. if (showsTable.update(show.id, show) == null) { res.errors = 1 res.error = show.title + ': show not updated: ' + showsTable.lastError() }
  462. }
  463. return res
  464. }
  465. // flush the three tables' index/meta state to disk (after a run, and every few shows)
  466. static persistSync = () => {
  467. showsTable.persist()
  468. seasonsTable.persist()
  469. episodesTable.persist()
  470. return true
  471. }

Branches

Latest commits

  • 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
  • 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
  • 96ba683adeploy.sh: a gate without a 'passed,' line (check-theme) no longer ends the scriptmre
  • eb3b9205tracker: report 031mre
  • 9b5d2e89tracker mission 031: README (What it does, Test: four gates + the #32 checks, Files: theme/, new pages), STATUS (real copy, A/B load, how to repeat, open points), LOGmre
  • 39950e4ctracker#32 (mission 031): the WorldAPI theme (theme/ vendored verbatim from layouts.worldapi.org 85b5654; styles.hl inherits it: accent green-dark, type colours 1-6; own base/header rules, row lines, genre-pill and inverted-button frames removed, the season foldable keeps its line; check-theme 21 -> 0, 4th deploy gate; main actions class primary) and the #32 header (theme AppHeader/MainMenu/UserMenu/Sidebar/ContentFirst: desktop brand, search, Series|Shows|Movies|Genres|People, user icon with Unwatched..Settings, Logout; signed out the ident selector, phone the iD icon dropdown; phone menu in the sidebar overlay; marked entry by :has); /find -> /search/<q>, /genres, /people(/<letter>), /settings; main { ContentFirst { slot } } works around the hl:web one-line slot bug; gates 365/0, 32/0, 52/0, check-theme 0mre
  • a386dc92tracker: reports 029 + 030mre
  • 71e0fd7dtracker missions 029 + 030: README (What it does, Files, gate count), STATUS (real-copy numbers, how to repeat, open points), LOGmre
  • d36ea6eatracker#34 + #35 (mission 030): Follow directly under the poster, as wide as the poster (show.hl, styles.hl); the status pill next to a series' title — TVmaze's status (new tvmazeStatus, stored by the sync's TVmaze merge) else TMDB's, TVmaze Ended + TMDB Canceled = Canceled, inverted (filled, dark text, no border), green running / yellow pending / red canceled / muted ended (shows.hl statusOf); the daily delta asks TVmaze's status of an unfollowed series TVmaze's change list names (dailysync.hl syncRunStep, sync.hl syncTvmazeStatus); the status backfill after the details repair (backfill.hl, jobs.hl statusTick; resumable, 550 ms per TVmaze request); gates 354/0, 32/0, 52/0mre
  • 7d7d4487tracker#33 (mission 029): reduced titles — every title TMDB's details never went through this app (no detailsAt, no tmdbSync) is incomplete (shows.hl isIncomplete; the old tracker's migrated rows passed #26's test: 5,697 non-adult on the live copy, 691 series without seasons); the repair job does the visibly reduced first (shows.hl missingParts), the page completes one on open; a title TMDB has no poster for (The Remaining) shows the placeholder; tools/count-incomplete.hl; gate fixtures stand for synced titles (tmdbSync), tests/seed-reduced.hl + #33 checks; gates 347/0, 32/0, 52/0mre
  • 661c2592tracker: report 028mre
  • 27c916fatracker mission 028: README ("Code order", the new file map), STATUS (counts before/after, tests, how to repeat, open), LOGmre
  • d924f398tracker mission 028: comments name the new files (sync.hl, dailysync.hl, backfill.hl, credits.hl, jobs.hl, images.hl …); tools/ref-params.py + tools/lambda-audit.py also scan lib/ (they globbed the root only), lambda-audit counts a plain `x = p` alias like `let x = p`mre
  • 2e89b968tracker mission 028 (code order) 5/5 let: `let` only where a variable is reassigned — 667 never-reassigned lets became plain declarations (project.hl, lib/, components/, tools/, tests/); kept: 264 in loop bodies (a plain declaration there is 'Cannot reassign' on the 2nd pass), 234 reassigned, 27 whose name is also a member/outer/free name (a plain write would rebind it); tools/let-audit.py decides and fixes (README 'Code order'); tests/realdata-m028.{sh,mjs} = the page-output diff on a real copy; gates 342/0, 32/0, 52/0, real-copy pages identicalmre
  • 54796ff2tracker mission 028 (code order) 4/5 thin faces + last copies: the show page's check/follow faces call lib/watches.hl toggleWatched / toggleSeasonWatched (seasonAllWatched moved there) and lib/follows.hl toggleFollowed; both logins (header selector face, /login/callback) share lib/users.hl userOfCode; todayStr/listOf copies in components and the export readers copied into tools/migrate.hl + tools/old-short-ids.hl now once (lib/util.hl, lib/export.hl); gates 342/0, 32/0, 52/0; old-short-ids output byte-identical, migrate output identicalmre
  • 06b078e3tracker mission 028 (code order) 3/5 project.hl is the map: config, routes, wiring and a feature → file index (914 → 258 lines); the background jobs (daily sync run, backfills, details repair, credits job, merge, short ids, collection seed) moved unchanged into lib/jobs.hl (a class: their state is reassigned every step, a static cannot be; one instance made after the server), the login callback into lib/users.hl, poster/photo serving into lib/images.hl, the /shows/<slug> rule into lib/shows.hl showsMovedPath; route handlers are thin wrappers; gates 342/0, 32/0, 52/0, real-copy pages identicalmre
  • 94716fd2tracker mission 028 (code order) 2/5 util + topics: lib/util.hl holds envOr, storageDir, postersDir, profilesDir, newId, hexDigits, todayStr, dateOr, textOr, hasId, listOr, firstOf, sortDesc once (were copied into up to 5 files); tmdbsync.hl split into tmdb.hl (TMDB/TVmaze requests), sync.hl (one title's sync), sync-helpers.hl, backfill.hl; details.hl split into details.hl, credits.hl, credits-helpers.hl (isIncomplete to shows.hl); search-helpers.hl (words, query, ranking, slugs); collections.hl (the TMDB collection seed, out of franchises.hl); deltasync.hl renamed dailysync.hl; no behaviour change: gates 342/0, 32/0, 52/0, real-copy pages identicalmre
  • 186079b0tracker mission 028 (code order) 1/5 move: every root .hl except project.hl into lib/ (styles.hl into components/), import paths only; gates 342/0, 32/0, 52/0; real-copy pages identicalmre