-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fetching termcard from google calendar
- Loading branch information
Showing
5 changed files
with
219 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import ical from "node-ical"; | ||
import type { VEvent } from "node-ical"; | ||
import type { Events } from "$components/events/event"; | ||
import { titleCase } from "title-case"; | ||
|
||
type EventDateTimes = { | ||
date: [number, number, number], | ||
time: [number, number], | ||
duration: [number, number] | ||
}; | ||
|
||
function getEventDateTimes (start: Date, end: Date):EventDateTimes { | ||
const date = start.getDate(); | ||
const month = start.getMonth() + 1; | ||
const year = start.getFullYear(); | ||
const hour = start.getHours(); | ||
const minute = start.getMinutes(); | ||
const durationInMins = Math.round((end.getTime() - start.getTime()) / 60000); | ||
|
||
return { | ||
date: [year, month, date], | ||
time: [hour, minute], | ||
duration: [Math.floor(durationInMins / 60), durationInMins % 60] | ||
} | ||
} | ||
|
||
export async function getEvents<T>(calendarURL: string, start: Date, end: Date, identifyType: (type: string) => T): Promise<Events<T>> { | ||
let events:Events<T> = []; | ||
const rawCalendar = await ical.async.fromURL(calendarURL); | ||
events = Object.values(rawCalendar) | ||
.filter(e => e.type == "VEVENT" && e.start > start && e.start < end) | ||
.map((e:VEvent) => { | ||
const eventDetails = /(?<type>jcr|soc): ?(?<title>[A-Za-z0-9-\/ ]+)/gmi.exec(e.summary).groups; | ||
const { date, time, duration } = getEventDateTimes(e.start, e.end); | ||
|
||
return { | ||
description: titleCase(eventDetails.title), | ||
date, | ||
time, | ||
duration, | ||
location: e.location, | ||
type: identifyType(eventDetails.type) | ||
}; | ||
}); | ||
|
||
console.log(events); | ||
return events; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,14 @@ | ||
import type { Events } from "$components/events/event"; | ||
export enum TermcardEventTypes { | ||
jcr = "jcr", | ||
soc = "society" | ||
} | ||
|
||
const year: number = 2024; | ||
export const term = "Summer"; | ||
export const termcardURL = "https://calendar.google.com/calendar/ical/webmaster%40thejcr.co.uk/public/basic.ics"; | ||
export const startDate = new Date(Date.UTC(2024, 7, 18)); | ||
export const endDate = new Date(Date.UTC(2024, 7, 25)); | ||
|
||
export const term: string = "Easter"; | ||
|
||
export const termcard: Events<"jcr" | "society"> = [ | ||
{ | ||
description: "Spring Has Sprung Bop", | ||
date: [year, 4, 26], | ||
time: [21, 30], | ||
duration: [3, 0], | ||
location: "Buttery", | ||
type: "jcr", | ||
}, | ||
{ | ||
description: "Christ's Maths Society talks", | ||
date: [year, 5, 2], | ||
time: [19, 0], | ||
duration: [1, 0], | ||
location: "Yusuf Hamied Theatre", | ||
type: "society", | ||
}, | ||
{ | ||
description: "Pool Party", | ||
date: [year, 6, 14], | ||
time: [19, 0], | ||
duration: [2, 0], | ||
location: "Malcolm Bowie Bathing Pool", | ||
type: "jcr", | ||
}, | ||
{ | ||
description: "(Bad at) Sports Day", | ||
date: [year, 6, 17], | ||
time: [0, 0], | ||
duration: [24, 0], | ||
location: "TBC", | ||
type: "jcr", | ||
}, | ||
{ | ||
description: "Christ's May Ball", | ||
date: [year, 6, 18], | ||
time: [19, 0], | ||
duration: [5, 0], | ||
location: "Christ's College", | ||
type: "jcr", | ||
}, | ||
]; | ||
export function identifyTermcardEventType (type: string) { | ||
if (type.toLowerCase() == "jcr") return TermcardEventTypes.jcr; | ||
else return TermcardEventTypes.soc; | ||
} |
Oops, something went wrong.