-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
135 lines (109 loc) · 4.4 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
"""
Author: battleoverflow (https://github.com/battleoverflow)
Project: Chess Dashboard (https://github.com/battleoverflow/chess-dashboard)
License: BSD 2-Clause
"""
import datetime
from flask import Flask, render_template, request
from chessdotcom import Client, get_player_profile, get_player_stats
from chessdotcom.types import ChessDotComError
app = Flask(__name__)
def get_profile(username):
"""
Get the profile for a specific username a structured dictionary.
@rtype dict
"""
# As of June 2023, the Chess.com API now requires a user agent to be set in request headers
# Forum post: https://www.chess.com/clubs/forum/view/error-403-in-member-profile
# If this request header is not set, be prepared to get a 403 Forbidden response
Client.request_config["headers"]["User-Agent"] = ("Chess Dashboard by battleoverflow (https://github.com/battleoverflow)")
response_profile = get_player_profile(username)
# Not all players will have a profile image
try:
player_avatar = response_profile.json['player']['avatar']
except KeyError:
player_avatar = "/static/img/missing_user.png"
profile = {
"avatar": player_avatar,
"player_id": response_profile.json['player']['player_id'],
"username": response_profile.json['player']['username'],
"title": "N/A",
"followers": response_profile.json['player']['followers'],
"last_online": response_profile.json['player']['last_online'],
"is_streamer": response_profile.json['player']['is_streamer'],
"twitch_url": "https://www.twitch.tv"
}
if "title" in response_profile.json['player']:
# Not everyone will have a title
profile['title'] = response_profile.json['player']['title']
if profile['is_streamer']:
# Not everyone will have a twitch
profile['twitch_url'] = response_profile.json['player']['twitch_url']
return profile
def get_stats(username):
"""
Get the stats for a specific username a structured dictionary.
@rtype dict
"""
response_stats = get_player_stats(username)
stats = {
"highest_elo": response_stats.json['stats']['tactics']['highest']['rating'],
"lowest_elo": response_stats.json['stats']['tactics']['lowest']['rating']
}
return stats
def calculate_increase(elo_values):
"""
Calculate ELO increase and round to the 2nd decimal place.
@rtype int
"""
try:
# (abs(current_elo - previous_elo) / previous_elo) * 100.0
percentage = (abs(elo_values[0] - elo_values[1]) / elo_values[1]) * 100.0
except ZeroDivisionError:
return 0
return round(percentage, 2)
@app.route("/", methods=["POST", "GET"])
def index():
username = 0
if request.method == "POST":
form_data = request.form
for _, value in form_data.items():
username = value
break
else:
form_data = "N/A"
try:
chess_template = render_template(
"index.html",
form_data=form_data,
avatar=get_profile(username)['avatar'],
player_id=get_profile(username)['player_id'],
username=get_profile(username)['username'],
title=get_profile(username)['title'],
followers=get_profile(username)['followers'],
last_online=datetime.datetime.fromtimestamp(get_profile(username)['last_online']).strftime("%m/%d/%Y @ %H:%M:%S"),
is_streamer=get_profile(username)['is_streamer'],
twitch_url=get_profile(username)['twitch_url'],
twitch_url_stripped=str(get_profile(username)['twitch_url']).replace("https://twitch.tv/", ""),
highest_elo=get_stats(username)['highest_elo'],
lowest_elo=get_stats(username)['lowest_elo'],
elo_increase=calculate_increase([get_stats(username)['highest_elo'], get_stats(username)['lowest_elo']])
)
except ChessDotComError:
chess_template = render_template(
"index.html",
avatar="/static/img/missing_user.png",
player_id="N/A",
username="N/A",
title="N/A",
followers="N/A",
last_online="N/A",
is_streamer="N/A",
twitch_url="https://www.twitch.tv",
highest_elo="N/A",
lowest_elo="N/A",
elo_increase="N/A"
)
return chess_template
if __name__ == "__main__":
app.run()