tracker
All repositories: gitoria
7.3 KB
// lib/sync-helpers.hl — the small pieces of the sync (sync.hl) that need no table and no request: placeholder titles, day// numbers, TVmaze's HTML stripped, whether TVmaze numbers a show like we do, which seasons to ask, batches, changed texts,// migrated rows, and the totals of a run (mission 028: out of tmdbsync.hl).import { textOr } from './util.hl'// ---- TVmaze merge (tracker.worldapi.org#15) ------------------------------------------------------// a title that is none yet: empty, "TBA"/"TBD", or "Episode <n>" (what TMDB and TVmaze name an untitled episode; also// French/German/Spanish: real data has TVmaze "Épisode 1" where TMDB says "Episode 1")static placeholderWords = ['episode', 'épisode', 'Épisode', 'folge', 'episodio']static isPlaceholderTitle = (&v) => {t = textOr(&v)if (t == null) { return true }l = t.trim().toLowerCase()if (l == 'tba' || l == 'tbd' || l == 'tba.' || l == 'to be announced') { return true }parts = l.split(' ')if (parts.length != 2 || !placeholderWords.includes(parts[0]) || parts[1].length == 0) { return false }let i = 0while (i < parts[1].length) {if (!'0123456789'.includes(parts[1][i])) { return false }i = i + 1}return true}// 'YYYY-MM-DD' → a day number (only differences matter), null when it is no such datestatic dayNumber = (&v) => {t = textOr(&v)if (t == null || t.length < 10) { return null }y = toNumber(t.slice(0, 4))m = toNumber(t.slice(5, 7))d = toNumber(t.slice(8, 10))if (y == null || m == null || d == null || m < 1 || m > 12) { return null }before = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]py = m <= 2 ? y - 1 : yleaps = (py - py % 4) / 4 - (py - py % 100) / 100 + (py - py % 400) / 400return y * 365 + leaps + before[m - 1] + d}static daysApart = (&a, &b) => {x = dayNumber(&a)y = dayNumber(&b)if (x == null || y == null) { return null }return x > y ? x - y : y - x}// TVmaze's text without its HTML (tracker#18: `tvmazeSummary`): tags dropped — a block tag (p, br, div, li) becomes a space —// the common entities decoded, white space collapsed; null when nothing is leftstatic blockTags = ['p', 'br', 'div', 'li', 'ul', 'ol', 'h1', 'h2', 'h3', 'h4', 'blockquote']static entities = [[' ', ' '], ['"', '"'], [''', "'"], [''', "'"], ['<', '<'], ['>', '>'], ['–', '–'], ['—', '—'], ['…', '…'], ['&', '&']]static stripHtml = (&v) => {t = textOr(&v)if (t == null) { return null }let out = ''let i = 0while (i < t.length) {if (t[i] == '<') {let close = t.indexOf('>', i)if (close < 0) { close = t.length }let name = t.slice(i + 1, close).trim().toLowerCase()if (name.startsWith('/')) { name = name.slice(1) }let sp = name.indexOf(' ')if (sp >= 0) { name = name.slice(0, sp) }if (name.endsWith('/')) { name = name.slice(0, name.length - 1) }if (blockTags.includes(name)) { out = out + ' ' }i = close + 1} else {// a line break / tab (a control character) is a space too (strings have no escapes to name them)out = out + (t.charCodeAt(i) < 32 ? ' ' : t[i])i = i + 1}}for (e of entities) { out = out.replaceAll(e[0], e[1]) }words = []for (w of out.split(' ')) { if (w != '') { words.push(w) } }return words.length > 0 ? words.join(' ') : null}// do we and TVmaze number a show the same way? Of the episodes both know (same S/E), at least 80 % must air within a day// of each other OR carry the same title (a streaming show TVmaze dates all on its release day, TMDB week by week, still// agrees by title). A show we have episodes of but TVmaze numbers none of the same (e.g. seasons by year) never agrees.// Answers { known, compared, agree, ok }.static sameTitle = (&a, &b) => { return !isPlaceholderTitle(&a) && !isPlaceholderTitle(&b) && a.trim().toLowerCase() == b.trim().toLowerCase() }static numberingAgrees = (&epByNumber, &list) => {out = { known = 0 compared = 0 agree = 0 ok = true }localCount = epByNumber.keys().lengthfor (te of list) {let e = epByNumber[te.season + 'x' + te.number]if (e != null) {out.known = out.known + 1let apart = daysApart(&e.release, &te.airdate)let titled = !isPlaceholderTitle(&e.title) && !isPlaceholderTitle(&te.name)if (apart != null || titled) {out.compared = out.compared + 1if ((apart != null && apart <= 1) || sameTitle(&e.title, &te.name)) { out.agree = out.agree + 1 }}}}out.ok = !((localCount > 0 && out.known == 0) || (out.compared > 0 && out.agree * 5 < out.compared * 4))return out}// which TMDB seasons to sync: every numbered one with episodes; 0 only when the show has a season 0 alreadystatic wantedSeasonNumbers = (&details, &localByNumber) => {out = []if (details.seasons == null) { return out }for (s of details.seasons) {let n = s.season_numberif (n != null && (n > 0 || localByNumber['' + n] != null) && (s.episode_count == null || s.episode_count > 0 || localByNumber['' + n] != null)) { out.push(n) }}return out}// the seasons in batches of 20 (TMDB's append_to_response limit)static batchesOf = (&list, &size) => {out = []let cur = []for (x of list) {cur.push(x)if (cur.length == size) { out.push(cur) cur = [] }}if (cur.length > 0) { out.push(cur) }return out}// fields of an episode/season from TMDB that differ from the stored record (null/empty never overwrites)static changedText = (stored, &fresh) => { return fresh != null && fresh != stored }// mission 070 (conductor: no step destroys migrated data): a season / episode the old tracker's migration brought (its `oldId`// is neither `tmdb-…` nor `tvmaze-…`) keeps its FIRST title / summary in `migratedTitle` / `migratedSummary` before TMDB's text// replaces it (set once; shown nowhere) — the daily delta run now also refreshes seasons of titles nobody followsstatic isMigratedRow = (&r) => { return r.oldId != null && ('' + r.oldId).slice(0, 5) != 'tmdb-' && ('' + r.oldId).slice(0, 7) != 'tvmaze-' }// totals of a run: the per-show counts added upstatic emptyTotals = () => { return { shows = 0 requests = 0 tvmazeRequests = 0 newSeasons = 0 newEpisodes = 0 updatedEpisodes = 0 updatedSeasons = 0 posters = 0 ids = 0 tvmazeEpisodes = 0 tvmazeSeasons = 0 tvmazeFilled = 0 tvmazeSkipped = 0 errors = 0 } }static addTotals = (t, &r) => {o = to.shows = o.shows + 1o.requests = o.requests + r.requestso.tvmazeRequests = o.tvmazeRequests + r.tvmazeRequestso.ids = o.ids + r.idso.newSeasons = o.newSeasons + r.newSeasonso.newEpisodes = o.newEpisodes + r.newEpisodeso.updatedEpisodes = o.updatedEpisodes + r.updatedEpisodeso.updatedSeasons = o.updatedSeasons + r.updatedSeasonso.posters = o.posters + r.posterso.tvmazeEpisodes = o.tvmazeEpisodes + r.tvmazeEpisodeso.tvmazeSeasons = o.tvmazeSeasons + r.tvmazeSeasonso.tvmazeFilled = o.tvmazeFilled + r.tvmazeFilledo.tvmazeSkipped = o.tvmazeSkipped + r.tvmazeSkippedo.errors = o.errors + r.errorsreturn o}static totalsLine = (&t) => {return 'shows=' + t.shows + ' newSeasons=' + t.newSeasons + ' newEpisodes=' + t.newEpisodes + ' updatedEpisodes=' + t.updatedEpisodes+ ' updatedSeasons=' + t.updatedSeasons + ' posters=' + t.posters + ' ids=' + t.ids + ' requests=' + t.requests + ' tvmazeRequests=' + t.tvmazeRequests + ' errors=' + t.errors+ ' tvmazeEpisodes=' + t.tvmazeEpisodes + ' tvmazeSeasons=' + t.tvmazeSeasons + ' tvmazeFilled=' + t.tvmazeFilled + ' tvmazeSkipped=' + t.tvmazeSkipped}
Branches
Latest commits
- aac9a98dlog: evening re-measure startmre
- 45163bd4log: person/tmdb error hypothesismre
- cb38de2blog: swap 503 causemre
- fd10c3d5log: CB livemre
- 7557c025log: CB deploy try 2mre
- 06a7534aMerge branch 'cb'mre
- 2f8030d6log: CB gates greenmre
- 498bf644log: CB gates startmre
- f59c2847gc1: bound the five request caches (shows.hl cacheLimit)mre
- 6ea2c6d6log: morning re-measure 11-04mre
- 446290f0log: GC1 livemre
- 8e6aa91dlog: GC1 deploy retrymre
- 87ad2dc1log: GC1 deployingmre
- 53f10fb8Merge branch 'gc1-deploy'mre
- 7fcbf1d2log: GC1 deploy GOmre
- 1fb6664bcompose: TRACKER_QSTATS=1 (GC1 measurement)mre
- 6576c1cdgc1: release the people-view scan lists once the view is builtmre
- 94f8a373gc1: back out the kept episode-id set (no measurable gain on /my/unwatched: 4.69 -> 4.61 GB per 150 loads)mre
- a1a88bd4gc1: search skips the stale-key string while no entry is stale; /my/unwatched asks a kept id set instead of building a key string per episode rowmre
- cd830744gc1: dbSearch: no split / map / list copy per candidate (scores from the stored text, top list touched only when the hit can enter)mre