jukebox
All repositories: gitoria
5.5 KB
// lib/api.hl — THE JSON API (project.hl wires the routes): thin wrappers — check the request, call the topic, answer.// GET /api/stats library numbers, the scan state, the last scan, the settings// GET /api/songs?sort=title|artist|album|plays&offset&limit[&minPlays][&artist=<id>][&album=<id>]// GET /api/artists?offset&limit GET /api/albums?offset&limit[&artist=<id>]// GET /api/search?q=&offset&limit GET /api/shuffle?minPlays=&count=// GET /api/cover/<id> the cover picture (cached for a year: the id is the content hash); /api/thumb/<id> the 96 px square one// GET /stream/<id> is in lib/stream.hl (the audio, with HTTP Range)// POST /api/rescan ask for a scan now (answers at once, 202)// GET /api/settings, POST /api/settings the library paths and the rescan interval// Every list is a page { total, offset, limit, items }; the default is 50 rows and no page is bigger than 200.import { reply, fail, queryNumber, queryText, pictureReply } from './api-helpers.hl'import { jsonErrorAt } from './jsoncheck.hl'import { loadSettings, saveSettings } from './settings.hl'import { scanState } from './library.hl'import { requestScan } from './schedule.hl'import { lastScan } from './db.hl'import { countPlay } from './plays.hl'import { copyReport } from './dups.hl'import { queueIds } from './queue.hl'import { dropOrderCache } from './state.hl'import { now } from 'hl:time'import { libraryStats, songsPage, artistsPage, albumsPage, searchAll, shuffle } from './browse.hl'static scanSummary = () => {st = scanStatereturn { running = st.running phase = st.phase seen = st.seen added = st.added updated = st.updated removed = st.removedunreadable = st.unreadable lastSeconds = st.lastSeconds lastError = st.lastError queued = st.wanted }}static apiStats = (route, req) => {if (req.method != 'GET') { return fail(405, 'GET only') }return reply(200, { library = libraryStats() scan = scanSummary() lastScan = lastScan() settings = loadSettings() })}static apiSongs = (route, req) => {if (req.method != 'GET') { return fail(405, 'GET only') }sort = queryText(req, 'sort')if (sort != null && sort != 'title' && sort != 'artist' && sort != 'album' && sort != 'plays') { return fail(400, 'sort is title, artist, album or plays') }return reply(200, songsPage(sort == null ? 'artist' : sort, queryNumber(req, 'offset'), queryNumber(req, 'limit'), queryNumber(req, 'minPlays'), queryText(req, 'artist'), queryText(req, 'album')))}static apiArtists = (route, req) => {if (req.method != 'GET') { return fail(405, 'GET only') }return reply(200, artistsPage(queryNumber(req, 'offset'), queryNumber(req, 'limit')))}static apiAlbums = (route, req) => {if (req.method != 'GET') { return fail(405, 'GET only') }return reply(200, albumsPage(queryNumber(req, 'offset'), queryNumber(req, 'limit'), queryText(req, 'artist')))}static apiSearch = (route, req) => {if (req.method != 'GET') { return fail(405, 'GET only') }q = queryText(req, 'q')if (q == null) { return fail(400, 'q is needed') }if (q.length > 200) { return fail(400, 'q is too long') }return reply(200, searchAll(q, queryNumber(req, 'offset'), queryNumber(req, 'limit')))}static apiShuffle = (route, req) => {if (req.method != 'GET') { return fail(405, 'GET only') }return reply(200, shuffle(queryNumber(req, 'minPlays'), queryNumber(req, 'count')))}static apiCover = (Hybrid route, Hybrid req) => { return pictureReply(route, req, false) }static apiThumb = (Hybrid route, Hybrid req) => { return pictureReply(route, req, true) }static apiRescan = (route, req) => {if (req.method != 'POST') { return fail(405, 'POST only') }started = requestScan()return reply(202, { started = started running = scanState.running })}static apiSettings = (route, req) => {if (req.method == 'GET') { return reply(200, loadSettings()) }if (req.method != 'POST') { return fail(405, 'GET or POST only') }if (req.body == null || req.body == '') { return fail(400, 'a JSON body is needed') }if (jsonErrorAt(req.body) >= 0) { return fail(400, 'the body is not valid JSON') }r = saveSettings(JSON.parse(req.body))if (r.error != null) { return fail(400, r.error) }return reply(200, r)}static apiNotFound = (route, req) => { return fail(404, 'no such endpoint') }// POST /api/played/<song id>: the jukebox counts one play of the song (its own counter; the file is never written)static apiPlayed = (route, req) => {if (req.method != 'POST') { return fail(405, 'POST only') }id = route.params.idr = countPlay(id, now())if (r == null) { return fail(404, 'no such song') }return reply(200, r)}// GET /api/copies/<song id>: every copy of the song (same artist and title, lengths within 2 s) with its own countsstatic apiCopies = (Hybrid route, Hybrid req) => {if (req.method != 'GET') { return fail(405, 'GET only') }r = copyReport(route.params.id)if (r == null) { return fail(404, 'no such song') }return reply(200, r)}// GET /api/queue?seed=&kind=favourites&minPlays=&from=&count=: the ids of a shuffle's next songs (the order the player gets), and how many songs the pool holdsstatic apiQueue = (Hybrid route, Hybrid req) => {if (req.method != 'GET') { return fail(405, 'GET only') }seed = queryNumber(req, 'seed')if (seed == null) { return fail(400, 'seed is needed') }count = queryNumber(req, 'count')if (count != null && count > 20000) { return fail(400, 'count is at most 20000') }return reply(200, queueIds(seed, queryNumber(req, 'minPlays'), queryNumber(req, 'from'), count))}
Branches
Latest commits
- 0a2ae3daMerge remote-tracking branch 'origin/records-1123' into records-1124mre
- 9a48cfcaMerge branch 'vendor-theme' into mainJukebox
- 170d3735Merge branch 'code-order-2' into mainJukebox
- 3f1918a7|||PR Re-sync layouts theme 4099e64 with Segoe UI firstmre
- 2e7f6a43Vendor virtual-list 489afcf from components.hybrielmre
- 0ba85799Merge branch 'reset-classes' into mainJukebox
- 502eeed5jukebox#20: vendor virtual-list initial resetKey baseline fixmre
- 6d5ce287|||PR jukebox#9 Centralize songs table reads and writes in db.hlmre
- dcca21abjukebox#8 Move copies and queue logic into their topicsmre
- 3b76651cjukebox#20: update virtual-list README pin and resetKey optionsmre
- bc09e1e8|||PR jukebox#20: reset virtual lists through resetKey (partial validation)mre
- a3796dc5Records: live step waits for idlemre
- 3b158e16Records: merges of #9 and #11mre
- 27204167Merge main into records-1123mre
- 10f8a8c8Merge branch 'code-order' into mainJukebox
- 204fc828Merge branch 'day72' into mainJukebox
- 8e8d0e2b|||PR Records: PROCEDURES.md, missions 016-019, GO 2026-11-23mre
- 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