gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitf57f64f0f57f64f0mission 046: report, LOGmref57f64f0/lib/people.hl

29.0 KB

  1. // lib/people.hl — THE PERSON PAGE's data (tracker.worldapi.org#16, components/person.hl `/person/<slug>`): a person by slug,
  2. // their facts and filmography, and the ONE-TIME fetch of their whole filmography from TMDB on the first visit. Creator:
  3. // "the movies come from artist scrolls. when one clicks on an artist that has no dataset yet the old version fetched that
  4. // artist and its entire bibilogrqphy so its profile was complete".
  5. //
  6. // personBySlug(slug) the person (slug = `urlSegment`) — a slug → id map built once (15k people, one scan)
  7. // personInfoOf(p) name, photo URL, born/died lines, bio
  8. // filmographyOf(p) their credits (`p.shows`): poster, title, year, role, kind — newest first, adult titles hidden
  9. // personFillStep(id) ONE step of the fetch, called again and again by the page until it answers `done`:
  10. // 1st call: GET <TMDB>/person/<tmdbId>?append_to_response=combined_credits (ONE request: details +
  11. // every cast/crew credit) + the photo (w185, like the posters); the person's facts are updated;
  12. // every next call stores the next `fillBatch` titles; the last one links the credits and marks
  13. // the person complete (`filmographyAt`, ms). hl:web serves one request at a time — between two
  14. // steps every other request is served, so a person with 300 credits never holds the server.
  15. // peoplePage(page) /people (tracker#37): everyone, the one updated last first, 24 per page — a view built once at boot
  16. // personPhotoOf(p) the photo of a /people tile and a search row: our file, else TMDB's (`tmdbProfile`), else the placeholder
  17. //
  18. // COMPLETE = `filmographyAt` set: never fetched again (the daily sync may refresh followed people later — not built).
  19. // Migrated people are NOT complete (153 of them carry the old tracker's credits — kept, the fetch adds the rest).
  20. // A title we don't have → a MINIMAL show record (title, year, type, tmdbId, `adult`, overview, genres by TMDB id, language;
  21. // no seasons, no poster file; `minimal = true` — details.hl completes it when it is opened or by the repair job, tracker#26), shaped like search.hl importTitle's
  22. // (`oldId tmdb-<tv|movie>-<id>`, a free slug), indexed for the search and put into the public lists (catalog.hl).
  23. // The person's credits (`shows`, the migrated field): [{ show, character, posterPath, adult }] — `character` is the role
  24. // text (the characters, then crew jobs like "Director", " / " between them); `posterPath` = TMDB's, so the page can show
  25. // TMDB's small poster (w92, like search's web rows) until we have the file. ADULT (TMDB's flag, mission 054): a row is
  26. // shown only for a PUBLIC title (shows.hl `isPublicTitle`: `adult == false`, unknown hidden like the public lists) whose
  27. // credit is not adult either (mission 056 merge).
  28. // ONE PROCESS PER STORAGE (hybriel#21); new rows get self-assigned 16-hex ids (hybriel#113), like sync.hl.
  29. import { now } from 'hl:time'
  30. import { fetch } from 'hl:fetch'
  31. import { writeFile, mkDir, exists } from 'hl:fs'
  32. import { profilesDir, newId, textOr, listOr, sortDesc } from './util.hl'
  33. import { personsTable, showsTable, genresTable, showById, showYear, posterUrlOf, posterUrlOfName, creditRowLive, isPublicTitle, titlePath, typeWordOf, typeClassOf, liveShowOf } from './shows.hl'
  34. import { tmdbGet, tmdbImageBase } from './tmdb.hl'
  35. import { titleIdByTmdb, indexShow, slugOf, indexPerson } from './search.hl'
  36. import { slugBaseOf } from './search-helpers.hl'
  37. import { refreshCatalogShow, pagesOf, pageNumberOf, pageSize } from './catalog.hl'
  38. import { claimShortId } from './shortids.hl'
  39. import { sliceBudgetMs } from './slices.hl'
  40. // titles stored per step (real data: see STATUS.md ticket #16 for the per-step time)
  41. static fillBatch = 40
  42. // ---- the person -------------------------------------------------------------------------------------------------
  43. static slugIndex = { built = false ids = {} }
  44. // mission 043: one person into the slug map IN PLACE (bootscan.hl's person pass, or the lazy scan below — streamed)
  45. static noteSlugOfPerson = (&p) => {
  46. if (p.urlSegment != null) { slugIndex.ids[p.urlSegment] = p.id }
  47. return null
  48. }
  49. static ensureSlugIndex = () => {
  50. if (!slugIndex.built) {
  51. slugIndex.built = true
  52. for (p of personsTable.iterate(null, null)) { noteSlugOfPerson(p) }
  53. }
  54. return true
  55. }
  56. static personBySlug = (&slug) => {
  57. if (slug == null || hlTypeName(slug) != 'String' || slug == '') { return null }
  58. ensureSlugIndex()
  59. id = slugIndex.ids[slug]
  60. return id == null ? null : personsTable.fetch(id)
  61. }
  62. static isComplete = (&p) => { return p.filmographyAt != null }
  63. // the page asks TMDB for this person: not complete yet, and TMDB knows them
  64. static needsFill = (&p) => { return !isComplete(p) && p.tmdbId != null }
  65. // the photo: /profiles/<oldId>.<ext> (images.hl) — only when the file is there (the migrated `image` = 'jpg'
  66. // of 129 people never came with a file)
  67. static profileName = (&p) => {
  68. if (p.oldId == null || p.image == null || p.image == '') { return null }
  69. return p.oldId + '.' + p.image
  70. }
  71. static photoUrlOf = (&p) => {
  72. name = profileName(p)
  73. return name != null && exists(profilesDir + '/' + name) ? '/profiles/' + name : ''
  74. }
  75. static monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
  76. // '1986-03-04' → '4 March 1986'; a bare year or anything else as it is
  77. static dateText = (&d) => {
  78. t = textOr(d)
  79. if (t == null) { return null }
  80. if (t.length < 10) { return t }
  81. m = toNumber(t.slice(5, 7))
  82. day = toNumber(t.slice(8, 10))
  83. if (m == null || day == null || m < 1 || m > 12) { return t }
  84. return day + ' ' + monthNames[m - 1] + ' ' + t.slice(0, 4)
  85. }
  86. // { name, photoUrl, born, died, bio } — every field a plain string ('' = none)
  87. static personInfoOf = (&p) => {
  88. born = dateText(p.birthDate)
  89. place = textOr(p.birthPlace)
  90. let bornText = ''
  91. if (born != null && place != null) { bornText = 'Born ' + born + ' in ' + place } else if (born != null) { bornText = 'Born ' + born } else if (place != null) { bornText = 'Born in ' + place }
  92. died = dateText(p.deathDate)
  93. return { name = p.name != null ? p.name : '' photoUrl = photoUrlOf(p) born = bornText died = died != null ? 'Died ' + died : ''
  94. bio = textOr(p.biography) != null ? p.biography : '' }
  95. }
  96. // ---- the filmography --------------------------------------------------------------------------------------------
  97. // the credits, one row per title (a title twice — the migrated credit and TMDB's — is ONE row, roles merged), newest
  98. // first by the title's release ('YYYY-MM-DD', else its year), undated last; only public titles (no adult/unknown)
  99. static filmographyOf = (&p) => {
  100. byShow = {}
  101. order = []
  102. for (c of listOr(p.shows)) {
  103. if (c != null && c.show != null) {
  104. let role = textOr(c.character)
  105. let prev = byShow[c.show]
  106. if (prev == null) {
  107. byShow[c.show] = { role = role != null ? role : '' posterPath = textOr(c.posterPath) adult = c.adult == true }
  108. order.push(c.show)
  109. } else {
  110. let x = prev
  111. if (role != null && !(' / ' + x.role + ' / ').includes(' / ' + role + ' / ')) { x.role = x.role == '' ? role : x.role + ' / ' + role }
  112. if (x.posterPath == null) { x.posterPath = textOr(c.posterPath) }
  113. if (c.adult == true) { x.adult = true }
  114. byShow[c.show] = x
  115. }
  116. }
  117. }
  118. list = []
  119. listed = {}
  120. for (sid of order) {
  121. let c = byShow[sid]
  122. // tracker#31: a credit on a merged tombstone is the keeper's (merge.hl repoints them; a keeper listed once)
  123. let s = creditRowLive(sid)
  124. if (s != null && s.public && !c.adult && listed[s.id] != true) {
  125. listed[s.id] = true
  126. let release = s.release
  127. let key = release != null && release.length >= 4 ? release : s.year
  128. let poster = posterUrlOfName(s.posterName)
  129. if (poster == '/posters/none' && c.posterPath != null) { poster = tmdbImageBase + '/w92' + c.posterPath }
  130. list.push({ key = key id = s.id title = s.title year = s.year href = s.href posterUrl = poster
  131. role = c.role typeLabel = s.typeLabel typeClass = s.typeClass })
  132. }
  133. }
  134. out = []
  135. undated = []
  136. for (r of sortDesc(list)) { if (r.key == '') { undated.push(r) } else { out.push(r) } }
  137. for (r of undated) { out.push(r) }
  138. return out
  139. }
  140. // ---- the fetch ------------------------------------------------------------------------------------------------
  141. // person id → { items, pos, credits, added, requests, started, steps } while a fetch is under way (in memory: a restart
  142. // loses it — the titles stored so far are found again by TMDB id, nothing is duplicated)
  143. static jobs = {}
  144. // TMDB genre id → ours (our genres carry TMDB's id)
  145. static genreMap = { built = false ids = {} }
  146. static genreIdsOfTmdb = (&tmdbIds) => {
  147. if (!genreMap.built) {
  148. m = {}
  149. for (g of genresTable.find(null, null)) { if (g.tmdbId != null) { m['' + g.tmdbId] = g.id } }
  150. genreMap.ids = m
  151. genreMap.built = true
  152. }
  153. out = []
  154. for (t of listOr(tmdbIds)) {
  155. let id = genreMap.ids['' + t]
  156. if (id != null && !out.includes(id)) { out.push(id) }
  157. }
  158. return out
  159. }
  160. // TMDB's combined_credits → one item per title (cast AND crew of the same title merged), TV + movies only
  161. static creditItems = (&cc) => {
  162. byKey = {}
  163. order = []
  164. all = []
  165. if (cc != null) {
  166. for (c of listOr(cc.cast)) { all.push({ credit = c role = textOr(c.character) }) }
  167. for (c of listOr(cc.crew)) { all.push({ credit = c role = textOr(c.job) }) }
  168. }
  169. for (a of all) {
  170. let c = a.credit
  171. let kind = c.media_type
  172. if ((kind == 'tv' || kind == 'movie') && c.id != null && hlTypeName(c.id) == 'Number') {
  173. let k = kind + ':' + c.id
  174. let known = byKey[k]
  175. if (known == null) {
  176. let name = kind == 'tv' ? textOr(c.name) : textOr(c.title)
  177. if (name != null) {
  178. byKey[k] = { kind = kind tmdbId = c.id title = name release = textOr(kind == 'tv' ? c.first_air_date : c.release_date)
  179. overview = textOr(c.overview) language = textOr(c.original_language) genreIds = listOr(c.genre_ids)
  180. posterPath = textOr(c.poster_path) adult = c.adult == true role = a.role != null ? a.role : '' }
  181. order.push(k)
  182. }
  183. } else if (a.role != null && !(' / ' + known.role + ' / ').includes(' / ' + a.role + ' / ')) {
  184. let x = known
  185. x.role = x.role == '' ? a.role : x.role + ' / ' + a.role
  186. byKey[k] = x
  187. }
  188. }
  189. }
  190. out = []
  191. for (k of order) { out.push(byKey[k]) }
  192. return out
  193. }
  194. // the photo: w185 → storage/mpackdb/profiles/<oldId>.<ext> — { requests, image, tmdbProfile, failed }
  195. static syncProfile = (&p, profilePath) => {
  196. out = { requests = 0 image = p.image tmdbProfile = p.tmdbProfile failed = null }
  197. if (textOr(profilePath) == null || p.oldId == null) { return out }
  198. parts = profilePath.split('.')
  199. ext = parts[parts.length - 1].toLowerCase()
  200. if (ext != 'jpg' && ext != 'jpeg' && ext != 'png' && ext != 'webp') { return out }
  201. file = profilesDir + '/' + p.oldId + '.' + ext
  202. if (exists(file) && p.tmdbProfile == profilePath && p.image == ext) { return out }
  203. out.requests = 1
  204. r = fetch(tmdbImageBase + '/w185' + profilePath, { headers = { 'user-agent' = 'tracker.worldapi.org (person page)' } timeoutMs = 20000 })
  205. if (r == null || r.status != 200 || r.body == null || r.body.length == 0) {
  206. out.failed = 'photo ' + profilePath + ': ' + (r == null ? 'no answer' : 'HTTP ' + r.status)
  207. return out
  208. }
  209. mkDir(profilesDir)
  210. writeFile(file, toBytes(r.body), 420)
  211. out.image = ext
  212. out.tmdbProfile = profilePath
  213. return out
  214. }
  215. // step 1: TMDB's person + credits; the facts (a TMDB value that is empty never overwrites ours) and the photo
  216. static startFill = (p) => {
  217. t0 = now()
  218. d = tmdbGet('/person/' + p.tmdbId + '?append_to_response=combined_credits')
  219. if (d.failed != null) { return { error = 'TMDB: ' + d.failed } }
  220. items = creditItems(d.combined_credits)
  221. photo = syncProfile(p, d.profile_path)
  222. x = p
  223. if (textOr(d.biography) != null) { x.biography = d.biography }
  224. if (textOr(d.birthday) != null) { x.birthDate = d.birthday }
  225. if (textOr(d.deathday) != null) { x.deathDate = d.deathday }
  226. if (textOr(d.place_of_birth) != null) { x.birthPlace = d.place_of_birth }
  227. if (textOr(x.imdbId) == null && textOr(d.imdb_id) != null) { x.imdbId = d.imdb_id }
  228. if (d.adult != null) { x.adult = d.adult == true }
  229. x.image = photo.image
  230. x.tmdbProfile = photo.tmdbProfile
  231. x.updatedAt = now()
  232. if (personsTable.update(x.id, x) == null) { return { error = 'could not store the person: ' + personsTable.lastError() } }
  233. personTouched(x.id)
  234. if (photo.failed != null) { console.log('person fill: ' + p.name + ': ' + photo.failed) }
  235. jobs[p.id] = { items = items pos = 0 credits = [] added = 0 requests = 1 + photo.requests started = t0 steps = 1 tmdbMs = now() - t0 }
  236. return { done = false pos = 0 total = items.length }
  237. }
  238. // a title we don't have: the minimal record (shape: search.hl importTitle) → its id, or null
  239. static addMinimalTitle = (&cr, &pid, pname) => {
  240. kindType = cr.kind == 'tv' ? 'series' : 'movie'
  241. rel = cr.release
  242. yr = rel != null && rel.length >= 4 ? toNumber(rel.slice(0, 4)) : null
  243. slug = slugOf(cr.title, cr.tmdbId)
  244. record = {
  245. id = newId() title = cr.title summary = null tmdbSummary = cr.overview tmdbId = cr.tmdbId
  246. language = cr.language type = kindType release = rel year = yr image = null
  247. urlSegment = slug episodesCount = null homepage = null imdbId = null seasonsCount = null
  248. status = null tagline = null tvdbId = null tvmzId = null genres = genreIdsOfTmdb(cr.genreIds)
  249. cast = [{ person = pid character = cr.role actor = pname }] seasons = [] oldId = 'tmdb-' + cr.kind + '-' + cr.tmdbId
  250. adult = cr.adult imported = now() minimal = true
  251. }
  252. // mission 060: its short id (shortids.hl — unique across shows and persons)
  253. record.shortId = claimShortId('show', record.id, record.oldId)
  254. if (showsTable.put(record) == null) {
  255. console.log('person fill: "' + cr.title + '" not stored: ' + showsTable.lastError())
  256. return null
  257. }
  258. indexShow(record)
  259. refreshCatalogShow(record.id)
  260. return record.id
  261. }
  262. // every next step: the next `fillBatch` titles; the last one links the credits and marks the person complete
  263. static fillStep = (p, jobIn) => {
  264. job = jobIn
  265. total = job.items.length
  266. let end = job.pos + fillBatch
  267. if (end > total) { end = total }
  268. credits = job.credits
  269. let added = job.added
  270. pid = p.id
  271. pname = p.name
  272. let i = job.pos
  273. while (i < end) {
  274. let item = job.items[i]
  275. let sid = titleIdByTmdb(item.kind == 'tv' ? 'series' : 'movie', item.tmdbId)
  276. if (sid == null) {
  277. sid = addMinimalTitle(item, pid, pname)
  278. if (sid != null) { added = added + 1 }
  279. }
  280. if (sid != null) { credits.push({ show = sid character = item.role posterPath = item.posterPath adult = item.adult }) }
  281. i = i + 1
  282. }
  283. job.pos = end
  284. job.credits = credits
  285. job.added = added
  286. job.steps = job.steps + 1
  287. jobs[pid] = job
  288. if (end < total) { return { done = false pos = end total = total } }
  289. // done: the credits (+ the migrated ones TMDB no longer lists), complete
  290. have = {}
  291. all = []
  292. for (c of credits) { have[c.show] = true all.push(c) }
  293. for (c of listOr(p.shows)) { if (c != null && c.show != null && have[c.show] != true) { all.push(c) } }
  294. x = p
  295. x.shows = all
  296. x.filmographyAt = now()
  297. x.updatedAt = x.filmographyAt
  298. if (personsTable.update(x.id, x) == null) { return { error = 'could not store the filmography: ' + personsTable.lastError() } }
  299. personTouched(x.id)
  300. personsTable.persist()
  301. showsTable.persist()
  302. jobs[pid] = null
  303. console.log('person fill: ' + pname + ' (tmdb ' + p.tmdbId + ') credits=' + total + ' added=' + added + ' requests=' + job.requests + ' steps=' + job.steps + ' tmdbMs=' + job.tmdbMs + ' ms=' + (now() - job.started))
  304. return { done = true pos = total total = total added = added }
  305. }
  306. // one step for the person with this id: { done, pos, total } or { error }
  307. static personFillStep = (&personId) => {
  308. p = personsTable.fetch(personId)
  309. if (p == null) { return { error = 'no such person' } }
  310. if (isComplete(p)) { return { done = true } }
  311. if (p.tmdbId == null) { return { error = 'TMDB does not know this person' } }
  312. job = jobs[personId]
  313. if (job == null) { return startFill(p) }
  314. return fillStep(p, job)
  315. }
  316. // ---- people from a title's cast (tracker#26, details.hl completeStep) ------------------------------------------
  317. // TMDB person id → our person id, built once (15k people, one scan), kept up to date by castPersonId
  318. // (flat statics, written key by key like search.hl's haveTmdb: `let m = …ids` would copy the whole map — plain data
  319. // deep-copies on assignment)
  320. static personTmdbBuilt = { yes = false }
  321. static personTmdbIds = {}
  322. static noteTmdbOfPerson = (&p) => {
  323. if (p.tmdbId != null) { personTmdbIds['' + p.tmdbId] = p.id }
  324. return null
  325. }
  326. // bootscan.hl streamed every person through noteSlugOfPerson / noteTmdbOfPerson, every title through noteCastProfiles
  327. static markPeopleIndexesBuilt = (&castPhotos) => {
  328. slugIndex.built = true
  329. personTmdbBuilt.yes = true
  330. peopleView.castProfiles = castPhotos
  331. return true
  332. }
  333. static personIdByTmdb = (&tmdbId) => {
  334. if (!personTmdbBuilt.yes) {
  335. personTmdbBuilt.yes = true
  336. for (p of personsTable.iterate(null, null)) { noteTmdbOfPerson(p) }
  337. }
  338. return personTmdbIds['' + tmdbId]
  339. }
  340. // a free person slug: "Maya-Rudolph", taken → "-2", … (the migrated people's form)
  341. static personSlugOf = (&name, &tmdbId) => {
  342. ensureSlugIndex()
  343. base = slugBaseOf(name, 'person-' + tmdbId)
  344. if (slugIndex.ids[base] == null) { return base }
  345. let n = 2
  346. while (slugIndex.ids[base + '-' + n] != null) { n = n + 1 }
  347. return base + '-' + n
  348. }
  349. // how many people castPersonId added since the start (details.hl counts a title's new people by the difference, tracker#28)
  350. static castPeopleAdded = { n = 0 }
  351. static castPeopleCount = () => { return castPeopleAdded.n }
  352. // our person for one cast entry of TMDB ({ tmdbId, name, character, gender, profile }) — the one we have by TMDB id, else a NEW
  353. // person (name, tmdbId, a free slug, this one credit, TMDB's profile path as `tmdbProfile` (tracker#37: the photo of /people and
  354. // the search); not complete: their page fetches the rest on the first visit like any migrated person). Answers the id, or null
  355. // when it could not be stored. Also a CREW person (tracker#28: `character` = the job, "Director").
  356. static castPersonId = (&c, &showId) => {
  357. have = personIdByTmdb(c.tmdbId)
  358. if (have != null) { return have }
  359. record = { id = newId() name = c.name tmdbId = c.tmdbId urlSegment = personSlugOf(c.name, c.tmdbId) oldId = 'tmdb-person-' + c.tmdbId
  360. gender = c.gender image = null tmdbProfile = textOr(c.profile) adult = false birthDate = null birthPlace = null deathDate = null imdbId = null
  361. shows = showId != null ? [{ show = showId character = c.character }] : [] added = now() }
  362. // mission 062 (tracker#27 for #26/#28): every person the app creates gets its short id at once (shortids.hl)
  363. record.shortId = claimShortId('person', record.id, record.oldId)
  364. if (personsTable.put(record) == null) {
  365. console.log('details: person "' + c.name + '" not stored: ' + personsTable.lastError())
  366. return null
  367. }
  368. personTmdbIds['' + c.tmdbId] = record.id
  369. slugIndex.ids[record.urlSegment] = record.id
  370. indexPerson(record)
  371. personTouched(record.id)
  372. castPeopleAdded.n = castPeopleAdded.n + 1
  373. return record.id
  374. }
  375. // ---- GUEST STARS (mission 062): a series' guests are NOT person records — details.hl stores them on the title ({ tmdbId, actor,
  376. // character, profile, episodes, guest }, `person` only when we have them already). 160k people (most of them one-episode guests)
  377. // cost 3.3 GB of RSS at boot (w061). A guest's link is `/person/tmdb/<tmdbId>?show=<show id>` (project.hl guestRoute): the FIRST
  378. // open creates the person (name + role from that title's guest entry; without one — an old link — TMDB's /person/<id>), with its
  379. // short id, then the person page fills it like any other.
  380. static guestOf = (&showId, &tmdbId) => {
  381. s = showId != null && hlTypeName(showId) == 'String' && showId != '' ? showsTable.fetch(showId) : null
  382. if (s == null) { return null }
  383. for (c of listOr(s.cast)) { if (c != null && c.tmdbId == tmdbId && textOr(c.actor) != null) { return c } }
  384. return null
  385. }
  386. // our person for this TMDB person (created now when we have none) → { id, created } or { error }
  387. static guestPersonId = (tmdbId, showId) => {
  388. have = personIdByTmdb(tmdbId)
  389. if (have != null) { return { id = have created = false } }
  390. g = guestOf(showId, tmdbId)
  391. let c = null
  392. let inShow = null
  393. if (g != null) {
  394. c = { tmdbId = tmdbId name = g.actor character = g.character gender = null }
  395. inShow = showId
  396. } else {
  397. d = tmdbGet('/person/' + tmdbId)
  398. if (d.failed != null) { return { error = 'TMDB: ' + d.failed } }
  399. if (textOr(d.name) == null || d.adult == true) { return { error = 'no such person' } }
  400. c = { tmdbId = tmdbId name = d.name character = null gender = d.gender }
  401. }
  402. pid = castPersonId(c, inShow)
  403. if (pid == null) { return { error = 'could not store the person' } }
  404. personsTable.persist()
  405. console.log('guest opened: ' + c.name + ' (tmdb ' + tmdbId + ') → new person ' + pid + (inShow != null ? ' from show ' + inShow : ' from TMDB'))
  406. return { id = pid created = true }
  407. }
  408. // ---- /people (tracker#37, mission 032): everyone we have, the one updated last first ------------------------------------------
  409. // Creator: "/people just lists persons by last one updated with poster name and pagination". LAST UPDATED = the newest of
  410. // `updatedAt` (stamped by the person page's fetch, startFill / fillStep), `filmographyAt` and `added` (a person the app created);
  411. // the migrated people with none of them come last (newest stored first). No adult filter (tracker#32's /people had none either).
  412. // A CACHED VIEW like tracker#30's list caches (mission 025): the order is built ONCE at boot (project.hl warmPeopleView: one pass
  413. // over the people + one sort, and the cast photos below — 105k people, 3–4 s on the live copy), a page only slices its 24 ids and
  414. // fetches those people (~15 ms server time, any page). Kept current without a rebuild: every person the app creates or updates is
  415. // `personTouched` → `recent`; the next page read moves them to the front (one pass over the ids, ~50 ms, only after a change).
  416. static peopleView = { built = false ids = [] recent = [] castProfiles = 0 }
  417. static updatedKeyOf = (&p) => {
  418. let k = p.updatedAt != null ? p.updatedAt : 0
  419. if (p.filmographyAt != null && p.filmographyAt > k) { k = p.filmographyAt }
  420. if (p.added != null && p.added > k) { k = p.added }
  421. return k
  422. }
  423. // one person's entry of the view's list (table order; bootscan.hl's person pass collects them)
  424. static viewEntryOf = (&p) => { return { id = p.id key = updatedKeyOf(p) } }
  425. static warmPeopleView = () => {
  426. if (peopleView.built) { return peopleView.ids.length }
  427. peopleView.castProfiles = castProfilesOf()
  428. let stored = []
  429. for (p of personsTable.iterate(null, null)) { stored.push(viewEntryOf(p)) }
  430. return finishPeopleView(stored)
  431. }
  432. static finishPeopleView = (&stored) => {
  433. // newest stored first: the table is in the order the people were stored, so the list is nearly sorted already (util.hl sortDesc
  434. // keeps runs) and equal keys (the migrated people: 0) end newest stored first
  435. newestFirst = []
  436. let i = stored.length - 1
  437. while (i >= 0) { newestFirst.push(stored[i]) i = i - 1 }
  438. ids = []
  439. for (x of sortDesc(newestFirst)) { ids.push(x.id) }
  440. peopleView.ids = ids
  441. peopleView.built = true
  442. return ids.length
  443. }
  444. // a person was created or updated: first in /people from the next page read on
  445. static personTouched = (&id) => {
  446. if (peopleView.built && id != null) { peopleView.recent.push(id) }
  447. return null
  448. }
  449. static peopleViewCurrent = () => {
  450. if (!peopleView.built) { warmPeopleView() }
  451. if (peopleView.recent.length == 0) { return true }
  452. seen = {}
  453. out = []
  454. let i = peopleView.recent.length - 1
  455. while (i >= 0) {
  456. let id = peopleView.recent[i]
  457. if (seen[id] != true) { seen[id] = true out.push(id) }
  458. i = i - 1
  459. }
  460. for (id of peopleView.ids) { if (seen[id] != true) { out.push(id) } }
  461. peopleView.ids = out
  462. peopleView.recent = []
  463. return true
  464. }
  465. static peopleViewStats = () => { return { people = peopleView.ids.length castProfiles = peopleView.castProfiles } }
  466. static peopleCount = () => {
  467. peopleViewCurrent()
  468. return peopleView.ids.length
  469. }
  470. // a person's photo for a tile or a search row: our file (photoUrlOf), else TMDB's (`tmdbProfile`, w185 — like the filmography's
  471. // TMDB posters), else TMDB's from a title's cast entry (castProfiles), else the placeholder
  472. static personPhotoOf = (&p) => {
  473. local = photoUrlOf(p)
  474. if (local != '') { return local }
  475. path = textOr(p.tmdbProfile) != null ? p.tmdbProfile : castProfiles[p.id]
  476. return path != null ? tmdbImageBase + '/w185' + path : '/profiles/none'
  477. }
  478. // person id → TMDB's profile path from a title's cast or crew entry (credits.hl stores `profile` there: cast since tracker#28, crew
  479. // since tracker#37) — for the people the credits job created before castPersonId kept it as `tmdbProfile` (live copy 2026-10-04:
  480. // 51,922 of 105,018 people; the crew entries had none yet). Built at boot with the view (one pass over the titles, ~1–3 s). IN
  481. // MEMORY, not written to the people: 51,907 record updates took 511 s at a start (mission 032, measured on the live copy).
  482. // (Flat static written key by key, like the maps above.)
  483. static castProfiles = {}
  484. // one title's cast and crew photos into the map; how many were new (bootscan.hl's title pass sums them)
  485. static noteCastProfiles = (&s) => {
  486. let n = 0
  487. for (c of listOr(s.cast)) {
  488. if (c != null && c.person != null && textOr(c.profile) != null && castProfiles[c.person] == null) {
  489. castProfiles[c.person] = c.profile
  490. n = n + 1
  491. }
  492. }
  493. for (c of listOr(s.crew)) {
  494. if (c != null && c.person != null && textOr(c.profile) != null && castProfiles[c.person] == null) {
  495. castProfiles[c.person] = c.profile
  496. n = n + 1
  497. }
  498. }
  499. return n
  500. }
  501. static castProfilesOf = () => {
  502. let n = 0
  503. // iterate, not find: one title decoded at a time
  504. for (s of showsTable.iterate(null, null)) { n = n + noteCastProfiles(s) }
  505. return n
  506. }
  507. // one page: { page, pages, tiles } — a tile like the title grids' (components/tilelist.hl): photo, name, no year
  508. static peoplePage = (&raw) => {
  509. peopleViewCurrent()
  510. pages = pagesOf(peopleView.ids.length)
  511. page = pageNumberOf(raw, pages)
  512. tiles = []
  513. for (id of peopleView.ids.slice((page - 1) * pageSize, page * pageSize)) {
  514. let p = personsTable.fetch(id)
  515. if (p != null && p.urlSegment != null) {
  516. tiles.push({ href = '/person/' + p.urlSegment posterUrl = personPhotoOf(p) title = p.name != null ? '' + p.name : '' year = '' })
  517. }
  518. }
  519. return { page = page pages = pages tiles = tiles }
  520. }
  521. // ---- THE PHOTO BACKFILL (mission 032, for tracker#37's /people) ----------------------------------------------------------------
  522. // The people the credits job created are mostly CREW with no photo source at all (live copy 2026-10-04: /people pages 1–3 = 72 of
  523. // 72) — one TMDB request per person (`GET /person/<tmdbId>`, ≤ 40 / 10 s, jobs.hl `photoTick`, the last start job) stores TMDB's
  524. // `profile_path` as `tmdbProfile` (shown from TMDB w185 like the cast photos, no file downloaded); TMDB has none → `photoCheck`
  525. // (never asked again), 404 the same. In /people's order (the one updated last first): page 1 is filled first. Not an update of
  526. // the person (`updatedAt` untouched). Resumes after a restart (the queue = who still has no photo source and no check).
  527. static needsPhoto = (&p) => {
  528. return p.tmdbId != null && p.photoCheck == null && textOr(p.tmdbProfile) == null && castProfiles[p.id] == null && photoUrlOf(p) == ''
  529. }
  530. // mission 046: the queue is built in SLICES (lib/slices.hl's idea, ids only): the people view's id list (already in /people's order, in RAM) is
  531. // walked from a cursor, a tick fetches rows until its time budget (sliceBudgetMs) is spent — the old one-shot loop did ~621 K fetches in
  532. // ONE tick. Same queue, same order (the view's order); the view is snapshotted at the start (people added while it runs are the next start's).
  533. static photoQueueStart = () => {
  534. peopleViewCurrent()
  535. return { ids = peopleView.ids pos = 0 out = [] ms = 0 steps = 0 skipped = 0 }
  536. }
  537. // true when the whole list is done
  538. static photoQueueStep = (&c) => {
  539. t0 = now()
  540. n = c.ids.length
  541. while (c.pos < n) {
  542. let id = c.ids[c.pos]
  543. c.pos = c.pos + 1
  544. let p = personsTable.fetch(id)
  545. // the damaged persons index of the real data: fetch() can answer a Number (mission 045, slices.hl): skipped + counted, as sliceNext does
  546. if (p != null && hlTypeName(p) != 'Hybrid') { c.skipped = c.skipped + 1 }
  547. else if (p != null && needsPhoto(p)) { c.out.push(id) }
  548. if (c.pos % 64 == 0 && now() - t0 >= sliceBudgetMs) { break }
  549. }
  550. c.steps = c.steps + 1
  551. c.ms = c.ms + (now() - t0)
  552. return c.pos >= n
  553. }
  554. // ONE person → { requests, found (a photo stored), errors, error, retry (a passing failure: ask again later) }
  555. static photoStep = (&personId) => {
  556. out = { requests = 0 found = false errors = 0 error = '' retry = false }
  557. p = personsTable.fetch(personId)
  558. if (p == null || !needsPhoto(p)) { return out }
  559. out.requests = 1
  560. d = tmdbGet('/person/' + p.tmdbId)
  561. x = p
  562. if (d.failed != null) {
  563. out.errors = 1
  564. out.error = p.name + ': ' + d.failed
  565. if (d.status != 404) { out.retry = true return out }
  566. x.photoCheck = 'TMDB 404'
  567. } else if (textOr(d.profile_path) != null) {
  568. x.tmdbProfile = d.profile_path
  569. out.found = true
  570. } else { x.photoCheck = 'TMDB has none' }
  571. if (personsTable.update(x.id, x) == null) { out.errors = out.errors + 1 out.error = p.name + ': not stored: ' + personsTable.lastError() }
  572. return out
  573. }
  574. static persistPhotos = () => {
  575. personsTable.persist()
  576. return true
  577. }

