download all games chess com

Solutions on MaxInterview for download all games chess com by the best coders in the world

showing results for - "download all games chess com"
Mirko
07 Apr 2019
1import urllib
2import urllib.request
3import json
4import datetime
5
6def download_games(username: str, filename: str):
7    baseUrl = "https://api.chess.com/pub/player/" + username + "/games/"
8
9    with urllib.request.urlopen(baseUrl + "archives") as f:
10        archives = f.read()
11
12    try:
13        json_file = json.loads(archives)
14    except json.decoder.JSONDecodeError as e:
15        return False
16
17    for url in json_file['archives']:
18        date = datetime.datetime.strptime(url[-7:], '%Y/%m')
19
20        filepath = filename.format(date=date, username=username)
21
22        urllib.request.urlretrieve(url + '/pgn', filepath)
23
24if __name__ == '__main__':
25    username = "magnuscarlsen"
26    filename = "./{username}_games/chess_{date:%Y}-{date:%m}.pgn"
27
28    download_games(username, filename)