gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit8ed6da458ed6da45|||PR README: media-player pin df9e7dfmre8ed6da45/lib/catalog.hl

4.7 KB

  1. // lib/catalog.hl — THE DERIVED TABLES (albums, artists): built from the songs table after a scan that changed anything.
  2. // An album is one albumKey (album artist + album name without the CD marker; not the folder); a song with copies counts once; an artist is one artistKey. Both get a stable primary
  3. // key (a digest of the key), so an id from the API stays valid across rebuilds. All writes to those two tables are here.
  4. import { textDigest } from 'hl:fs'
  5. import { songsTable, albumsTable, artistsTable, countOfRows } from './db.hl'
  6. import { fold, safeKey, SEP } from './util.hl'
  7. import { albumKeyOf, sortKeys } from './albums.hl'
  8. import { showOf } from './dups.hl'
  9. import { dropOrderCache } from './state.hl'
  10. static variousName = 'Various Artists'
  11. // an album name that says nothing ("Unknown album", "Unbekannt", "Unbekannter Titel"): songs with it are never taken for one compilation
  12. static unknownAlbum = (name) => {
  13. f = fold(name)
  14. return f == '' || f.startsWith('unknown') || f.startsWith('unbekannt')
  15. }
  16. // A COMPILATION has no album artist tag, so each of its songs would be an album of its own. When three or more different artists share an album name in one folder
  17. // (and none of them has a real album artist), those songs get the album artist "Various Artists" and one album key. Their artist key stays their own, so an artist's page
  18. // still lists the songs. Idempotent; only songs whose keys change are written. Answers how many were changed.
  19. static fixCompilations = () => {
  20. groups = {}
  21. for (s of songsTable.iterate(null, null)) {
  22. if (unknownAlbum(s.album)) { continue }
  23. if (s.albumArtist != s.artist && s.albumArtist != variousName) { continue }
  24. k = fold(s.album) + SEP + s.folder
  25. let g = groups[k]
  26. if (g == null) { g = {} }
  27. g[fold(s.artist)] = true
  28. groups[k] = g
  29. }
  30. let changed = 0
  31. rows = []
  32. for (s of songsTable.iterate(null, null)) { rows.push(s) }
  33. for (s of rows) {
  34. if (unknownAlbum(s.album)) { continue }
  35. if (s.albumArtist != s.artist && s.albumArtist != variousName) { continue }
  36. g = groups[fold(s.album) + SEP + s.folder]
  37. let many = 0
  38. for (a in g) { many = many + 1 }
  39. want = many >= 3 ? variousName : s.artist
  40. if (want == s.albumArtist) { continue }
  41. keys = sortKeys(want, s.album, s.disc, s.track == null ? 0 : s.track, s.title)
  42. rec = {}
  43. for (f in s) {
  44. if (f != 'id') { rec[f] = s[f] }
  45. }
  46. rec.albumArtist = want
  47. rec.albumKey = albumKeyOf(want, s.album)
  48. rec.sortArtist = keys.sortArtist
  49. rec.sortAlbum = keys.sortAlbum
  50. songsTable.update(s.id, rec)
  51. changed = changed + 1
  52. }
  53. return changed
  54. }
  55. static idOf = (key) => { return textDigest(key).slice(0, 16) }
  56. // Rebuild both tables from scratch (one pass over the songs, then the old rows go and the new come). Answers
  57. // { albums, artists } counts.
  58. static rebuildCatalog = () => {
  59. fixCompilations()
  60. albums = {}
  61. artists = {}
  62. dropOrderCache()
  63. for (s of songsTable.iterate(null, null)) {
  64. w = showOf(s.id, s.playCount)
  65. if (!w.shown) { continue }
  66. let a = albums[s.albumKey]
  67. if (a == null) {
  68. a = { id = idOf(s.albumKey) key = s.albumKey name = s.album albumArtist = s.albumArtist artistKey = s.albumArtist == variousName ? safeKey(fold(variousName)) : s.artistKey folder = s.folder
  69. year = s.year songCount = 0 duration = 0 discs = 1 playCount = 0 coverId = null sortName = safeKey(fold(s.album) + '|' + s.artistKey) }
  70. }
  71. a.songCount = a.songCount + 1
  72. if (s.disc != null && s.disc > a.discs) { a.discs = s.disc }
  73. a.duration = a.duration + (s.duration == null ? 0 : s.duration)
  74. a.playCount = a.playCount + w.plays
  75. if (a.coverId == null && s.coverId != null) { a.coverId = s.coverId }
  76. if (a.year == null && s.year != null) { a.year = s.year }
  77. albums[s.albumKey] = a
  78. let r = artists[s.artistKey]
  79. if (r == null) {
  80. r = { id = idOf('artist:' + s.artistKey) key = s.artistKey name = s.albumArtist == variousName ? s.artist : s.albumArtist songCount = 0 albumCount = 0 playCount = 0 coverId = null
  81. sortName = s.artistKey seenAlbums = {} }
  82. }
  83. r.songCount = r.songCount + 1
  84. r.playCount = r.playCount + w.plays
  85. if (r.seenAlbums[s.albumKey] == null) {
  86. r.seenAlbums[s.albumKey] = true
  87. r.albumCount = r.albumCount + 1
  88. }
  89. if (r.coverId == null && s.coverId != null) { r.coverId = s.coverId }
  90. artists[s.artistKey] = r
  91. }
  92. // the old rows out
  93. for (old of albumsTable.find(null, null)) { albumsTable.delete(old.id) }
  94. for (old of artistsTable.find(null, null)) { artistsTable.delete(old.id) }
  95. let na = 0
  96. for (k in albums) {
  97. albumsTable.put(albums[k])
  98. na = na + 1
  99. }
  100. let nr = 0
  101. for (k in artists) {
  102. r = artists[k]
  103. artistsTable.put({ id = r.id key = r.key name = r.name songCount = r.songCount albumCount = r.albumCount playCount = r.playCount coverId = r.coverId sortName = r.sortName })
  104. nr = nr + 1
  105. }
  106. return { albums = na artists = nr }
  107. }

