gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit7f541d117f541d11Tracker conductor: CONDUCTOR.md, short STATUS.md, old status + decisions archive to archive/2026-10-31mre7f541d11/lib/sync.hl

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

Branches

Latest commits

  • 7f541d11Tracker conductor: CONDUCTOR.md, short STATUS.md, old status + decisions archive to archive/2026-10-31mre
  • 8dd423f7README: login.js stays by caramboleyo's explicit rulemre
  • d8df1493tracker: standby service on port 45018 and a gap-free hand-over in deploy.sh step 4mre
  • 36dd1686ident#31: the selector's sign-in returns to the page the user started on (return attribute = the login callback with next), gate checkmre
  • ff9b838etracker: the Ended status pill uses the same red as layouts (pill danger); gate updatedmre
  • 40d9719eTP2: LOG line and report for the tracker pillsmre
  • ab3f2ba5tracker: status, genres, TMDB/IMDb/TVDB/TVmaze links and list kinds use the theme's pill; dense season list with the season check centred on its title line; gates for bothmre
  • 521061d6tracker header on phones: signed in, the brand fits whole at 360 and 390 px (tighter gaps and side paddings, slightly smaller brand, narrow screens and signed in only); mobile gate checks itmre
  • 18114a53tracker: franchises gate waits for the signed-out selector instead of the removed Log in buttonmre
  • 08561d73ident selector back in the header (creator 2026-10-24): selector top right signed in and out on phone and desktop, no second Log in button, new gate checks (WIP: gates not re-run after the last edits)mre
  • 59ef1e4atests/browser: repair 'done' check counts the 404 titles the first run already finished (race: failed=1 when Bulk Film 79 ran before the restart)mre
  • 5925fc4dreports/MK-mobile.md: rebase update, gate countsmre
  • 6adbc7f0extlink badges: background-clip after the background shorthand (it reset the clip: the pills were 44 px tall); gates check the lookmre
  • 8d3b0f20widget: the franchise link's 44 px tap area no longer overlaps the Prequel/Sequel rowmre
  • 1d98dcfdbrowser gate: the badges' size check reads the padding box (the 44 px tap area is transparent border)mre
  • 2f9e29ffreports/MK-mobile.md, deploy.sh GATES: tests/mobile.mjs, theme check markermre
  • 7cfc8ab5a season of more than 150 episodes starts closed on its title page; the phone gate checks itmre
  • 59095c59tests/mobile.mjs: the phone gate (390 px touch, tap targets, /franchises paging)mre
  • 105309detap targets: every row link, the TMDB/IMDb badges, the widget links, the franchise names and the login-failed link are 44 px high on a phonemre
  • f0b86c8efranchises: 40 loose timelines per page (/franchises/page/N) and a merge sort for the names (1,546 timelines: 20 s and 54,000 px before)mre