-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchessdotcom_api.py
More file actions
56 lines (45 loc) · 1.67 KB
/
Copy pathchessdotcom_api.py
File metadata and controls
56 lines (45 loc) · 1.67 KB
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
"""Some class and functions to get data from chess.com API."""
import pandas as pd
import requests
class Player_Profile:
"""Player profile class."""
def __init__(self, username):
"""Initialize."""
self.username = username
self.profile = self.player_profile()
self.stats = self.player_stats()
# self.online = self.isonline()
def player_profile(self):
"""Get player profile."""
url = "https://api.chess.com/pub/player/" + self.username
response = requests.get(url).json()
return pd.json_normalize(response)
def player_stats(self):
"""Get player stats."""
url = "https://api.chess.com/pub/player/" + self.username + "/stats"
response = requests.get(url).json()
return pd.json_normalize(response)
def isonline(self):
"""Check if player is online."""
url = "https://api.chess.com/pub/player/" + self.username + "/is-online"
response = requests.get(url).json()
return response["online"]
def games(self, year, month):
"""Get player games."""
url = (
"https://api.chess.com/pub/player/"
+ self.username
+ "/games/"
+ str(year)
+ "/"
+ str(month)
)
response = requests.get(url).json()
return pd.json_normalize(response["games"])
def titled_players(self, title):
"""Get titled players.
Valid title abbreviations are: GM, WGM, IM, WIM, FM, WFM, NM, WNM, CM, WCM.
"""
url = "https://api.chess.com/pub/titled/" + title
response = requests.get(url).json()
return pd.DataFrame(response["players"], columns=["username"])