gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmaind2e6d26bMerge branch 'records-park55' into mainTrackermain/tools/tmdb-fix.hl

10.6 KB

  1. // tools/tmdb-fix.hl — tracker#36 (mission T36): fix the years and the wrong TMDB ids tools/tmdb-check.hl found, as the creator said
  2. // yes on 2026-10-29 (groups 1 and 3 of lib/tmdbfix.hl; 2 and 4 and every non-unique group-3 case are only LISTED).
  3. // TRACKER_STORAGE=/abs/copy/storage/mpackdb TMDB_READ_TOKEN=<never printed> ./bin/hybriel tools/tmdb-fix.hl --dry-run [--plan-out FILE] [--from N] [--limit N]
  4. // TRACKER_STORAGE=/abs/copy/storage/mpackdb TMDB_READ_TOKEN=<never printed> ./bin/hybriel tools/tmdb-fix.hl --apply FILE
  5. // --dry-run asks TMDB for every migrated title (one request each, 260 ms apart, 2 for a group-3 title), prints every planned change
  6. // (`fix-year`, `fix-tmdb`) and every title left for the creator (`list-1`, `list-3`, `list-4`; `leave-2` the translations) and
  7. // CHANGES NOTHING; --plan-out writes the planned changes (space separated: kind, record id, slug, old ('-' = none), new, encoded identity snapshot). --apply reads such a plan,
  8. // asks TMDB ONCE MORE per planned title (the year / the new id must still hold, the record must still be as planned) and writes it;
  9. // a second --apply of the same plan changes nothing (the record no longer is as planned). hl:mpackdb rewrites files when it opens a
  10. // table, so ONLY on a COPY or with the app stopped. Never against the live storage without the creator's go.
  11. import { env, args, exit } from 'hl:proc'
  12. import { now, sleep } from 'hl:time'
  13. import { writeFile, tryReadFile } from 'hl:fs'
  14. import { syncEnabled, msPerRequest, tmdbGet } from '../lib/tmdb.hl'
  15. import { allShows, showById, showsTable } from '../lib/shows.hl'
  16. import { tcWanted, tcAssess } from '../lib/tmdbcheck.hl'
  17. import { tfGroupOf, tfSearch, tfCandText, tfKey, tfWithYear, tfWithTmdb, tfStateOf, tfMatches, tfOtherOwner } from '../lib/tmdbfix.hl'
  18. static tfArgOf = (&tfName) => {
  19. tfAll = args()
  20. let tfI = 0
  21. while (tfI < tfAll.length) {
  22. if (tfAll[tfI] == tfName && tfI + 1 < tfAll.length) { return tfAll[tfI + 1] }
  23. tfI = tfI + 1
  24. }
  25. return null
  26. }
  27. // the title's page path
  28. static tfPathOf = (&tfShow) => { return (tfShow.type == 'movie' ? '/movies/' : '/series/') + tfShow.urlSegment }
  29. static runDry36 = () => {
  30. tfStart = now()
  31. tfFrom = tfArgOf('--from') != null ? toNumber(tfArgOf('--from')) : 0
  32. tfLimit = tfArgOf('--limit') != null ? toNumber(tfArgOf('--limit')) : -1
  33. tfOut = tfArgOf('--plan-out')
  34. tfIds = []
  35. tfTaken = {}
  36. for (tfS of allShows()) {
  37. if (tfS.mergedInto == null && tfS.tmdbId != null) { tfTaken[tfKey(&tfS.type, &tfS.tmdbId)] = tfPathOf(&tfS) }
  38. if (tcWanted(&tfS)) { tfIds.push(tfS.id) }
  39. }
  40. console.log('tmdb fix (dry run): ' + tfIds.length + ' migrated titles with a TMDB id')
  41. let tfQueue = []
  42. let tfPos = 0
  43. for (tfId of tfIds) {
  44. if (tfPos >= tfFrom && (tfLimit < 0 || tfQueue.length < tfLimit)) { tfQueue.push(tfId) }
  45. tfPos = tfPos + 1
  46. }
  47. tfN = { asked = 0 ok = 0 g1 = 0 g1year = 0 g1none = 0 g2 = 0 g3 = 0 g3switch = 0 g3list = 0 g4 = 0 failed = 0 }
  48. let tfPlan = ''
  49. let tfRound = 1
  50. while (tfRound <= 2 && tfQueue.length > 0) {
  51. tfAgain = []
  52. for (tfId of tfQueue) {
  53. tfShow = showById(&tfId)
  54. tfA = tcAssess(&tfShow)
  55. if (tfRound == 1) { tfN.asked = tfN.asked + 1 }
  56. if (tfA.retry && tfRound == 1) { tfAgain.push(tfId) }
  57. else if (tfA.retry) { tfN.failed = tfN.failed + 1 console.log('failed ' + tfA.head + ': ' + tfA.failed) }
  58. else {
  59. tfG = tfGroupOf(&tfA)
  60. if (tfG.group == 0) { tfN.ok = tfN.ok + 1 }
  61. else if (tfG.group < 0) { tfN.failed = tfN.failed + 1 console.log('failed ' + tfA.head + ': ' + tfG.why) }
  62. else if (tfG.group == 1) {
  63. tfN.g1 = tfN.g1 + 1
  64. if (tfA.newYear == null) { tfN.g1none = tfN.g1none + 1 console.log('list-1 ' + tfA.head + ': ' + tfG.why + ', TMDB has no year either, nothing to take') }
  65. else {
  66. tfN.g1year = tfN.g1year + 1
  67. console.log('fix-year ' + tfA.head + ' (' + tfG.why + '): year ' + (tfA.old.year != null ? tfA.old.year : '?') + ' → ' + tfA.newYear)
  68. tfPlan = tfPlan + 'year ' + tfShow.id + ' ' + tfShow.urlSegment + ' ' + (tfA.old.year != null ? tfA.old.year : '-') + ' ' + tfA.newYear + ' ' + tfStateOf(&tfShow) + '
  69. '
  70. }
  71. }
  72. else if (tfG.group == 2) { tfN.g2 = tfN.g2 + 1 console.log('leave-2 ' + tfA.head + ' = "' + tfA.mainName + '" (' + (tfA.newYear != null ? tfA.newYear : '?') + '): ' + tfG.why) }
  73. else if (tfG.group == 4) { tfN.g4 = tfN.g4 + 1 console.log('list-4 ' + tfA.head + ' = "' + tfA.mainName + '" (' + (tfA.newYear != null ? tfA.newYear : '?') + '): ' + tfG.why) }
  74. else {
  75. tfN.g3 = tfN.g3 + 1
  76. sleep(msPerRequest / 1000)
  77. tfF = tfSearch(&tfA)
  78. if (tfF.state == 'unique' && tfTaken[tfKey(&tfShow.type, &tfF.cands[0].id)] != null) {
  79. tfN.g3list = tfN.g3list + 1
  80. console.log('list-3 ' + tfA.head + ': one match ' + tfCandText(&tfF.cands) + ' but that id is already ours at ' + tfTaken[tfKey(&tfShow.type, &tfF.cands[0].id)])
  81. } else if (tfF.state == 'unique') {
  82. tfN.g3switch = tfN.g3switch + 1
  83. console.log('fix-tmdb ' + tfA.head + ': tmdb ' + tfShow.tmdbId + ' → ' + tfF.cands[0].id + ' ("' + tfF.cands[0].name + '" ' + tfF.cands[0].year + ')')
  84. tfPlan = tfPlan + 'tmdb ' + tfShow.id + ' ' + tfShow.urlSegment + ' ' + tfShow.tmdbId + ' ' + tfF.cands[0].id + ' ' + tfStateOf(&tfShow) + '
  85. '
  86. tfTaken[tfKey(&tfShow.type, &tfF.cands[0].id)] = tfPathOf(&tfShow)
  87. } else {
  88. tfN.g3list = tfN.g3list + 1
  89. console.log('list-3 ' + tfA.head + ': ' + (tfF.state == 'noyear' ? 'we have no year to search with' : (tfF.state == 'failed' ? 'search failed: ' + tfF.failed : (tfF.state == 'none' ? 'no TMDB result with this name and year' : 'several TMDB results with this name and year'))) + '. candidates: ' + tfCandText(&tfF.cands))
  90. }
  91. }
  92. }
  93. if (tfRound == 1 && tfN.asked % 500 == 0) { console.log('tmdb fix: ' + tfN.asked + '/' + tfQueue.length + ' asked') }
  94. sleep(msPerRequest / 1000)
  95. }
  96. tfQueue = tfAgain
  97. tfRound = tfRound + 1
  98. }
  99. if (tfOut != null) { writeFile(tfOut, tfPlan, 420) }
  100. console.log('tmdb fix (dry run) done: asked=' + tfN.asked + ' ok=' + tfN.ok + ' group1=' + tfN.g1 + ' (year fixes=' + tfN.g1year + ' nothing-to-take=' + tfN.g1none + ') group2=' + tfN.g2
  101. + ' group3=' + tfN.g3 + ' (switch=' + tfN.g3switch + ' listed=' + tfN.g3list + ') group4=' + tfN.g4 + ' failed=' + tfN.failed + ' seconds=' + ((now() - tfStart) / 1000) + ' — NOTHING was changed')
  102. return 0
  103. }
  104. static runApply36 = () => {
  105. tfPlanFile = tfArgOf('--apply')
  106. tfText = tryReadFile(tfPlanFile)
  107. if (tfText == null) { console.log('REFUSED: cannot read the plan file ' + tfPlanFile) return 2 }
  108. tfN = { planned = 0 applied = 0 year = 0 tmdb = 0 skipped = 0 failed = 0 }
  109. let tfTouched = false
  110. for (tfLine of tfText.split('
  111. ')) {
  112. if (tfLine.trim() != '') {
  113. tfCols = tfLine.split(' ')
  114. tfN.planned = tfN.planned + 1
  115. tfShow = showById(&tfCols[1])
  116. tfWhat = tfCols[0] + ' ' + tfCols[2]
  117. if (tfShow == null || tfShow.mergedInto != null) { tfN.skipped = tfN.skipped + 1 console.log('skip ' + tfWhat + ': record gone or merged') }
  118. else if (tfCols.length != 6 || tfShow.urlSegment != tfCols[2] || tfStateOf(&tfShow) != tfCols[5]) { tfN.skipped = tfN.skipped + 1 console.log('skip ' + tfWhat + ': record preconditions changed or missing') }
  119. else if (tfCols[0] == 'year') {
  120. tfWasYear = tfCols[3] == '-' ? null : toNumber(tfCols[3])
  121. tfNewYear = toNumber(tfCols[4])
  122. if (tfShow.year == tfNewYear && (tfShow.release == null || ('' + tfShow.release).slice(0, 4) == '' + tfNewYear)) { tfN.skipped = tfN.skipped + 1 console.log('skip ' + tfWhat + ': already ' + tfNewYear) }
  123. else if ((tfShow.year != null ? tfShow.year : (tfShow.release != null ? toNumber(('' + tfShow.release).slice(0, 4)) : null)) != tfWasYear) { tfN.skipped = tfN.skipped + 1 console.log('skip ' + tfWhat + ': old year changed') }
  124. else {
  125. tfA = tcAssess(&tfShow)
  126. sleep(msPerRequest / 1000)
  127. if (tfA.failed != null) { tfN.skipped = tfN.skipped + 1 console.log('skip ' + tfWhat + ': ' + tfA.failed) }
  128. else if (tfA.newYear != tfNewYear || tfGroupOf(&tfA).group != 1) { tfN.skipped = tfN.skipped + 1 console.log('skip ' + tfWhat + ': TMDB now says ' + tfA.newYear) }
  129. else {
  130. let tfPut = showsTable.update(tfShow.id, tfWithYear(&tfShow, &tfNewYear, &tfA.newDate))
  131. if (tfPut == null) { tfN.failed = tfN.failed + 1 console.log('failed ' + tfWhat + ': not stored: ' + showsTable.lastError()) }
  132. else { tfN.applied = tfN.applied + 1 tfN.year = tfN.year + 1 tfTouched = true console.log('applied ' + tfWhat + ': year ' + (tfWasYear != null ? tfWasYear : '?') + ' → ' + tfNewYear) }
  133. }
  134. }
  135. } else if (tfCols[0] == 'tmdb') {
  136. tfOldId = toNumber(tfCols[3])
  137. tfNewId = toNumber(tfCols[4])
  138. if (tfShow.tmdbId == tfNewId) { tfN.skipped = tfN.skipped + 1 console.log('skip ' + tfWhat + ': already ' + tfNewId) }
  139. else if (tfShow.tmdbId != tfOldId) { tfN.skipped = tfN.skipped + 1 console.log('skip ' + tfWhat + ': the record has tmdb ' + tfShow.tmdbId + ' now, not ' + tfOldId) }
  140. else if (tfOtherOwner(&tfShow, &tfNewId) != null) { tfN.skipped = tfN.skipped + 1 console.log('skip ' + tfWhat + ': target id already owned by ' + tfOtherOwner(&tfShow, &tfNewId)) }
  141. else {
  142. tfMovie = tfShow.type == 'movie'
  143. tfD = tmdbGet((tfMovie ? '/movie/' : '/tv/') + tfNewId)
  144. sleep(msPerRequest / 1000)
  145. if (tfD.failed != null) { tfN.skipped = tfN.skipped + 1 console.log('skip ' + tfWhat + ': ' + tfD.failed) }
  146. else if (!tfMatches(&tfShow, &tfD, &tfNewId)) { tfN.skipped = tfN.skipped + 1 console.log('skip ' + tfWhat + ': fresh TMDB name or year no longer matches') }
  147. else {
  148. let tfPut = showsTable.update(tfShow.id, tfWithTmdb(&tfShow, &tfNewId))
  149. if (tfPut == null) { tfN.failed = tfN.failed + 1 console.log('failed ' + tfWhat + ': not stored: ' + showsTable.lastError()) }
  150. else { tfN.applied = tfN.applied + 1 tfN.tmdb = tfN.tmdb + 1 tfTouched = true console.log('applied ' + tfWhat + ': tmdb ' + tfOldId + ' → ' + tfNewId) }
  151. }
  152. }
  153. }
  154. }
  155. }
  156. if (tfTouched) { showsTable.persist() }
  157. console.log('tmdb fix (apply) done: planned=' + tfN.planned + ' applied=' + tfN.applied + ' (year=' + tfN.year + ' tmdb=' + tfN.tmdb + ') skipped=' + tfN.skipped + ' failed=' + tfN.failed)
  158. return 0
  159. }
  160. static runFix36 = () => {
  161. tfStorage = env('TRACKER_STORAGE')
  162. if (tfStorage == null || !tfStorage.startsWith('/')) { console.log('REFUSED: set TRACKER_STORAGE, absolute (a copy, or the app stopped)') return 2 }
  163. if (!syncEnabled) { console.log('REFUSED: TMDB_READ_TOKEN is not set') return 2 }
  164. tfA = args()
  165. if (tfA.includes('--dry-run')) { return runDry36() }
  166. if (tfA.includes('--apply') && tfArgOf('--apply') != null) { return runApply36() }
  167. console.log('REFUSED: say --dry-run [--plan-out FILE] or --apply FILE')
  168. return 2
  169. }
  170. exit(runFix36())
  171. on Error(e) { console.log(' error absorbed: ' + e.message) }

Branches

Latest commits

  • d2e6d26bMerge branch 'records-park55' into mainTracker
  • f567f025|||PR records: park #55, no Claude workers ruleTracker
  • c21aff6bMerge branch 'records-hm55c' into mainTracker
  • 9ba258d3|||PR records: HM55b resultTracker
  • 64f2d300Merge branch 'records-hm55b2' into mainTracker
  • ae3b80dc|||PR records: HM55b briefTracker
  • 7ef3cb3bMerge branch 'records-st55' into mainTracker
  • fec2e080|||PR records: STATUS HM55Tracker
  • 8e174918Merge branch 'records-hm55b' into mainTracker
  • b1ecee57|||PR records: HM55 stateTracker
  • 28eab08cMerge branch 'records-hm55' into mainTracker
  • d3edaa05|||PR records: HM55 start, PROCEDURES.mdTracker
  • 3059f86cMerge branch 'records-design-rule' into mainTracker
  • 421a4809|||PR records: his-design rule (Apollo 2026-11-13)Tracker
  • d1390692Merge branch 'records-dp75b' into mainTracker
  • a4d1a7d2|||PR records: later steps checked, branch auditTracker
  • e070e16fMerge branch 'records-dp75' into mainTracker
  • 642b4fa8|||PR records: DP75 day62 live, AP62 briefTracker
  • bfc4ea4dMerge branch 'ap62' into mainTracker
  • 2c95ac46|||PR AP62: re-vendor Hybriel set day62 (source 70341ad2, bin a54f5f08)Tracker