-
Notifications
You must be signed in to change notification settings - Fork 5
/
chesscom-games.py
35 lines (31 loc) · 1.25 KB
/
chesscom-games.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from codecs import open
from datetime import date
import os.path
import requests
def main(user, where):
print("Downloading %s's games to %s:" % (user, where))
for archive in get('https://api.chess.com/pub/player/%s/games/archives' % user)['archives']:
download_archive(archive, where)
def download_archive(url, where):
games = get(url)['games']
d = date.fromtimestamp(games[0]['end_time'])
y = d.year
m = d.month
filename = os.path.join(where, "%d-%02d.pgn" % (y, m))
print('Starting work on %s...' % filename)
# XXX: If a file with this name already exists, we'll blow away the old
# one. Possibly not ideal.
with open(filename, 'w', encoding='utf-8') as output:
for game in games:
print(game['pgn'], file=output)
print('', file=output)
def get(url):
return requests.get(url).json()
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description="Download a user's games from chess.com")
parser.add_argument('user', metavar='USER', help='The user whose games we want')
parser.add_argument('where', metavar='PATH', help='Where to create the PGN files',
default=".", nargs='?')
args = parser.parse_args()
main(args.user, args.where)