forked from softprops/muxup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
142 lines (123 loc) · 4.46 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
from flask import (Flask, flash, session, redirect, url_for,
render_template, request)
from datetime import datetime
from os import getenv
from meetup import (client, url_for_authentication, request_access_token,
refresh_access_token, MeetupNotAuthorized, MeetupBadRequest)
from urllib2 import HTTPError
app = Flask(__name__)
@app.route('/')
def index():
''' let's make this as simple as possbile.
are you or are you not oauthenticated?
'''
if connected():
try:
return render_template('connected.html',
current_user = mu().current_user())
except MeetupNotAuthorized, e:
try:
session['credentials'] = refresh_access_token(session['credentials']['refresh_token'])
index()
except MeetupNotAuthorized, e:
empty_credentials()
flash('User revoked access')
return redirect(url_for('index'))
else:
return render_template('index.html')
@app.route('/topics/<topic>')
def events(topic):
''' fetch of a list of 10 events by topic <topic>
happening around me
'''
if connected():
try:
current_user = mu().current_user()
events = mu().open_events({ 'topic': topic,
'page': 10,
'lat': current_user['lat'],
'lon': current_user['lon'] })
return render_template('events.html', topic = topic,
events = events['results'],
current_user = current_user)
except MeetupNotAuthorized, e:
try:
session['credentials'] = refresh_access_token(session['credentials']['refresh_token'])
events(topic)
except HTTPError:
empty_credentials()
flash('User revoked access')
return redirect(url_for('index'))
else:
return redirect(url_for('index'))
@app.route('/signout')
def signout():
''' let's go back to where we started
'''
empty_credentials()
flash('Signed out')
return redirect(url_for('index'))
@app.route('/connect')
def connect():
''' let's connect with meetup.com
'''
return redirect(url_for_authentication(url_for('auth', _external = True)))
@app.route('/auth')
def auth():
''' meetup.com will redirect the user here after
user was prompted for authentication.
if the user authorized this application, we
should request an access token
'''
if request.args.get('error'):
return redirect(url_for('denied'))
else:
code, state = map(lambda k: request.args.get(k), ['code', 'state'])
session['credentials'] = request_access_token(
code,
url_for('auth', _external = True))
flash('Get muxin.')
return redirect(url_for('index'))
@app.route('/denied')
def denied():
return render_template('denied.html')
@app.errorhandler(404)
def not_found(error):
return render_template('not_found.html')
@app.errorhandler(500)
def server_error(error):
return render_template('app_error.html',
error = 'Server error %s' % error)
@app.errorhandler(MeetupNotAuthorized)
def meetup_not_authorized(error):
try:
fresh = refresh_access_token(session['credentials']['refresh_token'])
session['credentials'] = fresh
return render_template('app_error.html', error = 'had to freshin up')
except Exception, e:
empty_credentials()
flash('User revoked access')
return redirect(url_for('index'))
@app.errorhandler(MeetupBadRequest)
def meetup_bad_request(error):
return render_template('app_error.html', error = error)
@app.template_filter('millidate')
def millidate_filter(t):
return datetime.fromtimestamp(t/1000).strftime('%a %b %d @ %I:%M%p')
app.secret_key = getenv(
'COOKIE_SECRET',
'\xd1\x9fQ=\xd7:\x89|\xce\x02\x93\xb5O\x1a\x8e\xf1\xccy\x92tu\\PF')
# helpers
def empty_credentials():
session.pop('credentials', None)
def connected():
''' return True if the current user is connected
to meetup, otherwise return False
'''
return 'credentials' in session
def mu():
''' shorthand for getting a configured meetup client
'''
return client(session['credentials']['access_token'])
if __name__ == '__main__':
app.run()