gitoriaLog in with ident

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit1ed5457e1ed5457etracker#20 + #21 (mission 068): typed headings "<Type> | <name>" in type colours; TV titles split into Series (/series) and Shows (/shows) by TMDB type + Reality/Talk/News genres — kind stored by sync/import/adult backfill + new kind backfill (resumes), /movies/<slug>, /shows/<slug> of a series/movie → 301, /my/series + /my/shows, home 5 tiles + 3 rows, search/filmography labels; gates kinds 32 + browser 266, tests/realdata-068.mjs, tools/count-kinds.hl, docs/kinds.md, README + STATUSmre1ed5457e/shows.hl

10.3 KB

  1. // shows.hl — READ-ONLY data access for a show's page (tracker.worldapi.org#4): shows, seasons,
  2. // episodes, persons, genres (storage/mpackdb/, migrated by tools/migrate.hl, tracker.worldapi.org#2).
  3. // Kept apart from watches.hl (the per-user watch state) and from components/show.hl (the page itself)
  4. // so /unwatched and /schedule (tickets #5/#6, "reuse, don't copy") can import the same functions.
  5. //
  6. // No declared index beyond the migration's own ['!oldId'] (opening a table with indexes it wasn't
  7. // created with is untested territory, hybriel#21 "opening a table rewrites its files") — a slug
  8. // lookup is a linear scan of ~9500 shows, cheap for one page load.
  9. import { MPackDB } from 'hl:mpackdb'
  10. import { env } from 'hl:proc'
  11. import { exists } from 'hl:fs'
  12. static envOr = (name, fallback) => {
  13. let v = env(name)
  14. return v != null && v.trim() != '' ? v.trim() : fallback
  15. }
  16. static storageDir = envOr('TRACKER_STORAGE', './storage/mpackdb')
  17. static genresTable = new MPackDB(file = storageDir + '/genres.db', primaryKey = '@id', indexes = ['!oldId'])
  18. static personsTable = new MPackDB(file = storageDir + '/persons.db', primaryKey = '@id', indexes = ['!oldId'])
  19. static showsTable = new MPackDB(file = storageDir + '/shows.db', primaryKey = '@id', indexes = ['!oldId'])
  20. static seasonsTable = new MPackDB(file = storageDir + '/seasons.db', primaryKey = '@id', indexes = ['!oldId'])
  21. static episodesTable = new MPackDB(file = storageDir + '/episodes.db', primaryKey = '@id', indexes = ['!oldId'])
  22. // the show with this urlSegment (slug), or null
  23. static showBySlug = (slug) => {
  24. if (slug == null || hlTypeName(slug) != 'String' || slug == '') { return null }
  25. for (s of showsTable.find(null, null)) { if (s.urlSegment == slug) { return s } }
  26. return null
  27. }
  28. static genreById = (id) => { return id == null ? null : genresTable.fetch(id) }
  29. // /genres/<genre> (mission 058): the genre with this urlSegment ('Action', 'Sci-Fi-Fantasy'), exact first, else ignoring case
  30. // — a scan of the genres table (27 rows)
  31. static genreBySlug = (slug) => {
  32. if (slug == null || hlTypeName(slug) != 'String' || slug == '') { return null }
  33. let lower = slug.toLowerCase()
  34. let loose = null
  35. for (g of genresTable.find(null, null)) {
  36. if (g.urlSegment == slug) { return g }
  37. if (loose == null && g.urlSegment != null && ('' + g.urlSegment).toLowerCase() == lower) { loose = g }
  38. }
  39. return loose
  40. }
  41. static personById = (id) => { return id == null ? null : personsTable.fetch(id) }
  42. static seasonById = (id) => { return id == null ? null : seasonsTable.fetch(id) }
  43. static showById = (id) => { return id == null ? null : showsTable.fetch(id) }
  44. static episodeById = (id) => { return id == null ? null : episodesTable.fetch(id) }
  45. // EVERY show / episode (tracker.worldapi.org#13, catalog.hl): read ONCE when the app starts, to build the public lists —
  46. // never per request
  47. static allShows = () => { return showsTable.find(null, null) }
  48. static allEpisodes = () => { return episodesTable.find(null, null) }
  49. // insertion sort, hand-written (no list sort(), hybriel#1) — a show has a handful of seasons at most
  50. static insertDesc = (sorted, s) => {
  51. let out = []
  52. let inserted = false
  53. for (x of sorted) {
  54. if (!inserted && s.seasonNumber >= x.seasonNumber) { out.push(s) inserted = true }
  55. out.push(x)
  56. }
  57. if (!inserted) { out.push(s) }
  58. return out
  59. }
  60. static insertAsc = (sorted, e) => {
  61. let out = []
  62. let inserted = false
  63. for (x of sorted) {
  64. if (!inserted && e.episodeNumber <= x.episodeNumber) { out.push(e) inserted = true }
  65. out.push(x)
  66. }
  67. if (!inserted) { out.push(e) }
  68. return out
  69. }
  70. // a show's seasons, latest (highest seasonNumber) first — creator: "all seasons in reverse order,
  71. // latest on top"
  72. static seasonsOfShow = (show) => {
  73. let sorted = []
  74. for (id of show.seasons) {
  75. let s = seasonsTable.fetch(id)
  76. if (s != null) { sorted = insertDesc(sorted, s) }
  77. }
  78. return sorted
  79. }
  80. // a season's episodes, in episode-number order
  81. static episodesOfSeason = (season) => {
  82. let sorted = []
  83. for (id of season.episodes) {
  84. let e = episodesTable.fetch(id)
  85. if (e != null) { sorted = insertAsc(sorted, e) }
  86. }
  87. return sorted
  88. }
  89. // ---- labels shared by every list (tracker.worldapi.org#8) -------------------------------------
  90. static pad2 = (n) => {
  91. let s = '' + n
  92. while (s.length < 2) { s = '0' + s }
  93. return s
  94. }
  95. // an episode as `S01E01` (creator: "write it like that: S01E01") — /my/shows, /my/unwatched, /my/schedule
  96. static episodeCode = (seasonNumber, episodeNumber) => { return 'S' + pad2(seasonNumber) + 'E' + pad2(episodeNumber) }
  97. // the show's year: `year`, else the first 4 characters of `release` ('YYYY-MM-DD'; 197 migrated shows have
  98. // no year, a few of those an empty release) — null when neither gives one
  99. static showYear = (show) => {
  100. if (show.year != null) { return '' + show.year }
  101. if (show.release != null && hlTypeName(show.release) == 'String' && show.release.length >= 4) { return show.release.slice(0, 4) }
  102. return null
  103. }
  104. // a show's title as every list shows it: `Doctor Who (2005)` (creator: so same-named shows can be told apart)
  105. static titleWithYear = (show) => {
  106. let y = showYear(show)
  107. return y != null ? show.title + ' (' + y + ')' : show.title
  108. }
  109. // ---- adult titles (mission 054) -----------------------------------------------------------------
  110. // `adult` on every title: true / false from TMDB, null = not known yet (the migrated rows; the app's backfill, tmdbsync.hl
  111. // `backfillTitle`, asks TMDB). The PUBLIC lists (catalog.hl: homepage rows, /movies, /shows) and the search (search.hl) show
  112. // ONLY a title TMDB says is not adult — unknown counts as hidden. A title the user follows stays on /my/* and its page.
  113. static isPublicTitle = (show) => { return show != null && show.adult == false }
  114. // ---- the kind of a title (tracker.worldapi.org#21, mission 068) ----------------------------------------------
  115. // A TV title (`type = 'series'`) is either a SERIES (TMDB's TV type Scripted, Miniseries, Documentary) or a SHOW (Reality,
  116. // Talk Show, News, Video — or one of our genres Reality / Talk / News, whatever the type). Stored on the title as `kind`
  117. // ('series' | 'show') and TMDB's type as `tmdbType`: by the kind backfill (tmdbsync.hl `kindTitle`, one TMDB request per
  118. // title without a show genre), the daily sync and the search import. Until a title has `kind`, its genres decide (no
  119. // type known yet = series). Movies are `type = 'movie'` and have no kind.
  120. static showTmdbTypes = ['Reality', 'Talk Show', 'News', 'Video']
  121. static showGenreNames = ['Reality', 'Talk', 'News']
  122. static showGenreIdsOf = () => {
  123. let out = {}
  124. for (g of genresTable.find(null, null)) { if (g.name != null && showGenreNames.includes('' + g.name)) { out[g.id] = true } }
  125. return out
  126. }
  127. // the genre ids that make a title a show — read once (the genres table never changes while the app runs)
  128. static showGenreIds = showGenreIdsOf()
  129. static hasShowGenre = (genres) => {
  130. if (genres == null) { return false }
  131. for (g of genres) { if (showGenreIds[g] == true) { return true } }
  132. return false
  133. }
  134. // 'show' | 'series' from TMDB's type (null = not known) and the title's genre ids
  135. static kindOfTitle = (tmdbType, genres) => {
  136. if (hasShowGenre(genres)) { return 'show' }
  137. if (tmdbType != null && showTmdbTypes.includes('' + tmdbType)) { return 'show' }
  138. return 'series'
  139. }
  140. // 'movie' | 'series' | 'show' of a stored title
  141. static titleKind = (show) => {
  142. if (show.type == 'movie') { return 'movie' }
  143. if (show.kind == 'show' || show.kind == 'series') { return show.kind }
  144. return kindOfTitle(show.tmdbType, show.genres)
  145. }
  146. // a title's own page: /movies/<slug>, /series/<slug>, /shows/<slug> (the old /shows/<slug> of a movie or series answers 301,
  147. // project.hl showsRoute)
  148. static titlePath = (show) => {
  149. let k = titleKind(show)
  150. return (k == 'movie' ? '/movies/' : (k == 'show' ? '/shows/' : '/series/')) + show.urlSegment
  151. }
  152. // tracker.worldapi.org#20: the type word of a page heading / list label and its colour class (styles.hl `type-*`)
  153. static kindWord = (kind) => { return kind == 'movie' ? 'Movie' : (kind == 'show' ? 'Show' : 'Series') }
  154. static typeWordOf = (show) => { return kindWord(titleKind(show)) }
  155. static typeClassOf = (show) => { return 'type-word type-' + titleKind(show) }
  156. // the poster file name (oldId + the migrated extension) — null when either is missing; served at
  157. // /posters/<name> (project.hl), which falls back to a placeholder when the file itself isn't there
  158. // yet (the real poster files are not part of the mongo export, see the report's `open`)
  159. static posterName = (show) => {
  160. if (show.oldId == null || show.image == null || show.image == '') { return null }
  161. return show.oldId + '.' + show.image
  162. }
  163. // the poster's URL for a LIST (/my/shows, tracker.worldapi.org#8): `/posters/<name>` only when the file really
  164. // is in storage/mpackdb/posters/ (project.hl posterRoute reads the same folder), else the ONE placeholder URL
  165. // `/posters/none` — the real data has no poster files yet, and 128 different URLs all answering the same
  166. // placeholder were 128 requests per page load (the browser can't cache them as one)
  167. static posterUrlOf = (show) => {
  168. let name = posterName(show)
  169. return name != null && exists(storageDir + '/posters/' + name) ? '/posters/' + name : '/posters/none'
  170. }
  171. // ---- the show on other sites (tracker.worldapi.org#12) ------------------------------------------
  172. // the header's link icons: TMDB, IMDb, TheTVDB, TVmaze — ONLY for the ids the show has; a movie (`type = 'movie'`)
  173. // gets TMDB + IMDb. TheTVDB's id form is `/dereferrer/series/<id>` (checked 2026-10-01: it and the old
  174. // `?tab=series&id=` both 301 to the slug page); the others are the sites' own id URLs (they too 301 to a slug).
  175. static hasExternalId = (v) => { return v != null && ('' + v).trim() != '' }
  176. static externalLinksOf = (show) => {
  177. let out = []
  178. let movie = show.type == 'movie'
  179. if (hasExternalId(show.tmdbId)) { out.push({ site = 'tmdb' label = 'TMDB' title = 'On TMDB' href = 'https://www.themoviedb.org/' + (movie ? 'movie/' : 'tv/') + show.tmdbId }) }
  180. if (hasExternalId(show.imdbId)) { out.push({ site = 'imdb' label = 'IMDb' title = 'On IMDb' href = 'https://www.imdb.com/title/' + show.imdbId + '/' }) }
  181. if (!movie && hasExternalId(show.tvdbId)) { out.push({ site = 'tvdb' label = 'TVDB' title = 'On TheTVDB' href = 'https://thetvdb.com/dereferrer/series/' + show.tvdbId }) }
  182. if (!movie && hasExternalId(show.tvmzId)) { out.push({ site = 'tvmaze' label = 'TVmaze' title = 'On TVmaze' href = 'https://www.tvmaze.com/shows/' + show.tvmzId }) }
  183. return out
  184. }

