gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitc1b153c4c1b153c4Merge branch 'records-1124c' into mainJukeboxc1b153c4/lib/api.hl

5.5 KB

  1. // lib/api.hl — THE JSON API (project.hl wires the routes): thin wrappers — check the request, call the topic, answer.
  2. // GET /api/stats library numbers, the scan state, the last scan, the settings
  3. // GET /api/songs?sort=title|artist|album|plays&offset&limit[&minPlays][&artist=<id>][&album=<id>]
  4. // GET /api/artists?offset&limit GET /api/albums?offset&limit[&artist=<id>]
  5. // GET /api/search?q=&offset&limit GET /api/shuffle?minPlays=&count=
  6. // 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
  7. // GET /stream/<id> is in lib/stream.hl (the audio, with HTTP Range)
  8. // POST /api/rescan ask for a scan now (answers at once, 202)
  9. // GET /api/settings, POST /api/settings the library paths and the rescan interval
  10. // Every list is a page { total, offset, limit, items }; the default is 50 rows and no page is bigger than 200.
  11. import { reply, fail, queryNumber, queryText, pictureReply } from './api-helpers.hl'
  12. import { jsonErrorAt } from './jsoncheck.hl'
  13. import { loadSettings, saveSettings } from './settings.hl'
  14. import { scanState } from './library.hl'
  15. import { requestScan } from './schedule.hl'
  16. import { lastScan } from './db.hl'
  17. import { countPlay } from './plays.hl'
  18. import { copyReport } from './dups.hl'
  19. import { queueIds } from './queue.hl'
  20. import { dropOrderCache } from './state.hl'
  21. import { now } from 'hl:time'
  22. import { libraryStats, songsPage, artistsPage, albumsPage, searchAll, shuffle } from './browse.hl'
  23. static scanSummary = () => {
  24. st = scanState
  25. return { running = st.running phase = st.phase seen = st.seen added = st.added updated = st.updated removed = st.removed
  26. unreadable = st.unreadable lastSeconds = st.lastSeconds lastError = st.lastError queued = st.wanted }
  27. }
  28. static apiStats = (route, req) => {
  29. if (req.method != 'GET') { return fail(405, 'GET only') }
  30. return reply(200, { library = libraryStats() scan = scanSummary() lastScan = lastScan() settings = loadSettings() })
  31. }
  32. static apiSongs = (route, req) => {
  33. if (req.method != 'GET') { return fail(405, 'GET only') }
  34. sort = queryText(req, 'sort')
  35. if (sort != null && sort != 'title' && sort != 'artist' && sort != 'album' && sort != 'plays') { return fail(400, 'sort is title, artist, album or plays') }
  36. return reply(200, songsPage(sort == null ? 'artist' : sort, queryNumber(req, 'offset'), queryNumber(req, 'limit'), queryNumber(req, 'minPlays'), queryText(req, 'artist'), queryText(req, 'album')))
  37. }
  38. static apiArtists = (route, req) => {
  39. if (req.method != 'GET') { return fail(405, 'GET only') }
  40. return reply(200, artistsPage(queryNumber(req, 'offset'), queryNumber(req, 'limit')))
  41. }
  42. static apiAlbums = (route, req) => {
  43. if (req.method != 'GET') { return fail(405, 'GET only') }
  44. return reply(200, albumsPage(queryNumber(req, 'offset'), queryNumber(req, 'limit'), queryText(req, 'artist')))
  45. }
  46. static apiSearch = (route, req) => {
  47. if (req.method != 'GET') { return fail(405, 'GET only') }
  48. q = queryText(req, 'q')
  49. if (q == null) { return fail(400, 'q is needed') }
  50. if (q.length > 200) { return fail(400, 'q is too long') }
  51. return reply(200, searchAll(q, queryNumber(req, 'offset'), queryNumber(req, 'limit')))
  52. }
  53. static apiShuffle = (route, req) => {
  54. if (req.method != 'GET') { return fail(405, 'GET only') }
  55. return reply(200, shuffle(queryNumber(req, 'minPlays'), queryNumber(req, 'count')))
  56. }
  57. static apiCover = (Hybrid route, Hybrid req) => { return pictureReply(route, req, false) }
  58. static apiThumb = (Hybrid route, Hybrid req) => { return pictureReply(route, req, true) }
  59. static apiRescan = (route, req) => {
  60. if (req.method != 'POST') { return fail(405, 'POST only') }
  61. started = requestScan()
  62. return reply(202, { started = started running = scanState.running })
  63. }
  64. static apiSettings = (route, req) => {
  65. if (req.method == 'GET') { return reply(200, loadSettings()) }
  66. if (req.method != 'POST') { return fail(405, 'GET or POST only') }
  67. if (req.body == null || req.body == '') { return fail(400, 'a JSON body is needed') }
  68. if (jsonErrorAt(req.body) >= 0) { return fail(400, 'the body is not valid JSON') }
  69. r = saveSettings(JSON.parse(req.body))
  70. if (r.error != null) { return fail(400, r.error) }
  71. return reply(200, r)
  72. }
  73. static apiNotFound = (route, req) => { return fail(404, 'no such endpoint') }
  74. // POST /api/played/<song id>: the jukebox counts one play of the song (its own counter; the file is never written)
  75. static apiPlayed = (route, req) => {
  76. if (req.method != 'POST') { return fail(405, 'POST only') }
  77. id = route.params.id
  78. r = countPlay(id, now())
  79. if (r == null) { return fail(404, 'no such song') }
  80. return reply(200, r)
  81. }
  82. // GET /api/copies/<song id>: every copy of the song (same artist and title, lengths within 2 s) with its own counts
  83. static apiCopies = (Hybrid route, Hybrid req) => {
  84. if (req.method != 'GET') { return fail(405, 'GET only') }
  85. r = copyReport(route.params.id)
  86. if (r == null) { return fail(404, 'no such song') }
  87. return reply(200, r)
  88. }
  89. // 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 holds
  90. static apiQueue = (Hybrid route, Hybrid req) => {
  91. if (req.method != 'GET') { return fail(405, 'GET only') }
  92. seed = queryNumber(req, 'seed')
  93. if (seed == null) { return fail(400, 'seed is needed') }
  94. count = queryNumber(req, 'count')
  95. if (count != null && count > 20000) { return fail(400, 'count is at most 20000') }
  96. return reply(200, queueIds(seed, queryNumber(req, 'minPlays'), queryNumber(req, 'from'), count))
  97. }

Branches

Latest commits

  • c1b153c4Merge branch 'records-1124c' into mainJukebox
  • d7b5e780|||PR Records 2026-11-24 (2): live day72, PR #19 merge, #10 newsmre
  • 43b564dbMerge branch 'records-1124b' into mainJukebox
  • fa86b08c|||PR Records 2026-11-24: LOG, STATUS, missions 020-023mre
  • 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