feat: update now page to use latest albums

This commit is contained in:
Devin Haska 2024-02-26 14:36:55 -08:00
parent 03c85cf276
commit 169858c05a
3 changed files with 62 additions and 15 deletions

View file

@ -10,6 +10,35 @@ dayjs.extend(relativeTime);
const lastFmApiKey = process.env.LAST_FM_API_KEY;
const baseUrl = "http://ws.audioscrobbler.com";
const username = "wonderfulfrog";
const fetchRecentAlbums = async (period = "7day") => {
const path = `/2.0/?method=user.gettopalbums&user=${username}&api_key=${lastFmApiKey}&format=json`;
const url = `${baseUrl}${path}&period=${period}`;
const response = await EleventyFetch(url, { duration: "7d", type: "json" });
const albums = response.topalbums.album.slice(0, 8);
const recentAlbums = albums.map((album) => {
const extraLargeImage = album.image.find(
(img) => img.size === "extralarge",
);
const imageUrl = extraLargeImage ? extraLargeImage["#text"] : "";
return {
artist: album.artist.name,
artistMbid: album.artist.mbid,
album: album.name,
albumMbid: album.mbid,
playcount: album.playcount,
url: album.url,
imageUrl,
};
});
return recentAlbums;
};
const fetchRecentTracks = async () => {
const url = `http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=wonderfulfrog&api_key=${lastFmApiKey}&format=json`;
@ -31,4 +60,12 @@ const fetchRecentTracks = async () => {
return recentTracks;
};
module.exports = fetchRecentTracks;
module.exports = async function () {
const recentTracks = await fetchRecentTracks();
const recentAlbums = await fetchRecentAlbums();
return {
recentTracks,
recentAlbums,
};
};