Branches

Latest commits

  • 1ed5457etracker#20 + #21 (mission 068): typed headings "<Type> | <name>" in type colours; TV titles split into Series (/series) and Shows (/shows) by TMDB type + Reality/Talk/News genres — kind stored by sync/import/adult backfill + new kind backfill (resumes), /movies/<slug>, /shows/<slug> of a series/movie → 301, /my/series + /my/shows, home 5 tiles + 3 rows, search/filmography labels; gates kinds 32 + browser 266, tests/realdata-068.mjs, tools/count-kinds.hl, docs/kinds.md, README + STATUSmre
  • fa1f9dfatracker#29 (mission 063): unwatched check muted grey outline + check (accent only on hover), watched stays solid — no code regression, the accent outline read as ticked; gate checks real checks visibly (computed style + screenshot pixel) on /my/unwatched, show, movie, /my/movies; gate 266, tests/realdata-063.mjs, README + STATUSmre
  • d8b12d67tracker#27 (mission 060): short ids for movies, series and persons — old 702 kept (data/old-short-ids.json), new random [a-z0-9]{5} unique across both, claimed at creation, background backfill (resumes), shown under poster/photo, /<shortId> → 301; gate 259, tests/realdata-060*, README + STATUSmre
  • f14db671tracker#22-#25 (mission 058): episode air dates, season check = all episodes watched, movie watched check (+ /my/movies count), /genres/<genre> pages (movies + series, newest first, paginated); gate 238, tests/realdata-058.mjs, README + STATUSmre
  • 1704ec45tracker#17 (mission 057): season caret down/up, skeleton rows while a season loads, sessionless showSeasonEpisodes face (no page re-mount), client-only close; gate 214, tests/realdata-057.mjs, README + STATUSmre
  • f2fe3e36mission 056: README + STATUS (merge, fixes, Hybriel 8590df63, real-data check), tests/realdata-056.mjs, tools/check-public-slugs.hlmre
  • f40c250emission 056: re-vendor hybriel master 8590df63 (#121, #122); an adult title's page is Not found for non-followers; gate: leave the page before stopping the servermre
  • 2b7fdd6cmission 056: signed-out header one row on phones ("Log in", nowrap), backfill skips adult titles' posters, gate checksmre
  • 2c53d5efMerge branch 't16-person' (tracker#16 person pages) into main; filmography shows only public titles (054 adult flag), gate race fix (backfill start line)mre
  • c171227emission 054: hide adult/unknown titles from the public lists and the search; in-app adult-flag backfill (TMDB details + poster per title, resumes), gate + real-data proofmre
  • 139fafd8tracker#16: short bio (4 lines, click = all), real-data check script, README + STATUSmre
  • 93be9476tracker#16: person pages /person/<slug> with the filmography fetched from TMDB on the first visit (step by step), gatemre
  • 47a3cae6STATUS: mission 053 merge commit idsmre
  • dcc5eecaMerge branch 't14-search'mre
  • 03edc783Merge branch 't15-tvmaze'mre
  • 71b46345tracker#15: numbering check by date or title, placeholder titles in other languages, docs + real-data proofmre
  • 6bb2daf1tracker#13: homepage (tiles, intro, latest movies/shows), /shows, /movies/page/N, /my/movies; lists cached in memorymre
  • b8bd1157tracker#14: README + STATUS (search, real-data numbers, gate, merge notes)mre
  • 65c694a8tracker#14: search — header magnifier, /search/<text> (in-memory word-prefix index over titles + people), Fetch from web (TMDB search/multi, ours left out), Add = import via syncShow; gate +25 checks, real-data scriptmre
  • 34f2c15btracker#15: TVmaze merge in the sync (gaps only: new episodes/seasons, empty titles/air dates; numbering check), fake TVmaze episodes + gatemre