gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit227c9b96227c9b96Records: jb19 livemre227c9b96/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

  • 227c9b96Records: jb19 livemre
  • 9f365376README: the bar's controls (jb19)mre
  • d18851fdjb19: pressed switches look different (whole selector), queue column cannot widen past the panel, gates follow the new bar (jb3, jb11, jb15)mre
  • 82b54c0ejb19: bar Shuffle · Previous · Play · Next · Repeat, no 10 s buttons; Shuffle switch (shuffle from the playing song, back to the list), Repeat off/all/one, both remembered in ui.json; Shuffle my favourites an ordinary button; gate tests/jb19.mjsmre
  • ba354710jb19: icons pinned to components.hybriel 46fefaa (shuffle, repeat, repeatOne, prevTrack, nextTrack)mre
  • d8e8d059jb19 step 2: media-player from components.hybriel c96709c (skip, repeat options; the end of a song only calls onPlay), sync-components.sh and SOURCE point to it, jb11 counts media loads and metadata writes (from jb21 d3a026e)mre
  • 7b048c1bRecords: jb19 done, L2 live stepmre
  • 665185aeMission 009: jb19 livemre
  • 47b2d3beRecords: P1 resumedmre
  • 8e326e94Records: resetKey blockedmre
  • f72dd7edRecords: icons comingmre
  • b264e5deRecords: P1 icons stopmre
  • 514d7cbeRecords: mission 008 startedmre
  • 61c40127Mission 008: #19 + #21mre
  • 6dcaeb46Records: resetKey planmre
  • c5dbf2ecRecords: R2 resultmre
  • 956069b8Records: R1 result, mission 007mre
  • 3c8a2e12Mission 007: #20 sort scrollmre
  • deb8cef5Records: mission 006 startedmre
  • 4f0ad42fMission 006: jukebox#20mre