Branches

Latest commits

  • f57f64f0mission 046: report, LOGmre
  • efe1b485mission 046: re-vendor Hybriel master 60b2817b (integration 8, #203): plugins mpackdb + web (bin/ git-ignored: hybriel, libhlrt.a, hlc.o, hlrt/ from native/zig-out)mre
  • 53099ffbmission 046: person page reads a slim per-title credit row cache (no whole-record fetch per credit); gate tests/person-lookup.mjsmre
  • 9b3828aemission 046a: report, LOGmre
  • 4b37356emission 046: franchise seed supersedeCollections reads the curated timelines once (10 s one-tick block at the end of boot)mre
  • ac6268e3mission 046a: re-vendor Hybriel 528bc89f (integration 6): plugins mpackdb/time/web (bin/ git-ignored)mre
  • 322b3293mission 046: photoQueue guard for non-row fetch results (damaged persons index)mre
  • e5b99aecmission 046: photoQueue in slices (people.hl photoQueueStart/Step, jobs.hl photoTick), tests/photoqueue.hlmre
  • 31dcf2admission 045: report, LOGmre
  • 9c3971d0tests/persons-skip-count.hlmre
  • 4d72e365mission 045mre
  • b9cae4c9slices: skip and count non-row values from range(); merge persons pass falls back to iterate; guard testmre
  • 3db7dc31merge w44-jobs-slices into w43-trkmemmre
  • 593dab6fmission 043: report, LOGmre
  • 1a5a4818boot scans back to iterate (slices measured slower: titles 114 s, episodes 141 s, persons failed); lambda per rowmre
  • 03d29893boot scans read primary-key slices (lib/slices.hl from mission 044): iterate is eager too; people view log line back; allShows/allEpisodes stay for toolsmre
  • 23375c58mission 044: report, LOG, STATUSmre
  • c6fd928emission 044: 4096 slices for tables over 200 K rows, 15 ms budget, TRACKER_SLICE_DEPTH for testsmre
  • fb057680mission 044: jobs.hl imports mergeLeft (gate found it), mission file, slices benchmre
  • 0d6293ebmission 043: mission filemre