From 9335e3e32f96ddf6f79fd966beb9e65fe80fc470 Mon Sep 17 00:00:00 2001 From: Tom Date: Thu, 5 Dec 2024 22:34:00 +0000 Subject: [PATCH] Added buffer to scrobblers. --- app.js | 2 +- package-lock.json | 7 ++++++ package.json | 1 + services/recorder.js | 2 +- services/scrobblers/maloja-scrobbler.js | 9 ++++--- services/scrobblers/scrobbler.js | 31 +++++++++++++++++++++++++ services/spotify.js | 2 +- 7 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 services/scrobblers/scrobbler.js diff --git a/app.js b/app.js index 55dedd2..9f67bd4 100644 --- a/app.js +++ b/app.js @@ -10,7 +10,7 @@ const Recorder = require("./services/recorder"); const MalojaScrobbler = require("./services/scrobblers/maloja-scrobbler"); -const maloja = new MalojaScrobbler(config.maloja); +const maloja = new MalojaScrobbler(config.maloja, logger); const spotify = new SpotifyTracker(config.spotify); (async () => await spotify.loadCredentials())(); const plex = new PlexTracker(config.plex); diff --git a/package-lock.json b/package-lock.json index bf35427..e7ca721 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "license": "ISC", "dependencies": { "ajv": "^8.17.1", + "async-await-queue": "^2.1.4", "axios": "^1.7.8", "express": "^4.21.1", "express-rate-limit": "^7.4.1", @@ -59,6 +60,12 @@ "integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==", "license": "MIT" }, + "node_modules/async-await-queue": { + "version": "2.1.4", + "resolved": "https://registry.npmjs.org/async-await-queue/-/async-await-queue-2.1.4.tgz", + "integrity": "sha512-3DpDtxkKO0O/FPlWbk/CrbexjuSxWm1CH1bXlVNVyMBIkKHhT5D85gzHmGJokG3ibNGWQ7pHBmStxUW/z/0LYQ==", + "license": "MIT" + }, "node_modules/asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", diff --git a/package.json b/package.json index 7585280..cda9fb1 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "description": "", "dependencies": { "ajv": "^8.17.1", + "async-await-queue": "^2.1.4", "axios": "^1.7.8", "express": "^4.21.1", "express-rate-limit": "^7.4.1", diff --git a/services/recorder.js b/services/recorder.js index 11dd8a6..9b37dcd 100644 --- a/services/recorder.js +++ b/services/recorder.js @@ -126,7 +126,7 @@ class Recorder { try { const duration = context.session.playDuration; const start = Date.now() - context.session.playDuration - context.session.pauseDuration; - await scrobbler.scrobble(context.scrobble, duration, start); + await scrobbler.queue(context.scrobble, duration, start); } catch (ex) { this.#logger.error(ex, "Could not send to maloja."); } diff --git a/services/scrobblers/maloja-scrobbler.js b/services/scrobblers/maloja-scrobbler.js index 763759d..7487e2a 100644 --- a/services/scrobblers/maloja-scrobbler.js +++ b/services/scrobblers/maloja-scrobbler.js @@ -1,10 +1,12 @@ const axios = require("axios"); +const Scrobbler = require("./scrobbler"); -class MalojaScrobbler { +class MalojaScrobbler extends Scrobbler { #config = null; #counter = 0; - constructor(config) { + constructor(config, logger) { + super(logger); this.#config = config; if (!config.name) @@ -12,7 +14,7 @@ class MalojaScrobbler { if (!config.url) throw new Error(`Invalid url for Maloja scrobbler '${this.name}'.`); if (!config.token) - throw new Error(`Invalid token for Maloja scrobbler '${this.name}'.`) + throw new Error(`Invalid token for Maloja scrobbler '${this.name}'.`); } get counter() { @@ -27,6 +29,7 @@ class MalojaScrobbler { const url = new URL(this.#config.url); url.pathname += "/apis/mlj_1/newscrobble"; url.search = "?key=" + this.#config.token; + await axios.post(url.toString(), { title: song.name, album: song.album, diff --git a/services/scrobblers/scrobbler.js b/services/scrobblers/scrobbler.js new file mode 100644 index 0000000..10da729 --- /dev/null +++ b/services/scrobblers/scrobbler.js @@ -0,0 +1,31 @@ +const { Queue } = require("async-await-queue"); + +class Scrobbler { + #queue = null; + #logger = null; + + constructor(logger) { + this.#queue = new Queue(1, 300); + this.#logger = logger; + } + + async queue(media, duration, start) { + const id = Symbol(); + + try { + await this.#queue.wait(id, 0); + + await this.scrobble(media, duration, start); + } catch (ex) { + this.#logger.console.error(media, "Failed to scrobble: " + ex.message); + } finally { + this.#queue.end(id, duration, start); + } + } + + async scrobble(media, duration, start) { + console.log("This should not be running, ever."); + } +} + +module.exports = Scrobbler; \ No newline at end of file diff --git a/services/spotify.js b/services/spotify.js index ee02fbf..c574b64 100644 --- a/services/spotify.js +++ b/services/spotify.js @@ -2,7 +2,7 @@ const axios = require("axios"); const config = require("../config/configuration") const logger = require("./logging"); const fs = require("fs/promises"); -const fss = require("fs") +const fss = require("fs"); let token = null; let cache = {}