Last active
November 28, 2023 01:03
-
-
Save Oqarshi/f0d94d126b56b0469c4977c862c2ddf6 to your computer and use it in GitHub Desktop.
stuff for chess.com api forum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests # pip install requests | |
import re # pip install re | |
from datetime import datetime | |
from colorama import Fore, Back, Style, init # pip install colorama | |
# colorama stuff | |
init(autoreset=True) | |
# for chess.com header | |
#------------------------ IMPORTANT------------------------ # | |
Auth_username = "YOUR_CHESSCOM_USERNAME" | |
Auth_email = "YOUR_CHESSCOM_USED_EMAIL" | |
# stuff to extract game id form two different urls | |
def extract_game_id(link): | |
match = re.search(r'/game/live/(\d+)', link) | |
if match: | |
return match.group(1) | |
else: | |
match = re.search(r'/analysis/game/live/(\d+)', link) | |
if match: | |
return match.group(1) | |
else: | |
return None | |
# send request and get response | |
def get_game_response(game_id): | |
url = f"https://www.chess.com/callback/live/game/{game_id}" | |
response = requests.get(url) | |
return response | |
# get response and parse data to return white player name and date | |
# parse date to get month and year | |
def parse_game_response(response_text): | |
data = response_text.json() | |
game_info = data.get("game", {}) | |
players_info = data.get("players", {}) | |
if game_info and players_info: | |
players = players_info.get("top", {}) | |
white_player = players.get("username") | |
date_str = game_info.get("pgnHeaders", {}).get("Date", "") | |
date = datetime.strptime(date_str, "%Y.%m.%d") | |
return white_player, date | |
else: | |
return None | |
# look at the official api, use white player name, month, year to get response | |
def get_monthly_games(username, year, month): | |
url = f"https://api.chess.com/pub/player/{username}/games/{year}/{month:02d}" | |
headers = {'User-Agent': f'username: {Auth_username}, email: {Auth_email}'} | |
response = requests.get(url, headers=headers) | |
return response | |
# use game id to find the game and extract the pgn | |
def find_game_pgn(game_url, games_data): | |
for game in games_data["games"]: | |
if game["url"] == game_url: | |
return game["pgn"] | |
return None | |
# get user input for the chess.com game ID | |
chess_link = input("Enter the Chess.com game link: ") | |
# extract id form the function | |
game_id = extract_game_id(chess_link) | |
if game_id: | |
# if game id found call function | |
response = get_game_response(game_id) | |
# check for errors | |
if response.status_code == 200: | |
game_data = parse_game_response(response) | |
if game_data: | |
white_player, date = game_data | |
# extract year and month from the date | |
year = date.year | |
month = date.month | |
# send a request to the API for monthly games | |
monthly_games_response = get_monthly_games(white_player, year, month) | |
if monthly_games_response.status_code == 200: | |
print(Fore.GREEN + "Monthly Games:") | |
monthly_games_data = monthly_games_response.json() | |
# the url of the game | |
target_game_url = 'https://www.chess.com/game/live/' + game_id | |
# find PGN for the specified game | |
target_game_pgn = find_game_pgn(target_game_url, monthly_games_data) | |
if target_game_pgn: | |
print(Fore.BLUE + f"\nPGN for the target game ({target_game_url}):") | |
print(Fore.YELLOW + target_game_pgn) | |
# create a new file and write the PGN into it | |
file_name = f"GameID:{game_id}_{year}_{month}.pgn" | |
with open(file_name, 'w') as file: | |
file.write(target_game_pgn) | |
print(Fore.GREEN + f"\nPGN written to file: {file_name}") | |
else: | |
print(Fore.RED + f"\nTarget game ({target_game_url}) not found in the monthly games.") | |
else: | |
print(Fore.RED + f"\nFailed to retrieve monthly games. Status code: {monthly_games_response.status_code}") | |
else: | |
print(Fore.RED + "\nFailed to parse game data.") | |
else: | |
print(Fore.RED + f"\nFailed to retrieve data. Status code: {response.status_code}") | |
else: | |
print(Fore.RED + "\nInvalid Chess.com game link. Please provide a valid link.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment