gitoriaLog in with ident

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit2b7fdd6c2b7fdd6cmission 056: signed-out header one row on phones ("Log in", nowrap), backfill skips adult titles' posters, gate checksmre2b7fdd6c/catalog.hl

8.7 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. import { now, timestamp } from 'hl:time'
  22. import { allShows, allEpisodes, showById, seasonsOfShow, episodesOfSeason, showYear, posterUrlOf, isPublicTitle } from './shows.hl'
  23. static pageSize = 24
  24. static todayStr = () => { return timestamp(now()).slice(0, 10) }
  25. static dateOr = (v) => { return v != null && hlTypeName(v) == 'String' && v.length >= 4 ? v : '' }
  26. static seriesKey = (latest, release) => { return latest != null && latest != '' ? '1' + latest : '0' + release }
  27. // ---- sorting, hand-written (no list sort(), hybriel#1): a bottom-up merge sort, newest key first, stable ----------
  28. static mergeDesc = (a, b) => {
  29. let out = []
  30. let i = 0
  31. let j = 0
  32. while (i < a.length && j < b.length) {
  33. if (b[j].key > a[i].key) { out.push(b[j]) j = j + 1 } else { out.push(a[i]) i = i + 1 }
  34. }
  35. while (i < a.length) { out.push(a[i]) i = i + 1 }
  36. while (j < b.length) { out.push(b[j]) j = j + 1 }
  37. return out
  38. }
  39. static sortDesc = (list) => {
  40. if (list.length == 0) { return [] }
  41. let runs = []
  42. for (x of list) { runs.push([x]) }
  43. while (runs.length > 1) {
  44. let next = []
  45. let k = 0
  46. while (k + 1 < runs.length) { next.push(mergeDesc(runs[k], runs[k + 1])) k = k + 2 }
  47. if (k < runs.length) { next.push(runs[k]) }
  48. runs = next
  49. }
  50. return runs[0]
  51. }
  52. // one entry moved to its place: out of the list, then in before the first entry with a smaller key (linear)
  53. static placed = (list, entry) => {
  54. let out = []
  55. let done = false
  56. for (x of list) {
  57. if (x.id != entry.id) {
  58. if (!done && entry.key > x.key) { out.push(entry) done = true }
  59. out.push(x)
  60. }
  61. }
  62. if (!done) { out.push(entry) }
  63. return out
  64. }
  65. // one entry out of the list (a title that is not public, mission 054)
  66. static without = (list, id) => {
  67. let out = []
  68. for (x of list) { if (x.id != id) { out.push(x) } }
  69. return out
  70. }
  71. // ---- the index -------------------------------------------------------------------------------------------------
  72. static buildIndex = () => {
  73. let today = todayStr()
  74. let movies = []
  75. let seriesIds = []
  76. let release = {}
  77. for (s of allShows()) {
  78. if (s.type == 'movie') { if (isPublicTitle(s)) { movies.push({ id = s.id key = dateOr(s.release) }) } }
  79. else if (s.type == 'series') {
  80. if (isPublicTitle(s)) { seriesIds.push(s.id) }
  81. release[s.id] = dateOr(s.release)
  82. }
  83. }
  84. let latest = {}
  85. let upcoming = {}
  86. for (e of allEpisodes()) {
  87. if (e.show != null && dateOr(e.release) != '') {
  88. if (e.release <= today) {
  89. let l = latest[e.show]
  90. if (l == null || e.release > l) { latest[e.show] = e.release }
  91. } else {
  92. let u = upcoming[e.show]
  93. if (u == null) { u = [] }
  94. u.push(e.release)
  95. upcoming[e.show] = u
  96. }
  97. }
  98. }
  99. let series = []
  100. for (id of seriesIds) { series.push({ id = id key = seriesKey(latest[id], release[id]) }) }
  101. return { day = today movies = sortDesc(movies) series = sortDesc(series) latest = latest upcoming = upcoming release = release }
  102. }
  103. static index = buildIndex()
  104. // a new day: upcoming episodes that are out now become their series' newest released one
  105. static rollDay = () => {
  106. let today = todayStr()
  107. if (index.day == today) { return null }
  108. let moved = false
  109. for (id of index.upcoming.keys()) {
  110. let rest = []
  111. let l = index.latest[id]
  112. for (d of index.upcoming[id]) {
  113. if (d <= today) {
  114. if (l == null || d > l) { l = d }
  115. moved = true
  116. } else { rest.push(d) }
  117. }
  118. index.upcoming[id] = rest
  119. if (l != null) { index.latest[id] = l }
  120. }
  121. if (moved) {
  122. let series = []
  123. for (x of index.series) { series.push({ id = x.id key = seriesKey(index.latest[x.id], index.release[x.id]) }) }
  124. index.series = sortDesc(series)
  125. }
  126. index.day = today
  127. return null
  128. }
  129. // after the sync (or the adult backfill) touched a show: its entry from its own data again (only that show's
  130. // seasons/episodes are read) — or out of the list when it is not public (adult or unknown, mission 054)
  131. static refreshCatalogShow = (showId) => {
  132. rollDay()
  133. let show = showById(showId)
  134. if (show == null) { return null }
  135. let shown = isPublicTitle(show)
  136. if (show.type == 'movie') {
  137. index.movies = shown ? placed(index.movies, { id = show.id key = dateOr(show.release) }) : without(index.movies, show.id)
  138. return null
  139. }
  140. if (show.type != 'series') { return null }
  141. let today = index.day
  142. let l = null
  143. let up = []
  144. for (s of seasonsOfShow(show)) {
  145. for (e of episodesOfSeason(s)) {
  146. if (dateOr(e.release) != '') {
  147. if (e.release <= today) { if (l == null || e.release > l) { l = e.release } } else { up.push(e.release) }
  148. }
  149. }
  150. }
  151. index.latest[show.id] = l
  152. index.upcoming[show.id] = up
  153. index.release[show.id] = dateOr(show.release)
  154. index.series = shown ? placed(index.series, { id = show.id key = seriesKey(l, index.release[show.id]) }) : without(index.series, show.id)
  155. return null
  156. }
  157. // ---- what the pages read ---------------------------------------------------------------------------------------
  158. // one poster tile: poster, title, year, the show page (movies use the same /shows/<slug> page and its Follow button)
  159. static tileOf = (show) => {
  160. let y = showYear(show)
  161. return { id = show.id title = show.title year = y != null ? y : '' href = '/shows/' + show.urlSegment posterUrl = posterUrlOf(show) }
  162. }
  163. static tilesOf = (entries) => {
  164. let out = []
  165. for (x of entries) {
  166. let show = showById(x.id)
  167. if (show != null) { out.push(tileOf(show)) }
  168. }
  169. return out
  170. }
  171. // the homepage: the n latest RELEASED movies (none dated after today) and the n series with the newest released episode
  172. static latestMovies = (n) => {
  173. rollDay()
  174. let want = []
  175. for (x of index.movies) {
  176. if (want.length < n && x.key != '' && x.key <= index.day) { want.push(x) }
  177. if (want.length >= n) { return tilesOf(want) }
  178. }
  179. return tilesOf(want)
  180. }
  181. static latestSeries = (n) => {
  182. rollDay()
  183. let want = []
  184. for (x of index.series) {
  185. if (want.length < n && x.key.slice(0, 1) == '1') { want.push(x) }
  186. if (want.length >= n) { return tilesOf(want) }
  187. }
  188. return tilesOf(want)
  189. }
  190. // a page number from the address (`/movies/page/:page`, a String) — anything that is not a whole number ≥ 1 is page 1;
  191. // past the last page is the last page
  192. static pageNumberOf = (raw, pages) => {
  193. if (raw == null) { return 1 }
  194. let s = '' + raw
  195. if (s == '' || s.length > 6) { return 1 }
  196. let i = 0
  197. while (i < s.length) {
  198. let c = s.charCodeAt(i)
  199. if (c < 48 || c > 57) { return 1 }
  200. i = i + 1
  201. }
  202. let p = toNumber(s)
  203. if (p < 1) { return 1 }
  204. return p > pages ? pages : p
  205. }
  206. static pagesOf = (count) => {
  207. let p = 1
  208. while (p * pageSize < count) { p = p + 1 }
  209. return p
  210. }
  211. // { page, pages, tiles } of one list
  212. static pageOf = (list, raw) => {
  213. let pages = pagesOf(list.length)
  214. let page = pageNumberOf(raw, pages)
  215. return { page = page pages = pages tiles = tilesOf(list.slice((page - 1) * pageSize, page * pageSize)) }
  216. }
  217. static moviesPage = (raw) => { rollDay() return pageOf(index.movies, raw) }
  218. static seriesPage = (raw) => { rollDay() return pageOf(index.series, raw) }
  219. static catalogCounts = () => { return { movies = index.movies.length series = index.series.length } }

Branches

Latest commits

  • 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
  • 2fa9d997tracker#9: TMDB sync (followed shows: seasons, episodes, posters), tools/sync-tmdb.hl + daily run 04:00 UTC, fake TMDB in gatemre
  • 49e1f61edeploy.sh: back up live storage/.sessions/.env before every deploy (newest 5 kept)mre
  • 54070a4etracker#8: /my/unwatched + /my/schedule (301 from old), S01E01, title (year), 1 episode, watched-set lookup (unwatched 15s -> 1s)mre
  • 3251488atracker#7: /my/shows (followed shows, newest follow first, poster, title, last watched SxxEyy); gate can take screenshots (TRACKER_GATE_SHOTS)mre