chesstok-lookup/player_games.py

74 lines
2.0 KiB
Python
Raw Normal View History

import user_agents
import random
2023-10-28 05:00:25 +00:00
import requests
import re
import os
import json
import concurrent.futures
2023-11-08 08:20:18 +00:00
2023-10-28 05:00:25 +00:00
def playerArchives(username):
# Create archive list. This is a list of pages containing lists of games used for futher downloading
user_agent = random.choice(user_agents.user_agents) # load up random user agent from list
headers = {
'User-Agent':user_agent
}
url = f"https://api.chess.com/pub/player/{username}/games/archives"
archive = requests.get(url, headers=headers).json()['archives']
2023-10-28 05:00:25 +00:00
file_path = os.getcwd()
2023-10-28 05:00:25 +00:00
file_name = os.path.join(file_path, username+'_archive.txt')
with open(file_name, 'w') as f:
json.dump(archive, f)
return archive
def playerMonthly(url=None):
2023-10-28 05:00:25 +00:00
user_agent = random.choice(user_agents.user_agents) # load up random user agent from list
headers = {
'User-Agent':user_agent
}
2023-10-28 05:00:25 +00:00
if url:
url=url+'/pgn' # connect to multi-game pgn download endpoint by appending a "pgn" to the url provided by url in archive list
2023-10-28 05:00:25 +00:00
else:
username=input("username: ")
YYYY=input("year in YYYY: ")
MM=input("month in MM: ")
url = f"https://api.chess.com/pub/player/{username}/games/{YYYY}/{MM}/pgn"
2023-10-28 05:00:25 +00:00
# get and save games list in .pgn format
games = requests.get(url, headers=headers).content.decode("utf-8")
2023-10-28 05:00:25 +00:00
return games
2023-10-28 05:00:25 +00:00
# Multithreaded games download
2023-11-08 08:44:16 +00:00
def threddGames(username=None, archive=None):
try:
path = os.getcwd()+'/archives'
os.makedirs(path)
except OSError:
pass
if archive:
with open(archive) as f:
archive = json.load(f)
else:
archive = playerArchives(username)
2023-10-28 05:00:25 +00:00
# async download games
games_list = []
2023-10-28 05:00:25 +00:00
with concurrent.futures.ThreadPoolExecutor() as executor:
for future in executor.map(playerMonthly, archive):
games_list.extend(future)
pgn_db = "\n\n".join(games_list)
with open(username+'.pgn', 'w') as f:
f.write(pgn_db)
return pgn_db