-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
69 lines (53 loc) · 1.88 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
from flask import Flask, Response, render_template
from chessdotcom import get_player_stats, Client
from chessdotcom import Client
from dotenv import load_dotenv
import os
load_dotenv()
def rating(time_control):
username = os.getenv("username")
response = get_player_stats(username)
if time_control == "blitz":
time_control = "chess_blitz"
elif time_control == "rapid":
time_control = "chess_rapid"
elif time_control == "bullet":
time_control = "chess_bullet"
if time_control:
return [response.json['stats'][time_control]['last']['rating']]
return [response.json['stats']['tactics']['highest']['rating'], response.json['stats']['tactics']['lowest']['rating']]
def generate_card():
time_control = os.getenv("time_control")
rating_level = os.getenv("rating_level")
email = os.getenv("email")
Client.request_config['headers']['User-Agent'] = 'My Chess.com Github Readme Application.You can find it on my Github profile https://github.com/KenWuqianghao. Contact me at {}'.format(email)
elo = rating(time_control)
if time_control == "chess_blitz":
time_control = "Chess.com Blitz"
elif time_control == "chess_rapid":
time_control = "Chess.com Rapid"
elif time_control == "chess_bullet":
time_control = "Chess.com Bullet"
else:
time_control = "Rating"
if rating_level == "highest":
elo = elo[0]
elif rating_level == "lowest":
elo = elo[1]
else:
elo = elo[0]
svg = render_template(
"card.html.j2",
time_control = time_control,
elo = elo
)
return svg
app = Flask(__name__)
@app.route("/")
def handle_all():
svg = generate_card()
resp = Response(svg, mimetype="image/svg+xml")
resp.headers["Cache-Control"] = "s-maxage=1"
return resp
if __name__ == "__main__":
app.run(debug=True)