Added buffer to scrobblers.

This commit is contained in:
Tom 2024-12-05 22:34:00 +00:00
parent 934689763b
commit 9335e3e32f
7 changed files with 48 additions and 6 deletions

2
app.js
View File

@ -10,7 +10,7 @@ const Recorder = require("./services/recorder");
const MalojaScrobbler = require("./services/scrobblers/maloja-scrobbler"); 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); const spotify = new SpotifyTracker(config.spotify);
(async () => await spotify.loadCredentials())(); (async () => await spotify.loadCredentials())();
const plex = new PlexTracker(config.plex); const plex = new PlexTracker(config.plex);

7
package-lock.json generated
View File

@ -10,6 +10,7 @@
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"ajv": "^8.17.1", "ajv": "^8.17.1",
"async-await-queue": "^2.1.4",
"axios": "^1.7.8", "axios": "^1.7.8",
"express": "^4.21.1", "express": "^4.21.1",
"express-rate-limit": "^7.4.1", "express-rate-limit": "^7.4.1",
@ -59,6 +60,12 @@
"integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==", "integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==",
"license": "MIT" "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": { "node_modules/asynckit": {
"version": "0.4.0", "version": "0.4.0",
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",

View File

@ -10,6 +10,7 @@
"description": "", "description": "",
"dependencies": { "dependencies": {
"ajv": "^8.17.1", "ajv": "^8.17.1",
"async-await-queue": "^2.1.4",
"axios": "^1.7.8", "axios": "^1.7.8",
"express": "^4.21.1", "express": "^4.21.1",
"express-rate-limit": "^7.4.1", "express-rate-limit": "^7.4.1",

View File

@ -126,7 +126,7 @@ class Recorder {
try { try {
const duration = context.session.playDuration; const duration = context.session.playDuration;
const start = Date.now() - context.session.playDuration - context.session.pauseDuration; 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) { } catch (ex) {
this.#logger.error(ex, "Could not send to maloja."); this.#logger.error(ex, "Could not send to maloja.");
} }

View File

@ -1,10 +1,12 @@
const axios = require("axios"); const axios = require("axios");
const Scrobbler = require("./scrobbler");
class MalojaScrobbler { class MalojaScrobbler extends Scrobbler {
#config = null; #config = null;
#counter = 0; #counter = 0;
constructor(config) { constructor(config, logger) {
super(logger);
this.#config = config; this.#config = config;
if (!config.name) if (!config.name)
@ -12,7 +14,7 @@ class MalojaScrobbler {
if (!config.url) if (!config.url)
throw new Error(`Invalid url for Maloja scrobbler '${this.name}'.`); throw new Error(`Invalid url for Maloja scrobbler '${this.name}'.`);
if (!config.token) 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() { get counter() {
@ -27,6 +29,7 @@ class MalojaScrobbler {
const url = new URL(this.#config.url); const url = new URL(this.#config.url);
url.pathname += "/apis/mlj_1/newscrobble"; url.pathname += "/apis/mlj_1/newscrobble";
url.search = "?key=" + this.#config.token; url.search = "?key=" + this.#config.token;
await axios.post(url.toString(), { await axios.post(url.toString(), {
title: song.name, title: song.name,
album: song.album, album: song.album,

View File

@ -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;

View File

@ -2,7 +2,7 @@ const axios = require("axios");
const config = require("../config/configuration") const config = require("../config/configuration")
const logger = require("./logging"); const logger = require("./logging");
const fs = require("fs/promises"); const fs = require("fs/promises");
const fss = require("fs") const fss = require("fs");
let token = null; let token = null;
let cache = {} let cache = {}