diff --git a/.gitignore b/.gitignore
index b068519..c4978fe 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,6 @@
node_modules/
config.json
bun.lockb
-GITVERSION
\ No newline at end of file
+GITVERSION
+todo.txt
+proto.js
\ No newline at end of file
diff --git a/config.example.json b/config.example.json
index 2192d6e..7725a1f 100644
--- a/config.example.json
+++ b/config.example.json
@@ -8,12 +8,6 @@
"host": "hazeldale:5984",
"authorization": ""
},
-
- "cider": {
- "targetHosts": ["localhost:10769"],
-
- "authKeysDoc": "cider"
- },
"mailjet": {
"apiKey": "",
@@ -23,14 +17,25 @@
"authKeysDoc": "mailjet"
},
- "etyd": {
- "randomHexLength": 6,
- "authKeyInDb": "apiAuthKeys.etyd"
- },
-
"blog": {
"postsDirectory": "C:/Users/natha/Downloads/proto/posts",
"postsDirUrl": "/posts"
+ },
+
+ "nowplaying": {
+ "lastfm": {
+ "apiKey": "",
+ "target": "enstrayed"
+ },
+ "jellyfin": {
+ "apiKey": "",
+ "host": "",
+ "target": ""
+ },
+ "cider": {
+ "apiKeys": [],
+ "hosts": []
+ }
}
}
\ No newline at end of file
diff --git a/liberals/nowplaying.js b/liberals/nowplaying.js
new file mode 100644
index 0000000..ab7ec30
--- /dev/null
+++ b/liberals/nowplaying.js
@@ -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": `
I'm listening to
${response.recenttracks.track[0].name} by ${response.recenttracks.track[0].artist["#text"]}
from ${response.recenttracks.track[0].album["#text"]}
View on Last.fm `
+ }
+ }
+ }
+ })
+}
+
+module.exports = { queryLastfm }
\ No newline at end of file
diff --git a/package-lock.json b/package-lock.json
index 43da3b4..0b770df 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -9,22 +9,43 @@
"version": "1.0.0",
"license": "UNLICENSED",
"dependencies": {
- "express": "^4.18.2",
- "typescript": "^5.4.3"
+ "express": "^4.18.2"
},
"devDependencies": {
+ "@types/bun": "^1.0.12",
"@types/node": "^20.12.3"
}
},
- "node_modules/@types/node": {
- "version": "20.12.3",
- "resolved": "https://registry.npmjs.org/@types/node/-/node-20.12.3.tgz",
- "integrity": "sha512-sD+ia2ubTeWrOu+YMF+MTAB7E+O7qsMqAbMfW7DG3K1URwhZ5hN1pLlRVGbf4wDFzSfikL05M17EyorS86jShw==",
+ "node_modules/@types/bun": {
+ "version": "1.1.5",
+ "resolved": "https://registry.npmjs.org/@types/bun/-/bun-1.1.5.tgz",
+ "integrity": "sha512-7RprVDMF+1o+EWSo7F1+iJpkfNz+Ikw9K//vwambcY+D1QHXfb9l7jWY1hSBfuFEkW9yFAhkMzP2uTi1pQXoqw==",
"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": {
"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": {
"version": "1.3.8",
"resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.8.tgz",
@@ -65,6 +86,17 @@
"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": {
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz",
@@ -679,18 +711,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": {
"version": "5.26.5",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz",
diff --git a/package.json b/package.json
index 01392d7..633d388 100644
--- a/package.json
+++ b/package.json
@@ -2,7 +2,7 @@
"dependencies": {
"express": "^4.18.2"
},
- "name": "api",
+ "name": "enstrayedapi",
"version": "1.0.0",
"description": "api.enstrayed.com",
"main": "index.js",
diff --git a/routes/nowplaying.js b/routes/nowplaying.js
index 2254ab3..ab7e75b 100644
--- a/routes/nowplaying.js
+++ b/routes/nowplaying.js
@@ -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": `I'm not currently listening to anything.`
+}
app.get("/nowplaying", (rreq,rres) => {
- if (rreq.query.format === "html") {
- rres.send("The /nowplaying endpoint is currently under construction.")
+
+ 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}
\ No newline at end of file