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
- 277a2722README: provenance linemre
- 4169ca6aREADME: sync pinmre
- a01f3aa8ui gate: back to the top before the sort checks (the screen reopens where it was left)mre
- f7936c20Lists keep their scroll position across a reload (jukebox#20): per-list offsets in storage/ui.json, gate jb20mre
- d67906devirtual-list and icons from components.hybriel e5b98ec (centerId, scrollTop, onScroll)mre
- df7920ceRecords: R3 startedmre
- 0549f038Mission 010: jb20 livemre
- 273494e2STATUS rewrittenmre
- 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