apollo/services/scrobblers/maloja-scrobbler.js

46 lines
1.1 KiB
JavaScript
Raw Permalink Normal View History

const axios = require("axios");
2024-12-05 17:34:00 -05:00
const Scrobbler = require("./scrobbler");
2024-12-05 17:34:00 -05:00
class MalojaScrobbler extends Scrobbler {
#config = null;
#counter = 0;
2024-12-05 17:34:00 -05:00
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)
2024-12-05 17:34:00 -05:00
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;
2024-12-05 17:34:00 -05:00
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;