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/franchiseseed.hl

13.5 KB

  1. // lib/franchiseseed.hl — THE CURATED FRANCHISES (tracker.worldapi.org#40, mission 034). Creator: "you never build timlines and
  2. // franchises". The big franchises (Star Trek, Star Wars, Stargate, MCU, DC …) with their timelines, MOVIES AND SERIES, come from
  3. // a hand-written DATA file in the repo — `data/franchises.json` (TRACKER_FRANCHISE_FILE): per franchise its name, summary,
  4. // the TMDB collections that join it, and its timelines, each with its order ("in-universe" | "release") and its titles as
  5. // [kind ('tv' | 'movie'), TMDB id, "title (year)" — for people only].
  6. //
  7. // franchiseSeedTable pk @id { kind, key, ref, order, franchise, created, why, at } — what the seed made, one row per thing:
  8. // kind 'franchise' key = its name ref = the franchise @id created (true: the seed made it; false: the
  9. // creator's own of that name, adopted)
  10. // kind 'timeline' key = franchise|timeline ref = the timeline @id order + franchise = what the seed LAST WROTE
  11. // (order null: the creator's own of that name)
  12. // kind 'collection' key = collection:<id> ref = the timeline @id franchise (null: it had one already, left alone)
  13. // kind 'missing' key = tv:<id> / movie:<id> why TMDB has no such title (404) — not asked again
  14. // storage/mpackdb/franchiseseed.db
  15. //
  16. // THE JOB (jobs.hl `franchiseSeedTick`, after every other start job — the collection seed last — one step per tick):
  17. // 1. the titles of the file we don't have (search.hl titleIdByTmdb) are IMPORTED, one per step, through the search's own
  18. // import (details.hl importWithCredits: details, seasons + episodes, poster, ids, TVmaze, cast + crew — complete), then
  19. // the pause the daily sync makes after the same requests (tmdb.hl pauseMsAfter). Without TMDB_READ_TOKEN: no imports.
  20. // 2. then ONE build step (no TMDB): each franchise found or made, its collections' timelines attached, each timeline made
  21. // or brought up to date with the titles we have, in the file's order; then every TMDB collection timeline whose titles
  22. // are ALL in one curated timeline is hidden (`supersededBy`, franchises.hl seededCoverOf), a partly covered one joins
  23. // that franchise.
  24. // EDITOR CHANGES WIN: a seeded timeline whose order or franchise is no longer what the seed last wrote was edited by the
  25. // creator — it is never written again (`kept`); a franchise or timeline of the same name the creator made is used / left
  26. // alone, never doubled; a collection timeline that has a franchise keeps it. RESUMES after a restart (imports are stored at
  27. // once; the queue = what is still missing); IDEMPOTENT (a second start imports nothing and writes nothing).
  28. import { MPackDB } from 'hl:mpackdb'
  29. import { readFile, exists } from 'hl:fs'
  30. import { now } from 'hl:time'
  31. import { storageDir, envOr, newId } from './util.hl'
  32. import { showById, liveShowOf } from './shows.hl'
  33. import { titleIdByTmdb } from './search.hl'
  34. import { importWithCredits } from './details.hl'
  35. import { ensureFranchises, franchiseById, timelineById, timelinesTable, createFranchise, putTimeline, setTimelineFranchise, franchiseByName, timelineByName, timelineOfCollection, storeSeeded, persistFranchises, seededCoverOf, setSuperseded } from './franchises.hl'
  36. static franchiseSeedTable = new MPackDB(file = storageDir + '/franchiseseed.db', primaryKey = '@id', indexes = [])
  37. static franchiseFile = envOr('TRACKER_FRANCHISE_FILE', './data/franchises.json')
  38. // the queue is a plain static list filled in place (a static cannot be reassigned — collections.hl)
  39. static importQueue = []
  40. static seedRows = {}
  41. static fseed = { built = false pos = 0 file = false franchises = 0 timelines = 0 titles = 0 have = 0 imported = 0 failed = 0 missing = 0
  42. requests = 0 tvmazeRequests = 0 franchisesCreated = 0 franchisesAdopted = 0 timelinesCreated = 0 updated = 0 unchanged = 0 kept = 0 empty = 0
  43. placed = 0 collections = 0 superseded = 0 unsuperseded = 0 joined = 0 writes = 0 error = '' }
  44. static typeOfKind = (&kind) => { return kind == 'tv' ? 'series' : 'movie' }
  45. static titleKeyOf = (&kind, &tmdbId) => { return kind + ':' + tmdbId }
  46. // our show @id for an entry of the file (a merged tombstone answers its keeper), or null
  47. static ourIdOf = (&entry) => {
  48. id = titleIdByTmdb(typeOfKind(entry[0]), entry[1])
  49. s = id != null ? liveShowOf(showById(id)) : null
  50. return s != null ? s.id : null
  51. }
  52. static sameList = (&a, &b) => {
  53. if (a == null || b == null || a.length != b.length) { return false }
  54. let i = 0
  55. while (i < a.length) {
  56. if (a[i] != b[i]) { return false }
  57. i = i + 1
  58. }
  59. return true
  60. }
  61. static noteRow = (kind, key, &ref, &order, &franchise, created, why) => {
  62. old = seedRows[kind + '|' + key]
  63. rec = { id = old != null ? old.id : newId() kind = kind key = key ref = ref order = order franchise = franchise created = created why = why at = now() }
  64. if (old != null) { franchiseSeedTable.update(rec.id, rec) } else { franchiseSeedTable.put(rec) }
  65. seedRows[kind + '|' + key] = rec
  66. return rec
  67. }
  68. static rowOf = (&kind, &key) => { return seedRows[kind + '|' + key] }
  69. // the file → { franchises: [...] }, or null (logged) when it is not there
  70. static readFranchiseFile = () => {
  71. if (!exists(franchiseFile)) {
  72. fseed.error = franchiseFile + ' not found'
  73. return null
  74. }
  75. d = JSON.parse(readFile(franchiseFile))
  76. if (d == null || d.franchises == null) {
  77. fseed.error = franchiseFile + ': no "franchises" list'
  78. return null
  79. }
  80. return d
  81. }
  82. static fileData = { franchises = [] }
  83. // at the job's start: the file read, the seed's rows loaded, the queue = the file's titles we don't have (`imports` false: none)
  84. static franchiseSeedPrepare = (&imports) => {
  85. if (fseed.built) { return importQueue.length }
  86. fseed.built = true
  87. ensureFranchises()
  88. for (r of franchiseSeedTable.find(null, null)) { seedRows[r.kind + '|' + r.key] = r }
  89. d = readFranchiseFile()
  90. if (d == null) { return 0 }
  91. fseed.file = true
  92. fileData.franchises = d.franchises
  93. queued = {}
  94. for (f of d.franchises) {
  95. fseed.franchises = fseed.franchises + 1
  96. for (t of f.timelines) {
  97. fseed.timelines = fseed.timelines + 1
  98. for (e of t.titles) {
  99. let key = titleKeyOf(e[0], e[1])
  100. if (queued[key] != true) {
  101. queued[key] = true
  102. fseed.titles = fseed.titles + 1
  103. if (ourIdOf(e) != null) { fseed.have = fseed.have + 1 }
  104. else if (rowOf('missing', key) != null) { fseed.missing = fseed.missing + 1 }
  105. else if (imports) { importQueue.push(e) }
  106. }
  107. }
  108. }
  109. }
  110. return importQueue.length
  111. }
  112. static franchiseSeedStats = () => { return fseed }
  113. // ---- step 1: ONE import → { requests, tvmazeRequests (what the pause counts), error } --------------------------------------
  114. static importOne = (&entry) => {
  115. out = { requests = 1 tvmazeRequests = 0 error = '' done = false }
  116. key = titleKeyOf(entry[0], entry[1])
  117. fseed.requests = fseed.requests + 1
  118. r = importWithCredits(entry[0], entry[1])
  119. if (r.error != null) {
  120. fseed.failed = fseed.failed + 1
  121. out.error = key + ' (' + entry[2] + '): ' + r.error
  122. // TMDB has no such title: remembered, never asked again (a passing failure is tried again on the next start)
  123. if (r.error.includes('HTTP 404')) { noteRow('missing', key, null, null, null, false, r.error) franchiseSeedTable.persist() }
  124. return out
  125. }
  126. if (r.sync != null) {
  127. out.requests = 1 + r.sync.requests
  128. out.tvmazeRequests = r.sync.tvmazeRequests
  129. fseed.requests = fseed.requests + r.sync.requests
  130. fseed.tvmazeRequests = fseed.tvmazeRequests + r.sync.tvmazeRequests
  131. }
  132. if (r.created == true) { fseed.imported = fseed.imported + 1 } else { fseed.have = fseed.have + 1 }
  133. return out
  134. }
  135. // ---- step 2: the build (no TMDB) ---------------------------------------------------------------------------------------------
  136. // the franchise of the file's entry `f`: the one the seed made / adopted before, else the creator's of that name, else a new one
  137. static franchiseOf = (&f) => {
  138. row = rowOf('franchise', f.name)
  139. if (row != null) { return franchiseById(row.ref) }
  140. mine = franchiseByName(f.name)
  141. if (mine != null) {
  142. fseed.franchisesAdopted = fseed.franchisesAdopted + 1
  143. noteRow('franchise', f.name, mine.id, null, null, false, 'the creator\'s own')
  144. return mine
  145. }
  146. c = createFranchise(f.name, f.summary)
  147. if (c.error != null) {
  148. fseed.error = f.name + ': ' + c.error
  149. return null
  150. }
  151. fseed.franchisesCreated = fseed.franchisesCreated + 1
  152. noteRow('franchise', f.name, c.id, null, null, true, '')
  153. return franchiseById(c.id)
  154. }
  155. // a TMDB collection's timeline (collection seed) joins the franchise — once, and only when it has none yet
  156. static attachCollection = (&collection, &fid) => {
  157. key = 'collection:' + collection
  158. if (rowOf('collection', key) != null) { return false }
  159. t = timelineOfCollection(collection)
  160. if (t == null) { return false }
  161. if (t.franchise != null && t.franchise != '') {
  162. noteRow('collection', key, t.id, null, null, false, 'has a franchise already')
  163. return false
  164. }
  165. r = setTimelineFranchise(t.id, fid)
  166. if (r.error != null) {
  167. fseed.error = key + ': ' + r.error
  168. return false
  169. }
  170. fseed.collections = fseed.collections + 1
  171. noteRow('collection', key, t.id, null, fid, true, '')
  172. return true
  173. }
  174. // one timeline of the file: made, brought up to date, or left alone (the creator's)
  175. static buildTimeline = (&f, &fid, &tl) => {
  176. key = f.name + '|' + tl.name
  177. order = []
  178. for (e of tl.titles) {
  179. let sid = ourIdOf(e)
  180. if (sid != null && !order.includes(sid)) { order.push(sid) }
  181. }
  182. fseed.placed = fseed.placed + order.length
  183. row = rowOf('timeline', key)
  184. if (row == null) {
  185. mine = timelineByName(tl.name)
  186. if (mine != null) {
  187. // the creator made one of that name: theirs, never written by the seed
  188. fseed.kept = fseed.kept + 1
  189. noteRow('timeline', key, mine.id, null, null, false, 'the creator\'s own')
  190. return true
  191. }
  192. // none of its titles here yet (no TMDB token, or every import failed): made on a later start
  193. if (order.length == 0) {
  194. fseed.empty = fseed.empty + 1
  195. return true
  196. }
  197. rec = putTimeline(tl.name, fid, order, 'seed', null, tl.order == 'release' ? 'release' : 'in-universe')
  198. if (rec == null) {
  199. fseed.error = key + ': not stored'
  200. return false
  201. }
  202. fseed.timelinesCreated = fseed.timelinesCreated + 1
  203. fseed.writes = fseed.writes + 1
  204. noteRow('timeline', key, rec.id, order, fid, true, '')
  205. return true
  206. }
  207. t = timelineById(row.ref)
  208. // gone, or the creator's own, or edited since the seed wrote it (its order or franchise changed): editor changes win
  209. if (t == null || row.order == null || !sameList(t.order, row.order) || t.franchise != row.franchise) {
  210. fseed.kept = fseed.kept + 1
  211. return true
  212. }
  213. if (sameList(t.order, order) && t.franchise == fid) {
  214. fseed.unchanged = fseed.unchanged + 1
  215. return true
  216. }
  217. r = storeSeeded(t, order, fid)
  218. if (r.error != null) {
  219. fseed.error = key + ': ' + r.error
  220. return false
  221. }
  222. fseed.updated = fseed.updated + 1
  223. fseed.writes = fseed.writes + 1
  224. noteRow('timeline', key, t.id, order, fid, true, '')
  225. return true
  226. }
  227. // tracker#40: every TMDB collection timeline against the curated ones — all its titles in one → hidden (superseded: no second
  228. // widget on First Contact); the cover gone → shown again; partly covered and without a franchise → joins that franchise
  229. static supersedeCollections = () => {
  230. for (t of timelinesTable.find(null, null)) {
  231. if (t.tmdbCollection != null && t.order != null) {
  232. let cov = seededCoverOf(t.order)
  233. if (cov.cover != null && t.supersededBy != cov.cover) {
  234. if (setSuperseded(t, cov.cover).error == null) { fseed.superseded = fseed.superseded + 1 }
  235. } else if (cov.cover == null && t.supersededBy != null) {
  236. if (setSuperseded(t, null).error == null) { fseed.unsuperseded = fseed.unsuperseded + 1 }
  237. } else if (cov.cover == null && cov.franchise != null && (t.franchise == null || t.franchise == '')) {
  238. if (setTimelineFranchise(t.id, cov.franchise).error == null) { fseed.joined = fseed.joined + 1 }
  239. }
  240. }
  241. }
  242. return true
  243. }
  244. static buildAll = () => {
  245. for (f of fileData.franchises) {
  246. let fr = franchiseOf(f)
  247. if (fr != null) {
  248. if (f.collections != null) { for (c of f.collections) { attachCollection(c, fr.id) } }
  249. for (tl of f.timelines) { buildTimeline(f, fr.id, tl) }
  250. }
  251. }
  252. supersedeCollections()
  253. persistFranchises()
  254. franchiseSeedTable.persist()
  255. return true
  256. }
  257. // ONE step: the next import, or — the queue done — the build → { requests, tvmazeRequests, error, done }
  258. static franchiseSeedStep = () => {
  259. if (!fseed.file) { return { requests = 0 tvmazeRequests = 0 error = fseed.error done = true } }
  260. if (fseed.pos < importQueue.length) {
  261. e = importQueue[fseed.pos]
  262. fseed.pos = fseed.pos + 1
  263. // imported meanwhile (the search, a person page): nothing to ask
  264. if (ourIdOf(e) != null) {
  265. fseed.have = fseed.have + 1
  266. return { requests = 0 tvmazeRequests = 0 error = '' done = false }
  267. }
  268. return importOne(e)
  269. }
  270. buildAll()
  271. return { requests = 0 tvmazeRequests = 0 error = fseed.error done = true }
  272. }
  273. static franchiseSeedLine = () => {
  274. s = fseed
  275. return 'franchises=' + s.franchises + ' (created=' + s.franchisesCreated + ' adopted=' + s.franchisesAdopted + ') timelines=' + s.timelines + ' (created=' + s.timelinesCreated
  276. + ' updated=' + s.updated + ' unchanged=' + s.unchanged + ' kept=' + s.kept + ' empty=' + s.empty + ') collections=' + s.collections + ' superseded=' + s.superseded + ' unsuperseded=' + s.unsuperseded + ' joined=' + s.joined + ' titles=' + s.titles + ' had=' + s.have
  277. + ' imported=' + s.imported + ' failed=' + s.failed + ' missing=' + s.missing + ' placed=' + s.placed + ' requests=' + s.requests + ' tvmazeRequests=' + s.tvmazeRequests
  278. }

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