Files
enstrayedapi/disabled/nowplaying.js
2024-09-22 14:04:15 -07:00

33 lines
929 B
JavaScript

import { app, globalConfig } from "../index.js"
import { queryLastfm } from "../liberals/libnowplaying.js"
var timeSinceLastLastfmQuery = Date.now()-5000
var cachedLastfmResult = {}
const notPlayingAnythingPlaceholder = {
"json": {
"playing": false
},
"html": `<span>I'm not currently listening to anything.</span>`
}
app.get("/api/nowplaying", (rreq,rres) => {
if (Date.now() < timeSinceLastLastfmQuery+5000) {
rres.send(cachedLastfmResult[rreq.query.format] ?? cachedLastfmResult.json)
} else {
timeSinceLastLastfmQuery = Date.now()
queryLastfm().then(response => {
if (response == {}) {
cachedLastfmResult = notPlayingAnythingPlaceholder
} else {
cachedLastfmResult = response
}
rres.send(cachedLastfmResult[rreq.query.format] ?? cachedLastfmResult.json)
})
}
})
export {app}