jukebox
All repositories: gitoria
6.8 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 { Response } from 'hl:http1'import { reply, fail, queryNumber, queryText } 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, jbCount } from './plays.hl'import { copiesOf } from './dups.hl'import { idAt, poolSize } from './queue.hl'import { dropOrderCache } from './state.hl'import { songsTable } from './db.hl'import { now } from 'hl:time'import { libraryStats, songsPage, artistsPage, albumsPage, searchAll, shuffle } from './browse.hl'import { coverFile, thumbFile } from './covers.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 = (route, req) => {if (req.method != 'GET') { return fail(405, 'GET only') }id = route.params.idif (id == null || id.length > 64) { return fail(404, 'no such cover') }c = coverFile(id)if (c == null) { return fail(404, 'no such cover') }return new Response(c.bytes, { status = 200 headers = { 'Content-Type' = c.mime 'Cache-Control' = 'public, max-age=31536000, immutable' 'X-Content-Type-Options' = 'nosniff' } })}static apiThumb = (route, req) => {if (req.method != 'GET') { return fail(405, 'GET only') }id = route.params.idif (id == null || id.length > 64) { return fail(404, 'no such cover') }c = thumbFile(id)if (c == null) { return fail(404, 'no such cover') }return new Response(c.bytes, { status = 200 headers = { 'Content-Type' = c.mime 'Cache-Control' = 'public, max-age=31536000, immutable' 'X-Content-Type-Options' = 'nosniff' } })}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 = (route, req) => {if (req.method != 'GET') { return fail(405, 'GET only') }s = songsTable.fetch(route.params.id)if (s == null) { return fail(404, 'no such song') }items = []for (id of copiesOf(s.id)) {c = songsTable.fetch(id)if (c != null) { items.push({ id = c.id path = c.path album = c.album duration = c.duration filePlays = c.playCount jukeboxPlays = jbCount(c.id) }) }}return reply(200, { id = s.id items = items })}// 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 = (route, req) => {if (req.method != 'GET') { return fail(405, 'GET only') }seed = queryNumber(req, 'seed')if (seed == null) { return fail(400, 'seed is needed') }min = queryNumber(req, 'minPlays')ctx = { kind = 'shuffle' seed = seed spec = { kind = 'favourites' minPlays = min == null ? 5 : min } }start = queryNumber(req, 'from') == null ? 0 : queryNumber(req, 'from')count = queryNumber(req, 'count') == null ? 50 : queryNumber(req, 'count')if (count > 20000) { return fail(400, 'count is at most 20000') }ids = []let i = startwhile (i < start + count) {ids.push(idAt(ctx, i))i = i + 1}return reply(200, { pool = poolSize(ctx) start = start ids = ids })}
Branches
Latest commits
- 193dfff1Records: S1 result, S1b startedmre
- 028ed776Records: mission 001 continuation S1bmre
- 1838d7e8Records: S1 startedmre
- a43fca3fRecords: mission 001 decided pointsmre
- 41773828Records: statusmre
- a82c0805Records: mission 001 brief (Style form)mre
- a4bce6d1Records: ticket push subscriptionmre
- 14abde3bjukebox: records: Atlas's records moved to worldapi-conductor/archivemre
- 0433e9a8jukebox: records: gitoria remote added and pushedmre
- 1722d707jukebox: records: repo creation handed to Gitoriamre
- b42e7dfbjukebox: conductor records (CONDUCTOR.md, STATUS.md), old STATUS archivedmre
- 1b0d0bc1jukebox: a startup error prints its message before exit 1 (hybriel#247)mre
- b19c478bjukebox JB18 (jukebox#17): onto Hybriel set-day31 (b287cd82, contains the SIGPIPE fix 6d2ca454): plugins/core and plugins/web re-vendored, bin/ from the set; a client that closes /stream while it is written no longer kills the server (90 aborted streams survived, the old bin died with exit 141 on the first)mre
- 73867a2ejukebox#2: Style blocks in the language's nested form (rules named after the View's own elements, pseudo-classes nested, merged duplicates); the same CSS rules, the pages look the samemre
- 06c548d6jukebox JB15: report, log linemre
- 980859cbjukebox JB15 (jukebox#15): the now-playing overlay on a short screen (tablet or phone on its side) gave the queue 12 to 100 px because the big cover took the room; the cover now shrinks with the screen height and goes away under 41rem, the queue keeps 3+ rows; gate tests/jb15.mjs at 8 sizesmre
- 9efc40ffjukebox#12 #14: Grok session 1793246574527-181160mre
- 7d58b36ejukebox JB13 (jukebox#13): Next and Previous ask for the songs after / before the current song's id in the current order, not for place + 1; gate tests/jb13.mjs checks Next with the queue down to one song after the order changedmre
- 51358794jukebox JB14 (jukebox#14): the Songs screen opens sorted the way it was left (kept on the server in storage/ui.json), the sort control shows the sort the rows really have (selected options, autocomplete off); gate tests/jb14.mjsmre
- cc68a063jukebox JB13 (jukebox#13): a tapped song plays the song that was tapped, found by its id in the list as it is now (not by its row number, which counted plays move); the queue goes on from its place there, the rows refresh on the tap; gate tests/jb13.mjs (songs by plays, favourites)mre