94 lines
2.9 KiB
JavaScript
94 lines
2.9 KiB
JavaScript
const got = require("got");
|
|
const cheerio = require("cheerio");
|
|
const time = require("s-ago");
|
|
|
|
/*
|
|
example of what an object is supposed to look like
|
|
|
|
{
|
|
"cover": "<url to the album cover>",
|
|
"track": {
|
|
"name": "<track title>",
|
|
"url": "<url to track on scrobbler/service>"
|
|
"stream": "<url to stream, if available>"
|
|
},
|
|
"artist": {
|
|
"url": "<url to artist on scrobbler/service>"
|
|
"name": "<artist's name>"
|
|
},
|
|
"album": {
|
|
"url": "<url to album on scrobbler/service>",
|
|
"name": "<name of album>"
|
|
},
|
|
"est-timestamp": "<'live' or est. of how long ago>"
|
|
"timestamp": "<actual timestamp, if provided>"
|
|
}
|
|
*/
|
|
|
|
exports.extract = function(user, cb) {
|
|
got(`https://www.last.fm/user/${user}/partial/recenttracks?ajax=1`, {
|
|
headers: {
|
|
"user-agent": "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0",
|
|
referer: `https://www.last.fm/user/${user}/`,
|
|
"X-Requested-With": "XMLHttpRequest"
|
|
}
|
|
}).then(function(resp) {
|
|
var $ = cheerio.load(resp.body);
|
|
var sb = `table tbody tr:first-of-type`;
|
|
var sd = $(`${sb} .chartlist-timestamp span`).text().replace(/^\s+|\s+$/g, "");
|
|
var ts = (parseInt($(`${sb}`).attr("data-timestamp")) * 1000 || null)
|
|
|
|
if (sd == "Scrobbling now") {
|
|
sd = "live";
|
|
} else if (sd == "" || !sd.includes("ago")) {
|
|
var sd = time(new Date(ts));
|
|
}
|
|
|
|
if ($(`${sb} .chartlist-image a img`).length > 0) {
|
|
var c = $(`${sb} .chartlist-image a img`).attr("src").replace(`64s`, `200s`);
|
|
} else {
|
|
var c = null;
|
|
}
|
|
|
|
if ($(`${sb} .chartlist-image a img`).length > 0) {
|
|
var al = {
|
|
url: (`https://www.last.fm${$(`${sb} .chartlist-image a`).attr("href")}` || null),
|
|
name: ($(`${sb} .chartlist-image a img`).attr("alt") || null)
|
|
};
|
|
} else {
|
|
var al = null;
|
|
}
|
|
|
|
cb(null,{
|
|
scrobbler: {
|
|
name: "Last.fm",
|
|
user_url: `https://www.last.fm/user/${user}/`
|
|
},
|
|
cover: c,
|
|
track: {
|
|
name: ($(`${sb} .chartlist-name a`).attr("title") || null),
|
|
url: (`https://www.last.fm${$(`${sb} .chartlist-name a`).attr("href")}` || null),
|
|
stream: ($(`${sb} .chartlist-play a`).attr("href") || null)
|
|
},
|
|
artist: {
|
|
url: (`https://www.last.fm${$(`${sb} .chartlist-artist a`).attr("href")}` || null),
|
|
name: ($(`${sb} .chartlist-artist a`).attr("title") || null)
|
|
},
|
|
album: al,
|
|
"est-timestamp": sd,
|
|
timestamp: ts
|
|
});
|
|
}).catch(function(err) {
|
|
if (err.response) {
|
|
if (err.response.statusCode == 404) {
|
|
cb("Profile doesn't exist.", null)
|
|
}
|
|
} else {
|
|
if (err.message && err.message == "Cannot read properties of undefined (reading 'replace')") {
|
|
cb("Scrobbling information unavailable for this user currently.", null)
|
|
} else {
|
|
cb(err, null);
|
|
}
|
|
}
|
|
});
|
|
} |