add: last.fm status integration

This commit is contained in:
trisua 2025-04-26 19:23:30 -04:00
parent 3e2bdceb99
commit 0765156697
17 changed files with 397 additions and 3 deletions

View file

@ -578,3 +578,109 @@
},
);
})();
(() => {
const self = reg_ns("last_fm");
self.define("api", async (_, method, data) => {
return JSON.parse(
(
await (
await fetch(
"/api/v1/auth/user/connections/last_fm/api_proxy",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
method,
data,
}),
},
)
).json()
).payload,
);
});
self.define("create_connection", (_, client_id) => {
fetch("/api/v1/auth/user/connections/last_fm", {
method: "POST",
})
.then((res) => res.json())
.then(async (_) => {
const params = new URLSearchParams();
params.append("api_key", client_id);
params.append(
"cb",
`${window.location.origin}/auth/connections_link/LastFm`,
);
window.open(`https://last.fm/api/auth?${params.toString()}`);
window.location.reload();
});
});
self.define("get_session", async ({ $ }, token) => {
return await $.api("auth.getSession", {
token,
});
});
self.define("get_playing", async ({ $ }, user, session_token) => {
// <https://lastfm-docs.github.io/api-docs/user/getRecentTracks/>
return (
await $.api("user.getRecentTracks", {
user,
sk: session_token,
limit: "1",
extended: "1",
})
).recenttracks.track[0];
});
self.define("publish_playing", async (_, playing) => {
if (!playing || !playing["@attr"] || !playing["@attr"].nowplaying) {
return await trigger("connections::push_con_state", [
"LastFm",
{
external_urls: {},
data: {},
},
]);
}
if (
window.localStorage.getItem("atto:connections.last_fm/name") ===
playing.name
) {
// item already pushed to connection, no need right now
return;
}
window.localStorage.setItem(
"atto:connections.last_fm/name",
playing.name,
);
return await trigger("connections::push_con_state", [
"LastFm",
{
external_urls: {
track: playing.url,
artist: playing.artist.url,
track_img: playing.image[2]["#text"],
},
data: {
id: playing.mbid,
// track
track: playing.name,
artist: playing.artist.name,
album: playing.album["#text"],
},
},
]);
});
})();