-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathNosedive.py
195 lines (161 loc) · 5.52 KB
/
Nosedive.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from flask import redirect, url_for, session, request, render_template, jsonify
from flask_oauth import OAuth
from random import randint
from algorithm import User, app, db, fb
import hashlib
DEBUG = True
oauth = OAuth()
facebook = oauth.remote_app('facebook',
base_url='https://graph.facebook.com/',
request_token_url=None,
access_token_url='/oauth/access_token',
authorize_url='https://www.facebook.com/dialog/oauth',
consumer_key="YOUR KEY HERE",
consumer_secret="YOUR SECRET HERE",
request_token_params={
'scope': 'user_posts, public_profile'}
)
@app.route('/')
def index():
return render_template("login.html")
@app.route('/login')
def login():
return facebook.authorize(callback=url_for('facebook_authorized',
next=request.args.get('next') or request.referrer or None,
_external=True))
@app.route('/login/authorized')
@facebook.authorized_handler
def facebook_authorized(resp):
if resp is None:
return 'Access denied: reason=%s error=%s' % (
request.args['error_reason'],
request.args['error_description']
)
session['oauth_token'] = (resp['access_token'], '')
try:
user_id = fb(resp['access_token'], "me")["id"]
session['user_id'] = (str(user_id), '')
except:
return authorise()
return redirect(url_for('profile', user_id=user_id))
# return "Base Score: " + str(me.naive_score) + ", Bonus: " + str(me.bonus)
# return 'Logged in as id=%s name=%s redirect=%s' % \
# (me.data['id'], me.data['name'], request.args.get('next'))
@app.route('/profile/<int:user_id>')
def profile(user_id):
try:
user = User.query.get(md5(user_id))
print(user)
except:
user = False
if not user:
try:
token = get_facebook_oauth_token()
except:
return authorise()
me = User(token, user_id)
return render_template("incomplete_profile.html", user=me)
else:
token = get_facebook_oauth_token()
recalculate = False
if token:
try:
visit_user_id = fb(token, "me")["id"]
if str(user_id) == visit_user_id:
recalculate = True
except:
pass
return render_template("profile.html", user=user, recalculate=recalculate, anon=True)
@app.route('/anon/<user_id_hash>')
def anon_profile(user_id_hash):
try:
user = User.query.get(user_id_hash)
except:
user = False
if not user:
return authorise()
else:
token = get_facebook_oauth_token()
recalculate = False
if token:
try:
visit_user_id = fb(token, "me")["id"]
if str(user.user_id) == visit_user_id:
recalculate = True
except:
pass
user.first_name = "Anon"
user.last_name = ""
user.name = "Anon"
user.avatar = "https://randomuser.me/api/portraits/lego/" + str(randint(1, 5)) + ".jpg"
return render_template("profile.html", user=user, recalculate=recalculate, anon=False)
@app.route('/go_anon/<int:user_id>')
def go_anon(user_id):
token = get_facebook_oauth_token()
if token:
try:
visit_user_id = fb(token, "me")["id"]
try:
user = User.query.get(md5(user_id))
except:
return authorise()
if str(user_id) == visit_user_id:
return redirect("/anon/" + user.hash_id)
except:
return authorise()
@app.route('/calculate/')
def calculate():
try:
token = get_facebook_oauth_token()
me = User(token)
me.get_scores()
try:
db.session.add(me)
db.session.commit()
except:
pass
rounded_final = round(float(me.final_score), 3)
rounded_bonus = round(float(me.bonus), 3)
return jsonify(final_score=str(rounded_final),
bonus=str(rounded_bonus),
num=rounded_final*10000,
int=str(rounded_final)[:3], decimal=str(rounded_final)[3:])
except: # If anything goes wrong, reauthorise
return authorise()
@app.route('/remove/<int:user_id>')
def remove_entry(user_id):
try:
token = get_facebook_oauth_token()
me = fb(token, "me")
if str(user_id) == me["id"]:
User.query.filter_by(id=user_id).delete()
db.session.commit()
return authorise()
except: # If anything goes wrong, reauthorise
return authorise()
@app.errorhandler(404)
def page_not_found(e):
print e
return redirect(url_for('index'))
@app.errorhandler(500)
def internal_error(e):
print e
return redirect(url_for('index'))
@facebook.tokengetter
def get_facebook_oauth_token():
try:
return session.get('oauth_token')[0]
except:
return 0
def get_facebook_session_user_id():
try:
return session.get('user_id')[0]
except:
return 0
def authorise():
return redirect(url_for('login'))
def md5(user_id):
return hashlib.md5(str(user_id)).hexdigest()
if __name__ == '__main__':
db.create_all()
app.run(debug=False, host="0.0.0.0", port="5001")