-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
159 lines (108 loc) · 4.3 KB
/
app.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import os
import json
import pytz
import requests
import icalendar
from icalendar import Calendar, Event
from datetime import datetime, timedelta
from flask import Flask, make_response
ICS_FILENAME = 'meetup.ics'
OUTPUT_TIMEZONE = 'America/New_York'
MEETUP_KEY = os.environ.get('MEETUP_KEY', None)
DEBUG = os.environ.get('DEBUG', False)
app = Flask(__name__)
# cached variable of icalendar string
ICAL_FEED = None
# timestamp of the last time we requested meetup
last_fetched = None
if DEBUG:
app.logger.warning('DEBUGGING ENABLED')
def fetch_groups():
payload = {'key': MEETUP_KEY}
r = requests.get('http://api.meetup.com/self/groups', params = payload)
groups = None
if r.status_code != 200:
app.logger.warning('FAILED REQUEST ' + str(r) + r.text)
groups_obj = json.loads(r.text)
return groups_obj
def fetch_events(groups):
events_obj = []
for g in groups:
groupname = g.get('urlname')
payload = {'key': MEETUP_KEY}
r = requests.get('http://api.meetup.com/' + groupname + '/events', params = payload)
events = json.loads(r.text)
for event in events:
events_obj.append(event)
# if we're debugging, we should only take the
# first few events so we don't bother meetup too much.
if DEBUG: break
return events_obj
def convert_event_obj_to_ical(e):
# note : e is an event object
ret = ''
# dict_keys(['created', 'duration', 'id', 'name', 'rsvp_limit',
# 'date_in_series_pattern', 'status', 'time', 'local_date',
# 'local_time', 'updated', 'utc_offset', 'waitlist_count',
# 'yes_rsvp_count', 'venue', 'group', 'link', 'description', 'visibility'])
# get our event details out of the meetup object
time = int(e.get('time')) / 1000 # meetup api gives time in milliseconds since epoch
duration = e.get('duration', None)
day = e.get('local_date')
venue = e.get('venue')
link = e.get('link')
# create address string for the event
address_string = ', '.join([venue.get('address_1', ''), venue.get('city', ''), venue.get('state', ''), venue.get('zip', '')])
address_string = address_string[0: len(address_string)]
start_time_object = datetime.fromtimestamp(time, tz=pytz.timezone('UTC'))
end_time_object = start_time_object + timedelta(hours=3) if duration is None else start_time_object + timedelta(milliseconds=duration)
event = icalendar.Event()
event.add('summary', e.get('name')) # 'summary' is the event title
event.add('description', e.get('description') + '\n' + link)
event.add('dtstart', start_time_object)
event.add('dtend', end_time_object)
event.add('location', address_string)
ret = event
return ret
def ical_convert(events):
ret = ''
cal = icalendar.Calendar()
cal.add('prodid', '-//Meetup Events Export//mxm.dk//')
cal.add('version', '2.0')
cal.add('X-WR-TIMEZONE', OUTPUT_TIMEZONE)
for e in events:
event = convert_event_obj_to_ical(e)
cal.add_component(event)
ical_string = cal.to_ical(sorted=False)
ret = ical_string
return ret
def fetch_feed():
global ICAL_FEED
global last_fetched
# is our data over a day old?
# assumes both meetups, and your life, are planned over a day in advance
old_data = False if last_fetched is None else datetime.now() - timedelta(hours=24) >= last_fetched
if ICAL_FEED is None or last_fetched is None or old_data :
app.logger.warning("ICAL_FEED is either old, or nonexistent, fetching new data.")
ICAL_FEED = ical_convert(fetch_events(fetch_groups()))
last_fetched = datetime.now()
else:
app.logger.warning("ICAL_FEED is cached, returning cached copy")
return ICAL_FEED
# health check
@app.route("/")
def hello():
app.logger.warning('Health Check; Hello World.')
return "Hello World!"
@app.route('/calendar/')
def calendar():
# Get the calendar data
# turn calendar data into a response
response = make_response(fetch_feed())
response.headers["Content-Disposition"] = "attachment; filename="+ICS_FILENAME
return response
# for quick local testing
if __name__ == "__main__":
if not MEETUP_KEY: exit(3)
ical_string = ical_convert(fetch_events(fetch_groups()))
print(ical_string)