Skip to content

Instantly share code, notes, and snippets.

@LalleSX
Created April 23, 2023 03:48
Show Gist options
  • Save LalleSX/ef55e5a3f53d7da4fddb65b8eb14fe05 to your computer and use it in GitHub Desktop.
Save LalleSX/ef55e5a3f53d7da4fddb65b8eb14fe05 to your computer and use it in GitHub Desktop.
Download all your Chess.com games to PGN
import requests
import os
## Set your username at the bottom of this file
def download_games(username, output_directory):
url = f"https://api.chess.com/pub/player/{username}/games/archives"
response = requests.get(url)
if response.status_code != 200:
print(f"Error: Unable to access user archives at {url}")
return
archives = response.json()["archives"]
if not os.path.exists(output_directory):
os.makedirs(output_directory)
for archive_url in archives:
response = requests.get(f"{archive_url}/pgn")
if response.status_code != 200:
print(f"Error: Unable to download archive at {archive_url}")
continue
month = archive_url.split("/")[-1]
file_path = os.path.join(output_directory, f"{username}_{month}.pgn")
with open(file_path, "w") as file:
file.write(response.text)
print(f"Downloaded {username}'s games for {month} to {file_path}")
if __name__ == "__main__":
username = "Hikaru" # Replace with your chess.com username
output_directory = "chess_games" # You can change this to the desired output directory
download_games(username, output_directory)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment