feat: dynamic states updates on /player

This commit is contained in:
Mateusz Pstrucha committed 2021-09-29 00:42:45 +02:00
1 parent ebef81eda3
commit ff1215127b
2 files changed
+126 -17

No files matched your search

+21 -17
View File
@@ -10,6 +10,7 @@ import track from './routes/track.js'
import search from './routes/search.js'
import vote from './routes/vote.js'
import login from './routes/login.js'
import player from './routes/player.js'
dotenv.config()
@@ -25,6 +26,7 @@ app.use('/track', track)
app.use('/search', search)
app.use('/vote', vote)
app.use('/login', login)
app.use('/player', player)
www.listen(process.env.portHttp)
@@ -42,20 +44,22 @@ www.on('listening', function () {
var i = 0
setInterval(async () => {
broadcast([
{
cat: 'status',
trackid: db.top.list[i],
progress: 20e3 + new Date().getMilliseconds(),
paused: false,
},
{
cat: 'top',
trackids: await db.top.get(),
timestamp: db.top.lastCount,
},
])
i++
if (i >= db.top.list.length) i = 0
}, 10e3)
// setInterval(async () => {
// db.current.update([
// {
// cat: 'status',
// content: {
// tid: db.top.list[i],
// },
// },
// {
// cat: 'top',
// content: {
// tids: await db.top.get(),
// timestamp: db.top.lastCount,
// },
// },
// ])
// i++
// if (i >= db.top.list.length) i = 0
// }, 10e3)
+105
View File
@@ -0,0 +1,105 @@
import dotenv from 'dotenv'
import express from 'express'
import spotifyToYt from 'spotify-to-yt'
import db from '../db.js'
import Track from '../Track.js'
dotenv.config()
const router = express.Router()
function update(data) {
if (!Array.isArray(data)) data = [data]
db.current.update(data)
}
const playerSecret = 'Bearer ' + process.env.playerSecret
// set current track
router.put(
'/set/status',
(
{
body: { tid, progress = null, duration = null, paused = null },
headers: { authorization },
},
res
) => {
if (authorization === undefined)
return res.status(401).end('Authorization required')
if (authorization != playerSecret)
return res.status(403).end('Token invalid')
if (tid === undefined) return res.status(400).end('"tid" field required')
if (tid.length < Track.length) return res.status(400).end('Invalid tid')
if (progress !== null)
if (typeof progress !== 'number')
return res.status(400).end('progress value invalid')
if (duration !== null)
if (typeof duration !== 'number')
return res.status(400).end('duration value invalid')
if (paused !== null)
if (typeof paused !== 'boolean')
return res.status(400).end('paused value invalid')
update({
cat: 'status',
content: {
tid,
...(progress !== null && { progress }),
...(duration !== null && { duration }),
...(paused !== null && { paused }),
},
})
res.json('true')
}
)
// set next track
router.put(
'/set/next',
({ body: { tid }, headers: { authorization } }, res) => {
if (authorization === undefined)
return res.status(401).end('Authorization required')
if (authorization != playerSecret)
return res.status(403).end('Token invalid')
if (tid === undefined) return res.status(400).end('"tid" field required')
if (tid.length < Track.length) return res.status(400).end('Invalid tid')
update({
cat: 'next',
content: { tid },
})
res.json('true')
}
)
router.get('/get/top/', ({ headers: { authorization } }, res) => {
if (authorization === undefined)
return res.status(401).end('Authorization required')
if (authorization != playerSecret) return res.status(403).end('Token invalid')
db.top.get().then((top) => {
Promise.all(
top.map((tid) => spotifyToYt.trackGet(`spotify:track:${tid}`))
).then((yts) => {
top.map((tid, i) => (yts[i].tid = tid))
res.json(yts)
})
})
})
export default router