71 lines
2.1 KiB
JavaScript
71 lines
2.1 KiB
JavaScript
const express = require("express");
|
|
const app = express();
|
|
const multer = require("multer");
|
|
const upload = multer({dest: "/tmp"});
|
|
const scribble = require("scribble");
|
|
const xmljs = require("xml-js");
|
|
const config = require("./config.json");
|
|
|
|
const scr = new scribble(config.scrobbler.key, config.scrobbler.secret, config.scrobbler.username, config.scrobbler.password);
|
|
|
|
app.listen(config.port, function() {
|
|
console.log(`Listening on port ${config.port}`);
|
|
});
|
|
|
|
app.get("/", function(req, res) {
|
|
res.send("meow")
|
|
});
|
|
|
|
app.post("/", upload.single("thumb"), function(req, res) {
|
|
let b = req.body.payload || req.body;
|
|
b = JSON.parse(b);
|
|
|
|
if (b.event !== "media.scrobble" || !isPlayerAccepted(b.Player) || !isUserAccepted(b.Account) || !isLibAccepted(b.Metadata.librarySectionID)) {
|
|
if (config.debug == true) console.log(b.Player.uuid, b.Account.title, b.Metadata.librarySectionID, b.event);
|
|
res.sendStatus(200);
|
|
return;
|
|
} else {
|
|
let scrobble = {
|
|
artist: b.Metadata.grandparentTitle,
|
|
album: b.Metadata.parentTitle,
|
|
track: b.Metadata.title
|
|
};
|
|
|
|
scr.Scrobble(scrobble, function(data) {
|
|
data = xmljs.xml2js(data, {compact: false}).elements[0];
|
|
if (data.attributes?.status == "ok") {
|
|
console.log(`Scrobbled "${scrobble.track}" by ${scrobble.artist} successfully.`);
|
|
} else {
|
|
console.log(`There was an error scrobbling "${scrobble.track}" by ${scrobble.artist}, ${JSON.stringify(data, null, 2)}`);
|
|
}
|
|
res.sendStatus(200);
|
|
});
|
|
}
|
|
})
|
|
|
|
function isPlayerAccepted(player) {
|
|
let accepted = config["accepted-players"];
|
|
if (accepted.length == 0) return true;
|
|
for (let a in accepted) {
|
|
if (player.uuid == accepted[a]) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function isUserAccepted(user) {
|
|
let accepted = config["accepted-users"];
|
|
if (accepted.length == 0) return true;
|
|
for (let a in accepted) {
|
|
if (user.title == accepted[a]) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function isLibAccepted(libId) {
|
|
let accepted = config["accepted-libraries"];
|
|
if (accepted.length == 0) return true;
|
|
for (let a in accepted) {
|
|
if (libId == accepted[a]) return true;
|
|
}
|
|
return false;
|
|
} |