-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (54 loc) · 2.32 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const express = require("express");
const axios = require("axios");
const xml2js = require("xml2js");
require("dotenv").config();
const app = express();
const port = process.env.port || 3000;
app.get("/last-played", async (req, res) => {
try {
const {
user = process.env.USER || "rj",
provider = req.query.provider || "shields",
style = req.query.style || "flat",
color = req.query.color || "red",
label = req.query.label || "Last Played",
icon = req.query.icon || "lastdotfm",
} = req.query;
const API_KEY = process.env.API_KEY;
if (!API_KEY) return res.status(500).send("Missing API Key");
const url = `https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=${user}&limit=1&api_key=${API_KEY}&format=xml`;
const response = await axios.get(url);
const parser = new xml2js.Parser();
parser.parseString(response.data, (err, result) => {
if (err || !result || !result.lfm || !result.lfm.recenttracks || !result.lfm.recenttracks[0] || !result.lfm.recenttracks[0].track || !result.lfm.recenttracks[0].track[0]) {
return res.status(500).send("Error parsing XML or missing data");
}
const track = result.lfm.recenttracks[0].track[0];
const artist = track.artist[0]._ || track.artist[0];
const song = track.name[0];
let badgeUrl = "";
switch (provider.toLowerCase()) {
case "badgen":
badgeUrl = `https://badgen.net/static/${encodeURIComponent(label)}/${encodeURIComponent(song)}%20by%20${encodeURIComponent(artist)}/${color}`;
break;
case "badgers":
badgeUrl = `https://badgers.space/badge/${encodeURIComponent(label)}/${encodeURIComponent(song)}%20by%20${encodeURIComponent(artist)}/${color}`;
break;
case "shields":
default:
badgeUrl = `https://img.shields.io/static/v1?label=${encodeURIComponent(label)}&color=${color}&style=${style}&message=${encodeURIComponent(song)}%20by%20${encodeURIComponent(artist)}&logo=${icon}`;
break;
}
res.redirect(badgeUrl);
});
} catch (error) {
console.error("Error:", error.message);
res.status(500).send("Error fetching data");
}
});
if (!process.env.VERCEL) {
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
}
module.exports = app;