-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
chess_com_box.py
121 lines (96 loc) · 4.02 KB
/
chess_com_box.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
from collections import namedtuple
import datetime
import math
import os
import sys
from bs4 import BeautifulSoup
from typing import List
from github.InputFileContent import InputFileContent
import json
import requests
from github import Github
WIDTH_JUSTIFICATION_SEPARATOR = "."
GIST_TITLE = "♟︎ Chess.com Ratings"
ENV_VAR_GIST_ID = "GIST_ID"
ENV_VAR_GITHUB_TOKEN = "GH_TOKEN"
ENV_VAR_CHESS_COM_USERNAME = "CHESS_COM_USERNAME"
REQUIRED_ENVS = [
ENV_VAR_GIST_ID,
ENV_VAR_GITHUB_TOKEN,
ENV_VAR_CHESS_COM_USERNAME
]
# LIVE_URL_FORMAT = "https://www.chess.com/stats/live/{format}/{user}"
# PUZZLES_URL_FORMAT = "https://www.chess.com/stats/{format}/{user}"
# DAILY_URL_FORMAT = "https://www.chess.com/stats/{format}/chess/{user}"
STATS_URL = "https://api.chess.com/pub/player/{user}/stats"
TitleAndValue = namedtuple("TitleAndValue", "title value")
def validate_and_init() -> bool:
env_vars_absent = [
env
for env in REQUIRED_ENVS
if env not in os.environ or len(os.environ[env]) == 0
]
if env_vars_absent:
print(f"Please define {env_vars_absent} in your github secrets. Aborting...")
return False
return True
def get_adjusted_line(title_and_value: TitleAndValue, max_line_length: int) -> str:
separation = max_line_length - (
len(title_and_value.title) + len(title_and_value.value) + 2
)
separator = f" {WIDTH_JUSTIFICATION_SEPARATOR * separation} "
return title_and_value.title + separator + title_and_value.value
def get_chess_com_stats(user: str = "sciencepal") -> dict:
headers = {
'User-Agent': f'chess-com-box-py @{user}'
}
stats_dict = requests.get(STATS_URL.format(user=user), headers=headers).json()
return stats_dict
def get_rating_line(
stats_key: str, chess_emoji: str, chess_format: str, chess_stats: dict
) -> TitleAndValue:
try:
rating = str(chess_stats.get(stats_key).get("highest" if (chess_format == "Tactics") else "last").get("rating"))
except Exception as e:
rating = "N/A"
return TitleAndValue(chess_emoji + " " + chess_format, rating + " 📈")
def update_gist(title: str, content: str) -> bool:
access_token = os.environ[ENV_VAR_GITHUB_TOKEN]
gist_id = os.environ[ENV_VAR_GIST_ID]
# gist = Github(access_token).get_gist(gist_id)
# # Shouldn't necessarily work, keeping for case of single file made in hurry to get gist id.
# old_title = list(gist.files.keys())[0]
# gist.edit(title, {old_title: InputFileContent(content, title)})
headers = {'Authorization': f'token {access_token}'}
r = requests.patch('https://api.github.com/gists/' + gist_id, data=json.dumps({'files':{title:{"content":content}}}),headers=headers)
print(f"{title}\n{content}")
def main():
if not validate_and_init():
return
chess_com_user_name = os.environ[ENV_VAR_CHESS_COM_USERNAME]
chess_stats = get_chess_com_stats(chess_com_user_name)
blitz_line = get_rating_line("chess_blitz", "⚡", "Blitz", chess_stats)
bullet_line = get_rating_line("chess_bullet", "🚅", "Bullet", chess_stats)
rapid_line = get_rating_line("chess_rapid", "⏲️", "Rapid", chess_stats)
puzzles_line = get_rating_line("tactics", "🧩", "Tactics", chess_stats)
daily_line = get_rating_line("chess_daily", "☀️", "Daily", chess_stats)
lines = [
get_adjusted_line(blitz_line, 52),
get_adjusted_line(bullet_line, 52),
get_adjusted_line(rapid_line, 53),
get_adjusted_line(puzzles_line, 52),
get_adjusted_line(daily_line, 53)
]
content = "\n".join(lines)
update_gist(GIST_TITLE, content)
if __name__ == "__main__":
import time
s = time.perf_counter()
# test with python chess_com_box.py test <gist> <github-token> <user>
if len(sys.argv) > 1:
os.environ[ENV_VAR_GIST_ID] = sys.argv[2]
os.environ[ENV_VAR_GITHUB_TOKEN] = sys.argv[3]
os.environ[ENV_VAR_CHESS_COM_USERNAME] = sys.argv[4]
main()
elapsed = time.perf_counter() - s
print(f"{__file__} executed in {elapsed:0.2f} seconds.")