Skip to content

Commit

Permalink
Add within_hours and within_days query options
Browse files Browse the repository at this point in the history
Also add dummy /favicon.ico route
  • Loading branch information
ebanner committed Aug 23, 2024
1 parent b6558ca commit 7398dcc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,38 @@ export async function filterEventData(event_data, params) {
}, {});
}

if (params.within_hours) {
const withinHours = parseInt(params.within_hours);
for (const [_, value] of Object.entries(event_data)) {
const upcomingEvents = value.eventSearch.edges.filter(edge => {
const eventDateTimeStr = edge.node.dateTime;
const eventDateTime = new Date(eventDateTimeStr);
const now = new Date();
const timeDifference = eventDateTime - now;
const hoursInMs = withinHours * 60 * 60 * 1000;
return timeDifference > 0 && timeDifference < hoursInMs;
});
value.eventSearch.edges = upcomingEvents;
value.eventSearch.count = upcomingEvents.length;
}
}

if (params.within_days) {
const withinDays = parseInt(params.within_days);
for (const [_, value] of Object.entries(event_data)) {
const upcomingEvents = value.eventSearch.edges.filter(edge => {
const eventDateTimeStr = edge.node.dateTime;
const eventDateTime = new Date(eventDateTimeStr);
const now = new Date();
const timeDifference = eventDateTime - now;
const daysInMs = withinDays * 24 * 60 * 60 * 1000;
return timeDifference > 0 && timeDifference < daysInMs;
});
value.eventSearch.edges = upcomingEvents;
value.eventSearch.count = upcomingEvents.length;
}
}

return event_data;
}

Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export default {
const url = new URL(request.url);
const params = await util.parseQueryParams(url);

if (url.pathname === '/favicon.ico') {
return new Response();
}

const eventData = await util.filterEventData(JSON.parse(await env.kv.get("event_data")), params)

if (url.pathname == '/rss' || url.pathname == '/feed') {
Expand Down

0 comments on commit 7398dcc

Please sign in to comment.