Last active
August 23, 2021 19:02
-
-
Save gixxerblade/5458f00349f5f6cf8985b6eac1889394 to your computer and use it in GitHub Desktop.
Get public events from a subscribed Meetup Google calendar ics ical file
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
import fetch from 'node-fetch'; | |
interface Events { | |
startTime: Date; | |
endTime: Date; | |
url: string; | |
description: string; | |
location: string; | |
title: string; | |
id: string; | |
} | |
enum DateTZIdx { | |
T = 8, | |
Z = 15, | |
} | |
const parseDate = (date: string): Date => { | |
const year = parseInt(date.substr(0, 4)); | |
const month = parseInt(date.substr(4, 2), 10) - 1; | |
const day = parseInt(date.substr(6, 2)); | |
const hour = parseInt(date.substr(9, 2)); | |
const minute = parseInt(date.substr(11, 2)); | |
const second = parseInt(date.substr(13, 2)); | |
return new Date(Date.UTC(year, month, day, hour, minute, second)); | |
}; | |
const validateDate = (val: string): boolean => { | |
if (val.length !== 16) return false; | |
if (val[DateTZIdx.T] !== 'T') return false; | |
if (val[DateTZIdx.Z] !== 'Z') return false; | |
val | |
.split('') | |
.filter((nums, i) => nums !== DateTZIdx[i]) | |
.every((num) => !isNaN(parseInt(num))); | |
return true; | |
}; | |
const main = async () => { | |
const result = await fetch( | |
'https://calendar.google.com/calendar/ical/<YOUR GOOGLE CALENDAR ID>/public/basic.ics' | |
); | |
const data = await result.text(); | |
const dataArr = data.split('\r\n'); | |
const event: { [key: string]: unknown } = {}; | |
const events: {}[] = []; | |
const eventMap: { [key: string]: string } = { | |
DTSTART: 'startTime', | |
DTEND: 'endTime', | |
URL: 'url', | |
DESCRIPTION: 'description', | |
LOCATION: 'location', | |
SUMMARY: 'title', | |
UID: 'id', | |
}; | |
dataArr.forEach((line: string) => { | |
const colon = line.indexOf(':'); | |
let key, value; | |
[key, value] = [line.slice(0, colon), line.slice(colon + 1)]; | |
if (key === 'END') { | |
events.push({ ...event }); | |
} else if (eventMap.hasOwnProperty(key)) { | |
if(validateDate(value)) value = parseDate(value) | |
event[eventMap[key]] = value as unknown as Events; | |
} | |
}); | |
console.log(events); | |
return events; | |
}; | |
main(); | |
console.log(parseDate('20210925T123000Z')); | |
console.log(parseDate('20210925T143000Z')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment