download all games chess.com
import urllib
import urllib.request
import json
import datetime
def download_games(username: str, filename: str):
baseUrl = "https://api.chess.com/pub/player/" + username + "/games/"
with urllib.request.urlopen(baseUrl + "archives") as f:
archives = f.read()
try:
json_file = json.loads(archives)
except json.decoder.JSONDecodeError as e:
return False
for url in json_file['archives']:
date = datetime.datetime.strptime(url[-7:], '%Y/%m')
filepath = filename.format(date=date, username=username)
urllib.request.urlretrieve(url + '/pgn', filepath)
if __name__ == '__main__':
username = "magnuscarlsen"
filename = "./{username}_games/chess_{date:%Y}-{date:%m}.pgn"
download_games(username, filename)