gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit9f3653769f365376README: the bar's controls (jb19)mre9f365376/lib/api.hl

6.8 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 { Response } from 'hl:http1'
  12. import { reply, fail, queryNumber, queryText } from './api-helpers.hl'
  13. import { jsonErrorAt } from './jsoncheck.hl'
  14. import { loadSettings, saveSettings } from './settings.hl'
  15. import { scanState } from './library.hl'
  16. import { requestScan } from './schedule.hl'
  17. import { lastScan } from './db.hl'
  18. import { countPlay, jbCount } from './plays.hl'
  19. import { copiesOf } from './dups.hl'
  20. import { idAt, poolSize } from './queue.hl'
  21. import { dropOrderCache } from './state.hl'
  22. import { songsTable } from './db.hl'
  23. import { now } from 'hl:time'
  24. import { libraryStats, songsPage, artistsPage, albumsPage, searchAll, shuffle } from './browse.hl'
  25. import { coverFile, thumbFile } from './covers.hl'
  26. static scanSummary = () => {
  27. st = scanState
  28. return { running = st.running phase = st.phase seen = st.seen added = st.added updated = st.updated removed = st.removed
  29. unreadable = st.unreadable lastSeconds = st.lastSeconds lastError = st.lastError queued = st.wanted }
  30. }
  31. static apiStats = (route, req) => {
  32. if (req.method != 'GET') { return fail(405, 'GET only') }
  33. return reply(200, { library = libraryStats() scan = scanSummary() lastScan = lastScan() settings = loadSettings() })
  34. }
  35. static apiSongs = (route, req) => {
  36. if (req.method != 'GET') { return fail(405, 'GET only') }
  37. sort = queryText(req, 'sort')
  38. if (sort != null && sort != 'title' && sort != 'artist' && sort != 'album' && sort != 'plays') { return fail(400, 'sort is title, artist, album or plays') }
  39. return reply(200, songsPage(sort == null ? 'artist' : sort, queryNumber(req, 'offset'), queryNumber(req, 'limit'), queryNumber(req, 'minPlays'), queryText(req, 'artist'), queryText(req, 'album')))
  40. }
  41. static apiArtists = (route, req) => {
  42. if (req.method != 'GET') { return fail(405, 'GET only') }
  43. return reply(200, artistsPage(queryNumber(req, 'offset'), queryNumber(req, 'limit')))
  44. }
  45. static apiAlbums = (route, req) => {
  46. if (req.method != 'GET') { return fail(405, 'GET only') }
  47. return reply(200, albumsPage(queryNumber(req, 'offset'), queryNumber(req, 'limit'), queryText(req, 'artist')))
  48. }
  49. static apiSearch = (route, req) => {
  50. if (req.method != 'GET') { return fail(405, 'GET only') }
  51. q = queryText(req, 'q')
  52. if (q == null) { return fail(400, 'q is needed') }
  53. if (q.length > 200) { return fail(400, 'q is too long') }
  54. return reply(200, searchAll(q, queryNumber(req, 'offset'), queryNumber(req, 'limit')))
  55. }
  56. static apiShuffle = (route, req) => {
  57. if (req.method != 'GET') { return fail(405, 'GET only') }
  58. return reply(200, shuffle(queryNumber(req, 'minPlays'), queryNumber(req, 'count')))
  59. }
  60. static apiCover = (route, req) => {
  61. if (req.method != 'GET') { return fail(405, 'GET only') }
  62. id = route.params.id
  63. if (id == null || id.length > 64) { return fail(404, 'no such cover') }
  64. c = coverFile(id)
  65. if (c == null) { return fail(404, 'no such cover') }
  66. return new Response(c.bytes, { status = 200 headers = { 'Content-Type' = c.mime 'Cache-Control' = 'public, max-age=31536000, immutable' 'X-Content-Type-Options' = 'nosniff' } })
  67. }
  68. static apiThumb = (route, req) => {
  69. if (req.method != 'GET') { return fail(405, 'GET only') }
  70. id = route.params.id
  71. if (id == null || id.length > 64) { return fail(404, 'no such cover') }
  72. c = thumbFile(id)
  73. if (c == null) { return fail(404, 'no such cover') }
  74. return new Response(c.bytes, { status = 200 headers = { 'Content-Type' = c.mime 'Cache-Control' = 'public, max-age=31536000, immutable' 'X-Content-Type-Options' = 'nosniff' } })
  75. }
  76. static apiRescan = (route, req) => {
  77. if (req.method != 'POST') { return fail(405, 'POST only') }
  78. started = requestScan()
  79. return reply(202, { started = started running = scanState.running })
  80. }
  81. static apiSettings = (route, req) => {
  82. if (req.method == 'GET') { return reply(200, loadSettings()) }
  83. if (req.method != 'POST') { return fail(405, 'GET or POST only') }
  84. if (req.body == null || req.body == '') { return fail(400, 'a JSON body is needed') }
  85. if (jsonErrorAt(req.body) >= 0) { return fail(400, 'the body is not valid JSON') }
  86. r = saveSettings(JSON.parse(req.body))
  87. if (r.error != null) { return fail(400, r.error) }
  88. return reply(200, r)
  89. }
  90. static apiNotFound = (route, req) => { return fail(404, 'no such endpoint') }
  91. // POST /api/played/<song id>: the jukebox counts one play of the song (its own counter; the file is never written)
  92. static apiPlayed = (route, req) => {
  93. if (req.method != 'POST') { return fail(405, 'POST only') }
  94. id = route.params.id
  95. r = countPlay(id, now())
  96. if (r == null) { return fail(404, 'no such song') }
  97. return reply(200, r)
  98. }
  99. // GET /api/copies/<song id>: every copy of the song (same artist and title, lengths within 2 s) with its own counts
  100. static apiCopies = (route, req) => {
  101. if (req.method != 'GET') { return fail(405, 'GET only') }
  102. s = songsTable.fetch(route.params.id)
  103. if (s == null) { return fail(404, 'no such song') }
  104. items = []
  105. for (id of copiesOf(s.id)) {
  106. c = songsTable.fetch(id)
  107. if (c != null) { items.push({ id = c.id path = c.path album = c.album duration = c.duration filePlays = c.playCount jukeboxPlays = jbCount(c.id) }) }
  108. }
  109. return reply(200, { id = s.id items = items })
  110. }
  111. // 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
  112. static apiQueue = (route, req) => {
  113. if (req.method != 'GET') { return fail(405, 'GET only') }
  114. seed = queryNumber(req, 'seed')
  115. if (seed == null) { return fail(400, 'seed is needed') }
  116. min = queryNumber(req, 'minPlays')
  117. ctx = { kind = 'shuffle' seed = seed spec = { kind = 'favourites' minPlays = min == null ? 5 : min } }
  118. start = queryNumber(req, 'from') == null ? 0 : queryNumber(req, 'from')
  119. count = queryNumber(req, 'count') == null ? 50 : queryNumber(req, 'count')
  120. if (count > 20000) { return fail(400, 'count is at most 20000') }
  121. ids = []
  122. let i = start
  123. while (i < start + count) {
  124. ids.push(idAt(ctx, i))
  125. i = i + 1
  126. }
  127. return reply(200, { pool = poolSize(ctx) start = start ids = ids })
  128. }

Branches

Latest commits

  • 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
  • 514d7cbeRecords: mission 008 startedmre
  • 61c40127Mission 008: #19 + #21mre
  • 6dcaeb46Records: resetKey planmre
  • c5dbf2ecRecords: R2 resultmre
  • 956069b8Records: R1 result, mission 007mre
  • 3c8a2e12Mission 007: #20 sort scrollmre
  • deb8cef5Records: mission 006 startedmre
  • 4f0ad42fMission 006: jukebox#20mre
  • 800cae06Records: #21 planmre