Untitled

mail@pastecode.io avatar
unknown
python
a year ago
1.2 kB
2
Indexable
Never
import json
import requests
from datetime import datetime

# with open("./config.json", "r") as file:
# 	config = json.load(file)

with open("./data.json", "r") as file:
	filedata = json.load(file)

username = "chesslover0003"

# Last Update
# lastupdate = datetime.utcfromtimestamp(config["lastupdate"])
lastupdate = 0

url = f"https://api.chess.com/pub/player/{username}/games/archives"

response = requests.get(url, headers = {'User-Agent': 'username: chesslover0003, email: brian.erdelyi@gmail.coml'})

data = response.json()

for archive in data["archives"]:
	pieces = archive.split("/")
	year = int(pieces[-2])
	month = int(pieces[-1])

if year > lastupdate.year or (year == lastupdate.year and month >= lastupdate.month):
	date = f"{year}/{str(month).zfill(2)}"
	print("update", date)

url = f"https://api.chess.com/pub/player/{username}/games/{date}"
response = requests.get(url, headers = {'User-Agent': 'username: myaccount, email: my@emaill'})

filedata[f"{year}/{month}"] = response.json()

config["lastupdate"] = datetime.now().timestamp()

# with open("./config.json", "w") as file:
#	json.dump(config, file)

with open("./data.json", "w") as file:
	json.dump(filedata, file)