Branches

Latest commits

  • 8ed6da45|||PR README: media-player pin df9e7dfmre
  • d5d45fc3|||PR media-player df9e7df (repeat-one fix), sync pin and SOURCEmre
  • 89cdabb1Vendor Hybriel day41 (57054d9e): plugin dirs, set .somre
  • 1fecd6a7Merge branch 'records-day39' into mainJukebox
  • d6b3e068|||PR Records: day39 live (PR #2), mission 011 PR step, statusmre
  • 5fa19e89Merge branch 'day39' into mainJukebox
  • cfe95440|||PR day39 re-vendor: Hybriel 51e921d7 (bin 6b481aa7), ids kebab-case since hybriel#256: quoted state rule and tests use jb-shuffle / jb-repeatmre
  • f3614c9bRe-vendor to Hybriel day39 (51e921d7, bin 6b481aa7)mre
  • 9f5c6532Merge branch 'status-22-pr' into mainJukebox
  • 5838d6c7|||PR STATUS: #22 pending decision, PR-only rulemre
  • 29d2029cRecords: PR-only rule (Kunibert)mre
  • adacb0abRecords: neutral ticket rule, #22 decision recordedmre
  • b2d6f121Records: #308 hold liftedmre
  • 7ab67533Records: day39 on hold (hybriel#308)mre
  • 22f21739Records: note rebase before mergemre
  • f7368ea3Records: mission 011 day39, waits for lisbethmre
  • 9515d2cdRecords: jb20 livemre
  • 019f6601ui gate: comment names the open bug components#27 (workaround, not fixed)mre
  • 277a2722README: provenance linemre
  • 4169ca6aREADME: sync pinmre