-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathflask_app.py
203 lines (151 loc) · 5.61 KB
/
flask_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
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
193
194
195
196
197
198
199
200
201
202
203
import os
import json
import git
import pandas as pd
from flask import render_template
from flask import Flask, request
from http import HTTPStatus
from src.auth import signature_verified
from src.update import update
from src.utils import json_to_dataframe, create_climber_key, split_name
THIS_FOLDER = os.path.dirname(os.path.abspath(__file__))
app = Flask("Hardest Climbs", template_folder=THIS_FOLDER + "/templates")
with open(os.path.join(THIS_FOLDER, 'data/lead.json'), "r", encoding='utf-8') as f:
lead_data = json.load(f)
with open(os.path.join(THIS_FOLDER, 'data/boulder.json'), "r", encoding='utf-8') as f:
boulder_data = json.load(f)
lead = json_to_dataframe(json_data=lead_data)
boulder = json_to_dataframe(json_data=boulder_data)
data = pd.concat([lead, boulder])
data = data.sort_values(
by=["style", "rank", "name", "last_name"],
ascending=False,
)
@app.route('/')
def index():
grades = request.args.get("grades", default="fr")
unique_climbs = data[data["is_fa"]]
climbs = pd.concat([
unique_climbs[unique_climbs["style"] == "sport"][0:1],
unique_climbs[unique_climbs["style"] == "bouldering"][0:1],
unique_climbs[unique_climbs["style"] == "sport"][1:2],
unique_climbs[unique_climbs["style"] == "bouldering"][1:2],
unique_climbs[unique_climbs["style"] == "sport"][2:3],
unique_climbs[unique_climbs["style"] == "bouldering"][2:3],
])
return render_template('index.html', climbs=climbs, grades=grades)
@app.route('/sport')
def sport():
grades = request.args.get("grades", default="fr")
climbs = data[(data["is_fa"]) & (data["style"] == "sport")]
return render_template(
'generic.html',
title="Sport Climbing",
category="sport",
climbs=climbs,
grades=grades,
)
@app.route("/bouldering")
def bouldering():
grades = request.args.get("grades", default="fr")
climbs = data[(data["is_fa"]) & (data["style"] == "bouldering")]
return render_template(
'generic.html',
title="Bouldering",
category="bouldering",
climbs=climbs,
grades=grades,
)
@app.route("/sport/climber/<climber>")
def sport_climber(climber):
grades = request.args.get("grades", default="fr")
climbs = data[(data["climber_key"] == climber) & (data["style"] == "sport")]
if climbs.shape[0] > 0:
return render_template(
'generic.html',
title=f"Sport Climbing: {climber.replace('+', ' ').title()}",
category="sport",
climbs=climbs,
grades=grades,
)
else:
return "Climber not found", 404
@app.route("/sport/route/<route>")
def sport_route(route):
grades = request.args.get("grades", default="fr")
climbs = data[(data["route_key"] == route) & (data["style"] == "sport") & (data["is_fa"])]
if climbs.shape[0] > 0:
return render_template(
'generic.html',
title="",
category="sport",
climbs=climbs,
grades=grades,
)
else:
return "Route not found", 404
@app.route("/bouldering/climber/<climber>")
def bouldering_climber(climber):
grades = request.args.get("grades", default="fr")
climbs = data[(data["climber_key"] == climber) & (data["style"] == "bouldering")]
if climbs.shape[0] > 0:
return render_template(
'generic.html',
title=f"Bouldering: {climber.replace('+', ' ').title()}",
category="bouldering",
climbs=climbs,
grades=grades,
)
else:
return "Climber not found", 404
@app.route("/bouldering/problem/<problem>")
def bouldering_problem(problem):
grades = request.args.get("grades", default="fr")
climbs = data[(data["route_key"] == problem) & (data["style"] == "bouldering") & (data["is_fa"])]
if climbs.shape[0] > 0:
return render_template(
'generic.html',
title="",
category="bouldering",
climbs=climbs,
grades=grades,
)
else:
return "Boulder not found", 404
@app.route("/update", methods=["POST"])
def webhook():
if request.method != "POST":
return 'Wrong event type', HTTPStatus.BAD_REQUEST
# Get signature from headers
signature_header = request.headers.get('X-Hub-Signature-256')
if not signature_header:
return 'No signature header', HTTPStatus.FORBIDDEN
webhook_secret = os.environ.get('WEBHOOK_SECRET')
if not webhook_secret:
return 'Webhook secret not configured', HTTPStatus.INTERNAL_SERVER_ERROR
if not signature_verified(request.data, webhook_secret, signature_header):
return "Signature not verified", HTTPStatus.FORBIDDEN
payload = request.get_json()
if payload and payload.get("ref") == "refs/heads/master":
try:
repo = git.Repo("~/mysite")
origin = repo.remotes.origin
origin.pull()
update()
return 'Updated PythonAnywhere successfully', HTTPStatus.OK
except Exception as e:
return f'Error during update: {str(e)}', HTTPStatus.INTERNAL_SERVER_ERROR
return "OK", HTTPStatus.OK
# helper template filters ----
@app.template_filter('bg_alternate')
def bg_alternate(index):
return "secondary" if index % 2 == 0 else "dark"
@app.template_filter('climber_first_name')
def climber_first_name(name):
return split_name(name)[0]
@app.template_filter('climber_last_name')
def climber_last_name(name):
return split_name(name)[1]
@app.template_filter('climber_key')
def climber_key(name):
return create_climber_key(name)