-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
41 lines (36 loc) · 1.01 KB
/
main.py
File metadata and controls
41 lines (36 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from icalendar import Calendar
import datetime
import pycurl, json
from StringIO import StringIO
def get_cal(url):
print url
buffer = StringIO()
c = pycurl.Curl()
c.setopt(c.URL, url.rstrip('\n'))
c.setopt(c.WRITEFUNCTION, buffer.write)
c.perform()
c.close()
body = buffer.getvalue()
return body
def handle(context, event):
with open('cals.txt') as f:
content = f.readlines()
calendars = []
for i in content:
data = {}
events = []
body = get_cal(i)
gcal = Calendar.from_ical(body)
for component in gcal.walk():
if component.name == "VCALENDAR":
data['calendar_name'] = component.get('X-WR-CALNAME')
if component.name == "VEVENT":
event = {}
event['summary'] = component.get('summary')
event['date'] = component.get('dtstart').dt.strftime('%Y-%m-%d %H:%M')
event['url'] = component.get('url')
events.append(event)
data['events'] = events
calendars.append(data)
return { 'calendars' : calendars }
print handle(0,0)