quintodrome/ui/src/subsonic/index.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

104 lines
2.5 KiB
JavaScript
Raw Normal View History

import { baseUrl } from '../utils'
2021-06-15 13:29:01 -08:00
import { httpClient } from '../dataProvider'
const url = (command, id, options) => {
const username = localStorage.getItem('username')
const token = localStorage.getItem('subsonic-token')
const salt = localStorage.getItem('subsonic-salt')
if (!username || !token || !salt) {
return ''
}
const params = new URLSearchParams()
params.append('u', username)
params.append('t', token)
params.append('s', salt)
params.append('f', 'json')
params.append('v', '1.8.0')
params.append('c', 'NavidromeUI')
id && params.append('id', id)
if (options) {
if (options.ts) {
options['_'] = new Date().getTime()
delete options.ts
}
Object.keys(options).forEach((k) => {
params.append(k, options[k])
})
}
2021-06-15 13:29:01 -08:00
return `/rest/${command}?${params.toString()}`
}
const scrobble = (id, time, submission = true) =>
httpClient(
url('scrobble', id, {
...(submission && time && { time }),
submission,
}),
)
const nowPlaying = (id) => scrobble(id, null, false)
2021-06-15 13:29:01 -08:00
const star = (id) => httpClient(url('star', id))
2021-06-15 13:29:01 -08:00
const unstar = (id) => httpClient(url('unstar', id))
2021-06-15 13:29:01 -08:00
const setRating = (id, rating) => httpClient(url('setRating', id, { rating }))
const download = (id, format = 'raw', bitrate = '0') =>
(window.location.href = baseUrl(url('download', id, { format, bitrate })))
2021-06-15 13:29:01 -08:00
const startScan = (options) => httpClient(url('startScan', null, options))
const getScanStatus = () => httpClient(url('getScanStatus'))
const getCoverArtUrl = (record, size, square) => {
const options = {
2022-12-28 06:19:52 -09:00
...(record.updatedAt && { _: record.updatedAt }),
...(size && { size }),
...(square && { square }),
}
2022-12-22 07:47:18 -09:00
// TODO Move this logic to server. `song` and `album` should have a CoverArtID
2022-12-19 11:34:21 -09:00
if (record.album) {
2022-12-27 07:36:23 -09:00
return baseUrl(url('getCoverArt', 'mf-' + record.id, options))
} else if (record.artist) {
2022-12-27 07:36:23 -09:00
return baseUrl(url('getCoverArt', 'al-' + record.id, options))
} else {
return baseUrl(url('getCoverArt', 'ar-' + record.id, options))
}
}
const getArtistInfo = (id) => {
return httpClient(url('getArtistInfo', id))
}
const getAlbumInfo = (id) => {
return httpClient(url('getAlbumInfo', id))
}
2023-01-19 18:52:55 -09:00
const streamUrl = (id, options) => {
return baseUrl(
url('stream', id, {
ts: true,
...options,
}),
2023-01-19 18:52:55 -09:00
)
}
export default {
url,
scrobble,
nowPlaying,
download,
star,
unstar,
setRating,
2021-06-15 13:29:01 -08:00
startScan,
getScanStatus,
getCoverArtUrl,
streamUrl,
getAlbumInfo,
getArtistInfo,
}