gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit421a4809421a4809|||PR records: his-design rule (Apollo 2026-11-13)Tracker421a4809/lib/collections.hl

7.8 KB

  1. // lib/collections.hl — TIMELINES FROM TMDB'S MOVIE COLLECTIONS (tracker.worldapi.org#19, mission 066): the seed of franchises.hl's
  2. // timelines. Mission 028: out of franchises.hl.
  3. //
  4. // collectionChecksTable pk = the movie's show @id { collection (TMDB id | null), why, at } — the seed's progress
  5. // storage/mpackdb/collectionchecks.db
  6. //
  7. // THE SEED (TMDB movie collections): every movie with a tmdbId (adult ones skipped) is asked once
  8. // GET <TMDB>/movie/<tmdbId> → belongs_to_collection { id, name } (or null)
  9. // GET <TMDB>/collection/<id> → parts [{ id, title, release_date, adult }] — once per collection
  10. // A collection with at least TWO of our (non-adult) movies becomes one timeline (no franchise), its parts in release order;
  11. // every part we have is marked checked at once (no own details request). A collection with fewer is remembered (checks) and
  12. // gives no timeline (a widget with both ends disabled says nothing). Run in the app like the adult backfill (jobs.hl
  13. // `collectionTick`, one movie per step, ≤ 40 TMDB requests / 10 s, waits while the daily sync or the adult backfill runs),
  14. // RESUMES after a restart (the queue = the movies without a check row). A hand-edited timeline is never touched by it.
  15. import { MPackDB } from 'hl:mpackdb'
  16. import { now } from 'hl:time'
  17. import { storageDir, dateOr, textOr } from './util.hl'
  18. import { showsTable } from './shows.hl'
  19. import { sliceScan, sliceDone, sliceNext, sliceBudgetMs } from './slices.hl'
  20. import { tmdbGet } from './tmdb.hl'
  21. import { timelinesTable, ensureFranchises, putTimeline, seededCoverOf, setSuperseded } from './franchises.hl'
  22. static collectionChecksTable = new MPackDB(file = storageDir + '/collectionchecks.db', primaryKey = '@id', indexes = [])
  23. // the queue is a plain static list filled in place (a static cannot be reassigned; read through a field it was copied on every
  24. // access — shortids.hl), worked off from `seed.pos`
  25. static seedQueue = []
  26. static checkedShows = {}
  27. static collectionsDone = {}
  28. static movieByTmdb = {}
  29. static seedTries = {}
  30. static seed = { built = false pos = 0 movies = 0 checked = 0 requests = 0 timelines = 0 titles = 0 superseded = 0 small = 0 noCollection = 0 notFound = 0 errors = 0 retries = 0 }
  31. static markChecked = (&showId, collection, why) => {
  32. if (checkedShows[showId] == true) { return true }
  33. checkedShows[showId] = true
  34. seed.checked = seed.checked + 1
  35. if (collectionChecksTable.put({ id = showId collection = collection why = why at = now() }) == null) {
  36. console.log('collections: check of ' + showId + ' not stored: ' + collectionChecksTable.lastError())
  37. }
  38. return true
  39. }
  40. // mission 044: SLICED (slices.hl) — one call takes slices of the checks table, then the timelines, then the shows until the time
  41. // budget is spent; true when the queue is built. The jobs.hl tick calls it until it answers true.
  42. static seedScan = { phase = 0 checks = null shows = null ms = 0 }
  43. static seedPrepare = () => {
  44. if (seed.built) { return true }
  45. t0 = now()
  46. if (seedScan.phase == 0) {
  47. ensureFranchises()
  48. seedScan.checks = sliceScan(collectionChecksTable)
  49. seedScan.shows = sliceScan(showsTable)
  50. seedScan.phase = 1
  51. }
  52. if (seedScan.phase == 1) {
  53. while (!sliceDone(&seedScan.checks) && now() - t0 < sliceBudgetMs) {
  54. for (c of sliceNext(&seedScan.checks)) {
  55. checkedShows[c.id] = true
  56. if (c.collection != null) { collectionsDone['' + c.collection] = true }
  57. }
  58. }
  59. if (!sliceDone(&seedScan.checks)) { seedScan.ms = seedScan.ms + (now() - t0) return false }
  60. // the timelines (a few hundred rows)
  61. for (t of timelinesTable.find(null, null)) { if (t.tmdbCollection != null) { collectionsDone['' + t.tmdbCollection] = true } }
  62. seedScan.phase = 2
  63. }
  64. while (!sliceDone(&seedScan.shows) && now() - t0 < sliceBudgetMs) {
  65. for (s of sliceNext(&seedScan.shows)) {
  66. if (s.type == 'movie' && s.tmdbId != null && s.adult != true) {
  67. movieByTmdb['' + s.tmdbId] = s.id
  68. if (checkedShows[s.id] != true) { seedQueue.push(s.id) }
  69. }
  70. }
  71. }
  72. seedScan.ms = seedScan.ms + (now() - t0)
  73. if (!sliceDone(&seedScan.shows)) { return false }
  74. seed.built = true
  75. seed.movies = seedQueue.length
  76. return true
  77. }
  78. static seedLeft = () => { return seedQueue.length - seed.pos }
  79. static seedStats = () => { return seed }
  80. static seedPersist = () => {
  81. collectionChecksTable.persist()
  82. timelinesTable.persist()
  83. return true
  84. }
  85. // a passing failure goes to the back of the queue, at most 2 more tries per start
  86. static seedRetry = (&showId) => {
  87. n = seedTries[showId] != null ? seedTries[showId] : 0
  88. if (n >= 2) { return false }
  89. seedTries[showId] = n + 1
  90. seedQueue.push(showId)
  91. seed.retries = seed.retries + 1
  92. return true
  93. }
  94. // ONE movie: → { requests (TMDB API requests made: what the pause counts), done, error }
  95. static seedStep = () => {
  96. out = { requests = 0 done = false error = '' }
  97. if (!seed.built) { return out }
  98. if (seed.pos >= seedQueue.length) { out.done = true return out }
  99. id = seedQueue[seed.pos]
  100. seed.pos = seed.pos + 1
  101. if (checkedShows[id] == true) { return out }
  102. show = showsTable.fetch(id)
  103. if (show == null || show.adult == true || show.tmdbId == null) { return out }
  104. out.requests = 1
  105. seed.requests = seed.requests + 1
  106. d = tmdbGet('/movie/' + show.tmdbId)
  107. if (d.failed != null) {
  108. seed.errors = seed.errors + 1
  109. out.error = show.title + ': ' + d.failed
  110. if (d.status == 404) { seed.notFound = seed.notFound + 1 markChecked(&id, null, 'TMDB 404') } else { seedRetry(&id) }
  111. return out
  112. }
  113. c = d.belongs_to_collection
  114. if (c == null || c.id == null) {
  115. seed.noCollection = seed.noCollection + 1
  116. markChecked(&id, null, 'no collection')
  117. return out
  118. }
  119. cid = '' + c.id
  120. if (collectionsDone[cid] == true) { markChecked(&id, c.id, 'collection known') return out }
  121. out.requests = 2
  122. seed.requests = seed.requests + 1
  123. coll = tmdbGet('/collection/' + cid)
  124. if (coll.failed != null || coll.parts == null) {
  125. seed.errors = seed.errors + 1
  126. out.error = show.title + ': collection ' + cid + ': ' + (coll.failed != null ? coll.failed : 'no parts')
  127. if (coll.status == 404) { markChecked(&id, c.id, 'collection 404') collectionsDone[cid] = true } else { seedRetry(&id) }
  128. return out
  129. }
  130. // the parts in release order (no date last), only the non-adult ones we have
  131. let parts = []
  132. for (p of coll.parts) {
  133. next = []
  134. let done = false
  135. pd = dateOr(&p.release_date)
  136. for (x of parts) {
  137. if (!done && pd != '' && (x.d == '' || pd < x.d)) { next.push({ p = p d = pd }) done = true }
  138. next.push(x)
  139. }
  140. if (!done) { next.push({ p = p d = pd }) }
  141. parts = next
  142. }
  143. order = []
  144. for (x of parts) {
  145. let sid = x.p.adult == true ? null : movieByTmdb['' + x.p.id]
  146. if (sid != null && !order.includes(sid)) { order.push(sid) }
  147. }
  148. collectionsDone[cid] = true
  149. if (order.length >= 2) {
  150. name = textOr(&coll.name) != null ? coll.name.trim() : (textOr(&c.name) != null ? c.name.trim() : 'Collection ' + cid)
  151. // tracker#40: all of it in one curated timeline → made hidden (superseded, no second widget); partly → joins that franchise
  152. cov = seededCoverOf(&order)
  153. rec = putTimeline(name, &cov.franchise, order, 'tmdb', &c.id, 'release')
  154. if (rec == null) {
  155. seed.errors = seed.errors + 1
  156. out.error = 'timeline "' + name + '" not stored: ' + timelinesTable.lastError()
  157. } else {
  158. seed.timelines = seed.timelines + 1
  159. seed.titles = seed.titles + order.length
  160. if (cov.cover != null) {
  161. setSuperseded(rec, &cov.cover)
  162. seed.superseded = seed.superseded + 1
  163. }
  164. }
  165. } else { seed.small = seed.small + 1 }
  166. for (sid of order) { markChecked(&sid, c.id, 'collection') }
  167. markChecked(&id, c.id, 'collection')
  168. return out
  169. }
  170. static seedLine = () => {
  171. return seed.pos + '/' + seedQueue.length + ' movies, checked=' + seed.checked + ' timelines=' + seed.timelines + ' titles=' + seed.titles + ' superseded=' + seed.superseded + ' small=' + seed.small
  172. + ' noCollection=' + seed.noCollection + ' notFound=' + seed.notFound + ' requests=' + seed.requests + ' errors=' + seed.errors + ' retries=' + seed.retries
  173. }

