apollo/services/scrobblers/maloja-scrobbler.js
2024-12-05 22:34:00 +00:00

46 lines
1.1 KiB
JavaScript

const axios = require("axios");
const Scrobbler = require("./scrobbler");
class MalojaScrobbler extends Scrobbler {
#config = null;
#counter = 0;
constructor(config, logger) {
super(logger);
this.#config = config;
if (!config.name)
throw new Error("Invalid name for Maloja scrobber.");
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}'.`);
}
get counter() {
return this.#counter;
}
get name() {
return this.#config.name;
}
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(duration / 1000),
length: Math.round(song.duration / 1000),
time: Math.round(start / 1000)
});
this.#counter++;
}
}
module.exports = MalojaScrobbler;