jukebox
All repositories: gitoria
4.7 KB
// lib/catalog.hl — THE DERIVED TABLES (albums, artists): built from the songs table after a scan that changed anything.// 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// key (a digest of the key), so an id from the API stays valid across rebuilds. All writes to those two tables are here.import { textDigest } from 'hl:fs'import { songsTable, albumsTable, artistsTable, countOfRows } from './db.hl'import { fold, safeKey, SEP } from './util.hl'import { albumKeyOf, sortKeys } from './albums.hl'import { showOf } from './dups.hl'import { dropOrderCache } from './state.hl'static variousName = 'Various Artists'// an album name that says nothing ("Unknown album", "Unbekannt", "Unbekannter Titel"): songs with it are never taken for one compilationstatic unknownAlbum = (name) => {f = fold(name)return f == '' || f.startsWith('unknown') || f.startsWith('unbekannt')}// 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// (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// still lists the songs. Idempotent; only songs whose keys change are written. Answers how many were changed.static fixCompilations = () => {groups = {}for (s of songsTable.iterate(null, null)) {if (unknownAlbum(s.album)) { continue }if (s.albumArtist != s.artist && s.albumArtist != variousName) { continue }k = fold(s.album) + SEP + s.folderlet g = groups[k]if (g == null) { g = {} }g[fold(s.artist)] = truegroups[k] = g}let changed = 0rows = []for (s of songsTable.iterate(null, null)) { rows.push(s) }for (s of rows) {if (unknownAlbum(s.album)) { continue }if (s.albumArtist != s.artist && s.albumArtist != variousName) { continue }g = groups[fold(s.album) + SEP + s.folder]let many = 0for (a in g) { many = many + 1 }want = many >= 3 ? variousName : s.artistif (want == s.albumArtist) { continue }keys = sortKeys(want, s.album, s.disc, s.track == null ? 0 : s.track, s.title)rec = {}for (f in s) {if (f != 'id') { rec[f] = s[f] }}rec.albumArtist = wantrec.albumKey = albumKeyOf(want, s.album)rec.sortArtist = keys.sortArtistrec.sortAlbum = keys.sortAlbumsongsTable.update(s.id, rec)changed = changed + 1}return changed}static idOf = (key) => { return textDigest(key).slice(0, 16) }// Rebuild both tables from scratch (one pass over the songs, then the old rows go and the new come). Answers// { albums, artists } counts.static rebuildCatalog = () => {fixCompilations()albums = {}artists = {}dropOrderCache()for (s of songsTable.iterate(null, null)) {w = showOf(s.id, s.playCount)if (!w.shown) { continue }let a = albums[s.albumKey]if (a == null) {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.folderyear = s.year songCount = 0 duration = 0 discs = 1 playCount = 0 coverId = null sortName = safeKey(fold(s.album) + '|' + s.artistKey) }}a.songCount = a.songCount + 1if (s.disc != null && s.disc > a.discs) { a.discs = s.disc }a.duration = a.duration + (s.duration == null ? 0 : s.duration)a.playCount = a.playCount + w.playsif (a.coverId == null && s.coverId != null) { a.coverId = s.coverId }if (a.year == null && s.year != null) { a.year = s.year }albums[s.albumKey] = alet r = artists[s.artistKey]if (r == null) {r = { id = idOf('artist:' + s.artistKey) key = s.artistKey name = s.albumArtist == variousName ? s.artist : s.albumArtist songCount = 0 albumCount = 0 playCount = 0 coverId = nullsortName = s.artistKey seenAlbums = {} }}r.songCount = r.songCount + 1r.playCount = r.playCount + w.playsif (r.seenAlbums[s.albumKey] == null) {r.seenAlbums[s.albumKey] = truer.albumCount = r.albumCount + 1}if (r.coverId == null && s.coverId != null) { r.coverId = s.coverId }artists[s.artistKey] = r}// the old rows outfor (old of albumsTable.find(null, null)) { albumsTable.delete(old.id) }for (old of artistsTable.find(null, null)) { artistsTable.delete(old.id) }let na = 0for (k in albums) {albumsTable.put(albums[k])na = na + 1}let nr = 0for (k in artists) {r = artists[k]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 })nr = nr + 1}return { albums = na artists = nr }}
Branches
Latest commits
- c5b8fde9jukebox JB3b: keyboard gap repro as a reportmre
- 9daebb4bjukebox JB3b: shuffle, now-playing overlay, next and previous, play counting, media session, gates; keyboard left open (Hybriel gap)mre
- 59c1373ejukebox JB3 (work in progress): albums without folder and with disc markers folded, compilations, duplicate songs, own play counter, server-side shuffle queuemre
- 2a4cfc55jukebox: library screens (songs, artists, albums, artist, album, search, favourites), compact player bar, /stream with Range, thumbnails, virtual list vendored, ui gate and real-library checkmre
- 93a44212jukebox: report, numbers of the full scan, notes on the index key rulemre
- bf5167dajukebox: index keys without commas (mpackdb loses them after a reopen), comma fixture and gate checksmre
- 28a82fa2jukebox: README, STATUS, LOG, DECISIONSmre
- d13987dfjukebox: library scanner, ID3 reader, mpackdb database and JSON API, with fixtures and a gatemre