-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
142 lines (106 loc) · 4.05 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
#!/usr/bin/env python
# ------------------------------------------------------------------------------
# app.py
# Author: Alan Ding
# ------------------------------------------------------------------------------
from sys import stderr
from flask import Flask, request, make_response, redirect, render_template, \
url_for
from flask_socketio import SocketIO
from stravalib import Client
from datetime import datetime
from database import db
from helper import *
import json
import os
import requests
# ------------------------------------------------------------------------------
API_BASE_URL = 'https://www.strava.com/api/v3'
LEGENDS_OUT = '22522586'
LEGENDS_BACK = '22522598'
app = Flask(__name__, template_folder='./templates')
app.config['SECRET_KEY'] = 'led leg sonny'
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL')
socketio = SocketIO(app)
db.init_app(app)
# ------------------------------------------------------------------------------
@app.before_first_request
def run_on_start():
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
try:
db.create_all()
except Exception as e:
print('db.create_all() did nothing since the database already exists.',
file=stderr)
@app.before_request
def before_request():
# redirect http to https
if request.url[0:5] != 'https':
return redirect(request.url.replace('http', 'https', 1))
@app.route('/login')
def login():
return make_response(render_template('login.html'))
@app.route('/logout')
def logout():
response = redirect(url_for('login'))
# delete authorization cookie
response.set_cookie('authcookie', '', expires=0)
return response
@app.route('/authorization')
def authorization():
code = request.args.get('code')
client = Client()
token_response = client.exchange_code_for_token(
client_id=os.environ.get('CLIENT_ID'),
client_secret=os.environ.get('CLIENT_SECRET'),
code=code
)
access_token = token_response['access_token']
client = Client(access_token=access_token)
athlete = client.get_athlete()
r_out = requests.get(API_BASE_URL +
'/segments/' + LEGENDS_OUT + '/all_efforts' +
'?access_token=' + access_token)
legends_out_data = r_out.json()
r_back = requests.get(API_BASE_URL +
'/segments/' + LEGENDS_BACK + '/all_efforts' +
'?access_token=' + access_token)
legends_back_data = r_back.json()
if (len(legends_out_data) == 0 or len(legends_back_data) == 0) and \
(int(athlete.id) != 13260725 and int(athlete.id) != 18685549):
return redirect(url_for('not_legend'))
# user is authorized at this point
response = redirect(url_for('chat'))
response.set_cookie('name', athlete.firstname + ' ' + athlete.lastname)
response.set_cookie('authcookie', os.environ.get('VERIFICATION_KEY'))
return response
@app.route('/not-legend')
def not_legend():
html = render_template('not-eligible.html')
return make_response(html)
@app.route('/')
@app.route('/index')
def index():
if request.cookies.get('name') is not None:
return redirect(url_for('chat'))
return redirect(url_for('login'))
@app.route('/chat')
def chat():
if request.cookies.get('authcookie') != os.environ.get('VERIFICATION_KEY'):
return redirect(url_for('login'))
name = request.cookies.get('name')
return make_response(render_template('chat.html', name=name))
@socketio.on('handle_message')
def handle_message(my_json):
print('Data passed to handle_message: ' + str(my_json))
add_message(time=my_json['time'],
sender=my_json['sender'],
message=my_json['message'])
socketio.emit('broadcast_message', my_json)
@socketio.on('get_existing_messages')
def get_existing_messages():
message_log = get_message_log()
socketio.emit('display_existing_messages', message_log)
# ------------------------------------------------------------------------------
if __name__ == '__main__':
socketio.run(app)