2024-12-05 12:32:37 -05:00
|
|
|
const axios = require("axios");
|
2024-12-05 17:34:00 -05:00
|
|
|
const Scrobbler = require("./scrobbler");
|
2024-12-05 12:32:37 -05:00
|
|
|
|
2024-12-05 17:34:00 -05:00
|
|
|
class MalojaScrobbler extends Scrobbler {
|
2024-12-05 12:32:37 -05:00
|
|
|
#config = null;
|
|
|
|
#counter = 0;
|
|
|
|
|
2024-12-05 17:34:00 -05:00
|
|
|
constructor(config, logger) {
|
|
|
|
super(logger);
|
2024-12-05 12:32:37 -05:00
|
|
|
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}'.`);
|
2024-12-05 12:32:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
get counter() {
|
|
|
|
return this.#counter;
|
|
|
|
}
|
|
|
|
|
|
|
|
get name() {
|
|
|
|
return this.#config.name;
|
|
|
|
}
|
|
|
|
|
2024-12-05 15:27:09 -05:00
|
|
|
async scrobble(song, duration, start) {
|
2024-12-05 12:32:37 -05:00
|
|
|
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
|
|
|
|
2024-12-05 12:32:37 -05:00
|
|
|
await axios.post(url.toString(), {
|
|
|
|
title: song.name,
|
|
|
|
album: song.album,
|
|
|
|
artists: song.artists,
|
2024-12-05 15:27:09 -05:00
|
|
|
duration: Math.round(duration / 1000),
|
2024-12-05 12:32:37 -05:00
|
|
|
length: Math.round(song.duration / 1000),
|
2024-12-05 15:27:09 -05:00
|
|
|
time: Math.round(start / 1000)
|
2024-12-05 12:32:37 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
this.#counter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = MalojaScrobbler;
|