gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchday7233072bb5|||PR Record day72 gates and Chrome proofmreday72/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

  • 33072bb5|||PR Record day72 gates and Chrome proofmre
  • 95f2cd73|||PR Use typed parent methods for screen playback and local server facesmre
  • 6422a020Sync media-player at 51c3818mre
  • b582ba1cVendor complete Hybriel day72 plugin setmre
  • 02dccbc9Merge branch 'records-1119' into mainJukebox
  • a39e1c23|||PR Records: missions 013-015, reconnect fix livemre
  • fc7db075Merge branch 'reconnect-retry' into mainJukebox
  • ac6f4c6fL2: plain declaration for a in pickedNextmre
  • 2f335086Review round 2: only an explicit queue pick drops pend (not the automatic advance); jb23 records every source changemre
  • 3eb6695fReview fixes: action generation, queue pick clears pend, Previous/Next retry the decided request, typed handlersmre
  • 7a53c0c2|||PR A tap lost to a dead socket plays after reconnect (mission 015)mre
  • 1f0cd77bMerge branch 'records-day41' into mainJukebox
  • 67be229f|||PR Records: day41 live (PR #4), mission 012, statusmre
  • 0bb027daconductor: caramboleyo is the user, not owner/creatorApollo
  • 04f5d9d5Merge branch 'day41' into mainJukebox
  • 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