- offer to create tag when none is found while adding a link from cli - fix tag display in search
87 lines
2.1 KiB
JavaScript
87 lines
2.1 KiB
JavaScript
import EleventyFetch from "@11ty/eleventy-fetch";
|
|
|
|
const { POSTGREST_URL, POSTGREST_API_KEY } = process.env;
|
|
|
|
const fetchDataFromView = async (viewName) => {
|
|
try {
|
|
return await EleventyFetch(`${POSTGREST_URL}/${viewName}?select=*`, {
|
|
duration: "1h",
|
|
type: "json",
|
|
fetchOptions: {
|
|
method: "GET",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Bearer ${POSTGREST_API_KEY}`
|
|
}
|
|
}
|
|
});
|
|
} catch (error) {
|
|
console.error(`Error fetching data from view ${viewName}:`, error);
|
|
return [];
|
|
}
|
|
};
|
|
|
|
export default async function fetchMusicData() {
|
|
try {
|
|
const [
|
|
recentTracks,
|
|
weekTracks,
|
|
weekArtists,
|
|
weekAlbums,
|
|
weekGenres,
|
|
monthTracks,
|
|
monthArtists,
|
|
monthAlbums,
|
|
monthGenres
|
|
] = await Promise.all([
|
|
fetchDataFromView("recent_tracks"),
|
|
fetchDataFromView("week_tracks"),
|
|
fetchDataFromView("week_artists"),
|
|
fetchDataFromView("week_albums"),
|
|
fetchDataFromView("week_genres"),
|
|
fetchDataFromView("month_tracks"),
|
|
fetchDataFromView("month_artists"),
|
|
fetchDataFromView("month_albums"),
|
|
fetchDataFromView("month_genres")
|
|
]);
|
|
|
|
return {
|
|
recent: recentTracks,
|
|
week: {
|
|
tracks: weekTracks,
|
|
artists: weekArtists,
|
|
albums: weekAlbums,
|
|
genres: weekGenres,
|
|
totalTracks: weekTracks.reduce((acc, track) => acc + track.plays, 0).toLocaleString("en-US")
|
|
},
|
|
month: {
|
|
tracks: monthTracks,
|
|
artists: monthArtists,
|
|
albums: monthAlbums,
|
|
genres: monthGenres,
|
|
totalTracks: monthTracks
|
|
.reduce((acc, track) => acc + track.plays, 0)
|
|
.toLocaleString("en-US")
|
|
}
|
|
};
|
|
} catch (error) {
|
|
console.error("Error fetching and processing music data:", error);
|
|
return {
|
|
recent: [],
|
|
week: {
|
|
tracks: [],
|
|
artists: [],
|
|
albums: [],
|
|
genres: [],
|
|
totalTracks: "0"
|
|
},
|
|
month: {
|
|
tracks: [],
|
|
artists: [],
|
|
albums: [],
|
|
genres: [],
|
|
totalTracks: "0"
|
|
}
|
|
};
|
|
}
|
|
}
|