Repo maintenance & add nowplaying

This commit is contained in:
=
2024-06-26 16:48:05 -07:00
parent 3b61566024
commit 9a7a5b69ee
6 changed files with 109 additions and 34 deletions

View File

@@ -1,11 +1,33 @@
const { app, globalConfig } = require("../index.js")
const { queryLastfm } = require("../liberals/nowplaying.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("/nowplaying", (rreq,rres) => {
if (rreq.query.format === "html") {
rres.send("<span>The /nowplaying endpoint is currently under construction.</span>")
if (Date.now() < timeSinceLastLastfmQuery+5000) {
rres.send(cachedLastfmResult[rreq.query.format] ?? cachedLastfmResult.json)
} else {
rres.send({"message":"The /nowplaying endpoint is currently under construction."})
timeSinceLastLastfmQuery = Date.now()
queryLastfm().then(response => {
if (response == 1) {
cachedLastfmResult = notPlayingAnythingPlaceholder
} else {
cachedLastfmResult = response
}
rres.send(cachedLastfmResult[rreq.query.format] ?? cachedLastfmResult.json)
})
}
})
module.exports = {app}