Compare commits
2 Commits
0bfbc36952
...
9335e3e32f
Author | SHA1 | Date | |
---|---|---|---|
9335e3e32f | |||
934689763b |
2
app.js
2
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);
|
||||
|
7
package-lock.json
generated
7
package-lock.json
generated
@ -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",
|
||||
|
@ -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",
|
||||
|
@ -62,7 +62,7 @@ class Recorder {
|
||||
this.#sessions.add(session);
|
||||
}
|
||||
|
||||
return { session, media, tracker, extraDuration: 0 }
|
||||
return { session, media, tracker, extraDuration: 0, scrobble: null }
|
||||
}
|
||||
|
||||
#listen(context, timestamp) {
|
||||
@ -88,8 +88,12 @@ class Recorder {
|
||||
|
||||
const canScrobble = this.#canScrobble(session, current, previous);
|
||||
if (canScrobble || current.id != previous.id) {
|
||||
context.extraDuration = Math.min(current.progress, timeDiff - (previous.duration - previous.progress));
|
||||
context.extraDuration = Math.max(Math.min(current.progress, timeDiff - (previous.duration - previous.progress)), 0);
|
||||
context.extraDuration += Math.max(0, session.playDuration - previous.duration);
|
||||
session.lastScrobbleTimestamp = timestamp;
|
||||
|
||||
if (canScrobble)
|
||||
context.scrobble = previous;
|
||||
}
|
||||
|
||||
session.lastUpdateTimestamp = timestamp;
|
||||
@ -106,11 +110,11 @@ class Recorder {
|
||||
const newPlayback = current == null || current.progress < previous.progress;
|
||||
const canBeScrobbled = session.playDuration > scrobbleDuration * 1000 || session.playDuration / previous.duration > scrobblePercent / 100.0;
|
||||
|
||||
return newPlayback && canBeScrobbled;
|
||||
return newPlayback && canBeScrobbled || session.playDuration >= previous.duration;
|
||||
}
|
||||
|
||||
async #scrobble(context) {
|
||||
this.#logger.info(context, "Scrobble");
|
||||
this.#logger.info(context.scrobble, "Scrobble");
|
||||
|
||||
for (var scrobblerName of context.tracker.scrobblerNames) {
|
||||
const scrobbler = this.#scrobblers.find(s => s.name == scrobblerName);
|
||||
@ -120,7 +124,9 @@ class Recorder {
|
||||
}
|
||||
|
||||
try {
|
||||
await scrobbler.scrobble(context.media, Date.now() - Math.min(context.media.duration, context.session.playDuration));
|
||||
const duration = context.session.playDuration;
|
||||
const start = Date.now() - context.session.playDuration - context.session.pauseDuration;
|
||||
await scrobbler.queue(context.scrobble, duration, start);
|
||||
} catch (ex) {
|
||||
this.#logger.error(ex, "Could not send to maloja.");
|
||||
}
|
||||
|
@ -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() {
|
||||
@ -23,17 +25,18 @@ class MalojaScrobbler {
|
||||
return this.#config.name;
|
||||
}
|
||||
|
||||
async scrobble(song, progress, start) {
|
||||
async scrobble(song, duration, start) {
|
||||
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,
|
||||
artists: song.artists,
|
||||
duration: Math.round(progress / 1000),
|
||||
duration: Math.round(duration / 1000),
|
||||
length: Math.round(song.duration / 1000),
|
||||
//time: start
|
||||
time: Math.round(start / 1000)
|
||||
});
|
||||
|
||||
this.#counter++;
|
||||
|
31
services/scrobblers/scrobbler.js
Normal file
31
services/scrobblers/scrobbler.js
Normal 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;
|
@ -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 = {}
|
||||
|
Loading…
Reference in New Issue
Block a user