coryd.dev/src/data/albumReleases.js

55 lines
1.5 KiB
JavaScript

import EleventyFetch from '@11ty/eleventy-fetch';
const { POSTGREST_URL, POSTGREST_API_KEY } = process.env;
const fetchAlbumReleases = async () => {
try {
const data = await EleventyFetch(`${POSTGREST_URL}/optimized_album_releases`, {
duration: '1d',
type: 'json',
fetchOptions: {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${POSTGREST_API_KEY}`
}
}
});
const pacificNow = new Date().toLocaleString('en-US', {
timeZone: 'America/Los_Angeles'
});
const pacificDate = new Date(pacificNow);
pacificDate.setHours(0, 0, 0, 0);
const todayTimestamp = pacificDate.getTime() / 1000;
const all = data
.map((album) => {
const releaseDate = new Date(album.release_timestamp * 1000);
return {
...album,
description: album.artist?.description || 'No description',
date: releaseDate.toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric'
})
};
})
.sort((a, b) => a.release_timestamp - b.release_timestamp);
const upcoming = all.filter(
(album) => album.release_timestamp > todayTimestamp && album.total_plays === 0
);
return { all, upcoming };
} catch (error) {
console.error('Error fetching and processing album releases:', error);
return { all: [], upcoming: [] };
}
};
export default async function () {
return await fetchAlbumReleases();
}