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

4
.gitignore vendored
View File

@@ -1,4 +1,6 @@
node_modules/ node_modules/
config.json config.json
bun.lockb bun.lockb
GITVERSION GITVERSION
todo.txt
proto.js

View File

@@ -8,12 +8,6 @@
"host": "hazeldale:5984", "host": "hazeldale:5984",
"authorization": "" "authorization": ""
}, },
"cider": {
"targetHosts": ["localhost:10769"],
"authKeysDoc": "cider"
},
"mailjet": { "mailjet": {
"apiKey": "", "apiKey": "",
@@ -23,14 +17,25 @@
"authKeysDoc": "mailjet" "authKeysDoc": "mailjet"
}, },
"etyd": {
"randomHexLength": 6,
"authKeyInDb": "apiAuthKeys.etyd"
},
"blog": { "blog": {
"postsDirectory": "C:/Users/natha/Downloads/proto/posts", "postsDirectory": "C:/Users/natha/Downloads/proto/posts",
"postsDirUrl": "/posts" "postsDirUrl": "/posts"
},
"nowplaying": {
"lastfm": {
"apiKey": "",
"target": "enstrayed"
},
"jellyfin": {
"apiKey": "",
"host": "",
"target": ""
},
"cider": {
"apiKeys": [],
"hosts": []
}
} }
} }

26
liberals/nowplaying.js Normal file
View File

@@ -0,0 +1,26 @@
const { globalConfig } = require("../index.js")
async function queryLastfm() {
return await fetch(`https://ws.audioscrobbler.com/2.0/?format=json&method=user.getrecenttracks&limit=1&api_key=${globalConfig.nowplaying.lastfm.apiKey}&user=${globalConfig.nowplaying.lastfm.target}`).then(response => response.json()).then(response => {
if (response["recenttracks"] == undefined) {
return 1
} else {
if (response.recenttracks.track[0]["@attr"] == undefined) {
return 1
} else {
return {
"json": {
"songName": response.recenttracks.track[0].name,
"artistName": response.recenttracks.track[0].artist["#text"],
"albumName": response.recenttracks.track[0].album["#text"],
"artUrl": response.recenttracks.track[0].image[3]["#text"],
"link": response.recenttracks.track[0].url
},
"html": `<img src="${response.recenttracks.track[0].image[3]["#text"]}" alt="Album Art" style="width: 10em;"> <div class="textlist"> <p>I'm listening to</p> <h3>${response.recenttracks.track[0].name} by ${response.recenttracks.track[0].artist["#text"]}</h3> <p>from ${response.recenttracks.track[0].album["#text"]}</p> <a href="${response.recenttracks.track[0].url}" class="noindent">View on Last.fm</a></div>`
}
}
}
})
}
module.exports = { queryLastfm }

56
package-lock.json generated
View File

@@ -9,22 +9,43 @@
"version": "1.0.0", "version": "1.0.0",
"license": "UNLICENSED", "license": "UNLICENSED",
"dependencies": { "dependencies": {
"express": "^4.18.2", "express": "^4.18.2"
"typescript": "^5.4.3"
}, },
"devDependencies": { "devDependencies": {
"@types/bun": "^1.0.12",
"@types/node": "^20.12.3" "@types/node": "^20.12.3"
} }
}, },
"node_modules/@types/node": { "node_modules/@types/bun": {
"version": "20.12.3", "version": "1.1.5",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.12.3.tgz", "resolved": "https://registry.npmjs.org/@types/bun/-/bun-1.1.5.tgz",
"integrity": "sha512-sD+ia2ubTeWrOu+YMF+MTAB7E+O7qsMqAbMfW7DG3K1URwhZ5hN1pLlRVGbf4wDFzSfikL05M17EyorS86jShw==", "integrity": "sha512-7RprVDMF+1o+EWSo7F1+iJpkfNz+Ikw9K//vwambcY+D1QHXfb9l7jWY1hSBfuFEkW9yFAhkMzP2uTi1pQXoqw==",
"dev": true, "dev": true,
"license": "MIT",
"dependencies": {
"bun-types": "1.1.14"
}
},
"node_modules/@types/node": {
"version": "20.12.14",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.12.14.tgz",
"integrity": "sha512-scnD59RpYD91xngrQQLGkE+6UrHUPzeKZWhhjBSa3HSkwjbQc38+q3RoIVEwxQGRw3M+j5hpNAM+lgV3cVormg==",
"dev": true,
"license": "MIT",
"dependencies": { "dependencies": {
"undici-types": "~5.26.4" "undici-types": "~5.26.4"
} }
}, },
"node_modules/@types/ws": {
"version": "8.5.10",
"resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.5.10.tgz",
"integrity": "sha512-vmQSUcfalpIq0R9q7uTo2lXs6eGIpt9wtnLdMv9LVpIjCA/+ufZRozlVoVelIYixx1ugCBKDhn89vnsEGOCx9A==",
"dev": true,
"license": "MIT",
"dependencies": {
"@types/node": "*"
}
},
"node_modules/accepts": { "node_modules/accepts": {
"version": "1.3.8", "version": "1.3.8",
"resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.8.tgz", "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.8.tgz",
@@ -65,6 +86,17 @@
"npm": "1.2.8000 || >= 1.4.16" "npm": "1.2.8000 || >= 1.4.16"
} }
}, },
"node_modules/bun-types": {
"version": "1.1.14",
"resolved": "https://registry.npmjs.org/bun-types/-/bun-types-1.1.14.tgz",
"integrity": "sha512-esfxOvECTkjEuUEHBOoOo590Qggf4b9cz5h29AOB2SKt3yZwG3LbAX4iIYwWZX7GnO7vaY5hIdcQygwN0xGdNw==",
"dev": true,
"license": "MIT",
"dependencies": {
"@types/node": "~20.12.8",
"@types/ws": "~8.5.10"
}
},
"node_modules/bytes": { "node_modules/bytes": {
"version": "3.1.2", "version": "3.1.2",
"resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz",
@@ -679,18 +711,6 @@
"node": ">= 0.6" "node": ">= 0.6"
} }
}, },
"node_modules/typescript": {
"version": "5.4.3",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.4.3.tgz",
"integrity": "sha512-KrPd3PKaCLr78MalgiwJnA25Nm8HAmdwN3mYUYZgG/wizIo9EainNVQI9/yDavtVFRN2h3k8uf3GLHuhDMgEHg==",
"bin": {
"tsc": "bin/tsc",
"tsserver": "bin/tsserver"
},
"engines": {
"node": ">=14.17"
}
},
"node_modules/undici-types": { "node_modules/undici-types": {
"version": "5.26.5", "version": "5.26.5",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz",

View File

@@ -2,7 +2,7 @@
"dependencies": { "dependencies": {
"express": "^4.18.2" "express": "^4.18.2"
}, },
"name": "api", "name": "enstrayedapi",
"version": "1.0.0", "version": "1.0.0",
"description": "api.enstrayed.com", "description": "api.enstrayed.com",
"main": "index.js", "main": "index.js",

View File

@@ -1,11 +1,33 @@
const { app, globalConfig } = require("../index.js") 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) => { 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 { } 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} module.exports = {app}