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
- 95f5faae|||PR jukebox#7 Drop needless let declarationsmre
- 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