gitoriaLog in with ident

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitf14db671f14db671tracker#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 + STATUSmref14db671/catalog.hl

11.3 KB

  1. // catalog.hl — THE PUBLIC LISTS (tracker.worldapi.org#13): every movie, newest release first, and every series, the one
  2. // whose newest RELEASED episode is newest first — the homepage's two rows (10 each) and /movies, /shows (24 per page).
  3. // Movies and series are both rows of `shows` (`type` 'movie' / 'series', the old tracker's shape).
  4. //
  5. // BUILT ONCE when the app starts — one scan of shows and of episodes (real data: 9,453 shows, 232k episodes, ~1 s) — and
  6. // kept in memory as two sorted lists of { id, key }. A request only slices its page and fetches those 24 shows: no scan.
  7. // Kept up to date without a rebuild:
  8. // - the daily TMDB sync (project.hl syncTick) calls `refreshCatalogShow(id)` after each show: its entry is recomputed
  9. // from its own seasons/episodes (new episodes, moved release dates) and moved to its new place;
  10. // - a NEW DAY: an episode upcoming yesterday may be out today — each series keeps the release dates of its upcoming
  11. // episodes (`upcoming`), and the first read of a new day moves the ones that are out into `latest` and re-sorts.
  12. // tools/sync-tmdb.hl runs with the app stopped (hybriel#21): the next start builds the lists afresh.
  13. //
  14. // ADULT TITLES (mission 054): only a title with `adult == false` (shows.hl isPublicTitle) is in the two lists — unknown is
  15. // hidden. The adult backfill (project.hl backfillTick) calls `refreshCatalogShow(id)` per title, so the lists fill as the
  16. // flags arrive. The series' release maps (latest/upcoming/release) hold every series, shown or not.
  17. //
  18. // ORDER: a movie by its `release` ('YYYY-MM-DD'; none → last). A series by its newest released episode (release ≤
  19. // today, UTC like the rest of the app); a series with none released yet comes after all that have one, by its own
  20. // `release`. Ties keep the table's order. `key` is one string so one comparison sorts: series '1<date>' / '0<date>'.
  21. //
  22. // GENRES (mission 058, tracker#25, /genres/<genre>): per genre id one list of its public movies AND series, newest first by
  23. // ONE date for both kinds: a movie's `release`, a series' newest released episode (none → its own `release`). Built in the
  24. // same scans; `refreshCatalogShow` moves the title in the lists of its old genres (`genresOf`) and its current ones.
  25. import { now, timestamp } from 'hl:time'
  26. import { allShows, allEpisodes, showById, seasonsOfShow, episodesOfSeason, showYear, posterUrlOf, isPublicTitle } from './shows.hl'
  27. static pageSize = 24
  28. static todayStr = () => { return timestamp(now()).slice(0, 10) }
  29. static dateOr = (v) => { return v != null && hlTypeName(v) == 'String' && v.length >= 4 ? v : '' }
  30. static seriesKey = (latest, release) => { return latest != null && latest != '' ? '1' + latest : '0' + release }
  31. // a series in a genre list: the plain date (comparable with a movie's release) — its newest released episode, else its release
  32. static genreKey = (latest, release) => { return latest != null && latest != '' ? latest : release }
  33. // ---- sorting, hand-written (no list sort(), hybriel#1): a bottom-up merge sort, newest key first, stable ----------
  34. static mergeDesc = (a, b) => {
  35. let out = []
  36. let i = 0
  37. let j = 0
  38. while (i < a.length && j < b.length) {
  39. if (b[j].key > a[i].key) { out.push(b[j]) j = j + 1 } else { out.push(a[i]) i = i + 1 }
  40. }
  41. while (i < a.length) { out.push(a[i]) i = i + 1 }
  42. while (j < b.length) { out.push(b[j]) j = j + 1 }
  43. return out
  44. }
  45. static sortDesc = (list) => {
  46. if (list.length == 0) { return [] }
  47. let runs = []
  48. for (x of list) { runs.push([x]) }
  49. while (runs.length > 1) {
  50. let next = []
  51. let k = 0
  52. while (k + 1 < runs.length) { next.push(mergeDesc(runs[k], runs[k + 1])) k = k + 2 }
  53. if (k < runs.length) { next.push(runs[k]) }
  54. runs = next
  55. }
  56. return runs[0]
  57. }
  58. // one entry moved to its place: out of the list, then in before the first entry with a smaller key (linear)
  59. static placed = (list, entry) => {
  60. let out = []
  61. let done = false
  62. for (x of list) {
  63. if (x.id != entry.id) {
  64. if (!done && entry.key > x.key) { out.push(entry) done = true }
  65. out.push(x)
  66. }
  67. }
  68. if (!done) { out.push(entry) }
  69. return out
  70. }
  71. // one entry out of the list (a title that is not public, mission 054)
  72. static without = (list, id) => {
  73. let out = []
  74. for (x of list) { if (x.id != id) { out.push(x) } }
  75. return out
  76. }
  77. // ---- the index -------------------------------------------------------------------------------------------------
  78. static buildIndex = () => {
  79. let today = todayStr()
  80. let movies = []
  81. let seriesIds = []
  82. let release = {}
  83. let genreTitles = []
  84. for (s of allShows()) {
  85. if (s.type == 'movie') { if (isPublicTitle(s)) { movies.push({ id = s.id key = dateOr(s.release) }) } }
  86. else if (s.type == 'series') {
  87. if (isPublicTitle(s)) { seriesIds.push(s.id) }
  88. release[s.id] = dateOr(s.release)
  89. }
  90. if ((s.type == 'movie' || s.type == 'series') && isPublicTitle(s) && s.genres != null && s.genres.length > 0) {
  91. genreTitles.push({ id = s.id movie = s.type == 'movie' release = dateOr(s.release) genres = s.genres })
  92. }
  93. }
  94. let latest = {}
  95. let upcoming = {}
  96. for (e of allEpisodes()) {
  97. if (e.show != null && dateOr(e.release) != '') {
  98. if (e.release <= today) {
  99. let l = latest[e.show]
  100. if (l == null || e.release > l) { latest[e.show] = e.release }
  101. } else {
  102. let u = upcoming[e.show]
  103. if (u == null) { u = [] }
  104. u.push(e.release)
  105. upcoming[e.show] = u
  106. }
  107. }
  108. }
  109. let series = []
  110. for (id of seriesIds) { series.push({ id = id key = seriesKey(latest[id], release[id]) }) }
  111. let byGenre = {}
  112. let genresOf = {}
  113. for (t of genreTitles) {
  114. let key = t.movie ? t.release : genreKey(latest[t.id], t.release)
  115. for (g of t.genres) {
  116. let list = byGenre[g]
  117. if (list == null) { list = [] }
  118. list.push({ id = t.id key = key })
  119. byGenre[g] = list
  120. }
  121. genresOf[t.id] = t.genres
  122. }
  123. let genres = {}
  124. for (g of byGenre.keys()) { genres[g] = sortDesc(byGenre[g]) }
  125. return { day = today movies = sortDesc(movies) series = sortDesc(series) latest = latest upcoming = upcoming release = release genres = genres genresOf = genresOf }
  126. }
  127. static index = buildIndex()
  128. // a new day: upcoming episodes that are out now become their series' newest released one
  129. static rollDay = () => {
  130. let today = todayStr()
  131. if (index.day == today) { return null }
  132. let moved = false
  133. for (id of index.upcoming.keys()) {
  134. let rest = []
  135. let l = index.latest[id]
  136. for (d of index.upcoming[id]) {
  137. if (d <= today) {
  138. if (l == null || d > l) { l = d }
  139. moved = true
  140. } else { rest.push(d) }
  141. }
  142. index.upcoming[id] = rest
  143. if (l != null) { index.latest[id] = l }
  144. }
  145. if (moved) {
  146. let series = []
  147. for (x of index.series) { series.push({ id = x.id key = seriesKey(index.latest[x.id], index.release[x.id]) }) }
  148. index.series = sortDesc(series)
  149. // the genre lists too: a series' key is its newest released episode (movies keep theirs)
  150. for (g of index.genres.keys()) {
  151. let list = []
  152. for (x of index.genres[g]) { list.push({ id = x.id key = index.release[x.id] != null ? genreKey(index.latest[x.id], index.release[x.id]) : x.key }) }
  153. index.genres[g] = sortDesc(list)
  154. }
  155. }
  156. index.day = today
  157. return null
  158. }
  159. // one title out of the lists of the genres it was in, then (when public) into those of its current genres
  160. static refreshGenres = (show, shown, key) => {
  161. let old = index.genresOf[show.id]
  162. if (old != null) {
  163. for (g of old) { if (index.genres[g] != null) { index.genres[g] = without(index.genres[g], show.id) } }
  164. }
  165. index.genresOf[show.id] = null
  166. if (!shown || show.genres == null || show.genres.length == 0) { return null }
  167. for (g of show.genres) {
  168. let list = index.genres[g]
  169. if (list == null) { list = [] }
  170. index.genres[g] = placed(list, { id = show.id key = key })
  171. }
  172. index.genresOf[show.id] = show.genres
  173. return null
  174. }
  175. // after the sync (or the adult backfill) touched a show: its entry from its own data again (only that show's
  176. // seasons/episodes are read) — or out of the list when it is not public (adult or unknown, mission 054)
  177. static refreshCatalogShow = (showId) => {
  178. rollDay()
  179. let show = showById(showId)
  180. if (show == null) { return null }
  181. let shown = isPublicTitle(show)
  182. if (show.type == 'movie') {
  183. index.movies = shown ? placed(index.movies, { id = show.id key = dateOr(show.release) }) : without(index.movies, show.id)
  184. refreshGenres(show, shown, dateOr(show.release))
  185. return null
  186. }
  187. if (show.type != 'series') { return null }
  188. let today = index.day
  189. let l = null
  190. let up = []
  191. for (s of seasonsOfShow(show)) {
  192. for (e of episodesOfSeason(s)) {
  193. if (dateOr(e.release) != '') {
  194. if (e.release <= today) { if (l == null || e.release > l) { l = e.release } } else { up.push(e.release) }
  195. }
  196. }
  197. }
  198. index.latest[show.id] = l
  199. index.upcoming[show.id] = up
  200. index.release[show.id] = dateOr(show.release)
  201. index.series = shown ? placed(index.series, { id = show.id key = seriesKey(l, index.release[show.id]) }) : without(index.series, show.id)
  202. refreshGenres(show, shown, genreKey(l, index.release[show.id]))
  203. return null
  204. }
  205. // ---- what the pages read ---------------------------------------------------------------------------------------
  206. // one poster tile: poster, title, year, the show page (movies use the same /shows/<slug> page and its Follow button)
  207. static tileOf = (show) => {
  208. let y = showYear(show)
  209. return { id = show.id title = show.title year = y != null ? y : '' href = '/shows/' + show.urlSegment posterUrl = posterUrlOf(show) }
  210. }
  211. static tilesOf = (entries) => {
  212. let out = []
  213. for (x of entries) {
  214. let show = showById(x.id)
  215. if (show != null) { out.push(tileOf(show)) }
  216. }
  217. return out
  218. }
  219. // the homepage: the n latest RELEASED movies (none dated after today) and the n series with the newest released episode
  220. static latestMovies = (n) => {
  221. rollDay()
  222. let want = []
  223. for (x of index.movies) {
  224. if (want.length < n && x.key != '' && x.key <= index.day) { want.push(x) }
  225. if (want.length >= n) { return tilesOf(want) }
  226. }
  227. return tilesOf(want)
  228. }
  229. static latestSeries = (n) => {
  230. rollDay()
  231. let want = []
  232. for (x of index.series) {
  233. if (want.length < n && x.key.slice(0, 1) == '1') { want.push(x) }
  234. if (want.length >= n) { return tilesOf(want) }
  235. }
  236. return tilesOf(want)
  237. }
  238. // a page number from the address (`/movies/page/:page`, a String) — anything that is not a whole number ≥ 1 is page 1;
  239. // past the last page is the last page
  240. static pageNumberOf = (raw, pages) => {
  241. if (raw == null) { return 1 }
  242. let s = '' + raw
  243. if (s == '' || s.length > 6) { return 1 }
  244. let i = 0
  245. while (i < s.length) {
  246. let c = s.charCodeAt(i)
  247. if (c < 48 || c > 57) { return 1 }
  248. i = i + 1
  249. }
  250. let p = toNumber(s)
  251. if (p < 1) { return 1 }
  252. return p > pages ? pages : p
  253. }
  254. static pagesOf = (count) => {
  255. let p = 1
  256. while (p * pageSize < count) { p = p + 1 }
  257. return p
  258. }
  259. // { page, pages, tiles } of one list
  260. static pageOf = (list, raw) => {
  261. let pages = pagesOf(list.length)
  262. let page = pageNumberOf(raw, pages)
  263. return { page = page pages = pages tiles = tilesOf(list.slice((page - 1) * pageSize, page * pageSize)) }
  264. }
  265. static moviesPage = (raw) => { rollDay() return pageOf(index.movies, raw) }
  266. static seriesPage = (raw) => { rollDay() return pageOf(index.series, raw) }
  267. static catalogCounts = () => { return { movies = index.movies.length series = index.series.length } }
  268. // /genres/<genre> (mission 058): one genre's movies and series, newest first, 24 per page; how many there are
  269. static genrePage = (genreId, raw) => {
  270. rollDay()
  271. let list = index.genres[genreId]
  272. return pageOf(list != null ? list : [], raw)
  273. }
  274. static genreCount = (genreId) => {
  275. let list = index.genres[genreId]
  276. return list != null ? list.length : 0
  277. }

Branches

Latest commits

  • 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
  • cbdc4ea7tracker#12: link icons TMDB/IMDb/TVDB/TVmaze; sync fills missing ids (TVmaze lookup); movies fetched via /movie/mre
  • b105bcd8tracker#11: Hybriel master ff51cf46 (checks no longer vanish), mobile-first styles, carets, follow button, sign-in modal, inverted check, orange castmre
  • 31b758aatracker#10: installable app (manifest, service worker, offline shell), own icon + faviconmre