Branches

Latest commits

  • 421a4809|||PR records: his-design rule (Apollo 2026-11-13)Tracker
  • d1390692Merge branch 'records-dp75b' into mainTracker
  • a4d1a7d2|||PR records: later steps checked, branch auditTracker
  • e070e16fMerge branch 'records-dp75' into mainTracker
  • 642b4fa8|||PR records: DP75 day62 live, AP62 briefTracker
  • bfc4ea4dMerge branch 'ap62' into mainTracker
  • 2c95ac46|||PR AP62: re-vendor Hybriel set day62 (source 70341ad2, bin a54f5f08)Tracker
  • b9c7c3c8Merge branch 'records-dp74' into mainTracker
  • 85cfb4ae|||PR records: AP61 day61, DP74 liveTracker
  • b1496147Merge branch 'ap61' into mainTracker
  • 2047955bMerge branch 'u12-gateports' into mainTracker
  • c8da40f3|||PR AP61: re-vendor Hybriel set day61 (source a05af47c, bin a11042e5)Tracker
  • 4e018678|||PR deploy.sh: gates use Tracker's ports 8716-8719 and LC_ALL=C; README: 2 backups keptTracker
  • 0b86324aMerge branch 'records-dp73' into mainTracker
  • 9ce5f59b|||PR records: DP73 live 613ae0e; deploy.sh keeps 2 backupsTracker
  • 613ae0ecMerge branch 'bs3' into mainTracker
  • 1383264b|||PR BS3: keep shared synchronous sorting fastTracker
  • 63e5ba1a|||PR BS3: guard boot imports and share resumable sort buffersTracker
  • 310613f1Merge branch 'records-dp72' into mainTracker
  • 6f7b61ba|||PR records: deploy-swap fix live, Kunibert findingsTracker