-
Notifications
You must be signed in to change notification settings - Fork 31
Adds downloading user games from Chess.com and cleans up downloading user games from Lichess.org #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Adds downloading user games from Chess.com and cleans up downloading user games from Lichess.org #33
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,14 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| """Downloading chess puzzles for lichess.org""" | ||
| """Downloading chess puzzles for Lichess.org""" | ||
|
|
||
| import argparse | ||
| import logging | ||
| import sys | ||
| import urllib | ||
|
|
||
| import requests | ||
| import lichess | ||
|
|
||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument("--token", metavar="TOKEN", default="", | ||
|
|
@@ -18,15 +20,41 @@ | |
| help="substantially reduce the number of logged messages") | ||
| parser.add_argument("--max", metavar="MAX", default="60", | ||
| help="max number of games") | ||
| parser.add_argument("--site", metavar="SITE", default="lichess", help="Website to query user game data") | ||
|
|
||
| settings = parser.parse_args() | ||
| logging.basicConfig(format="%(message)s", level=settings.loglevel, stream=sys.stdout) | ||
|
|
||
| logging.basicConfig(format="%(message)s", level=settings.loglevel, stream=sys.stdout) | ||
| logging.debug("Downloading games from: " + settings.username) | ||
|
|
||
| response = requests.get( | ||
| 'https://lichess.org/api/games/user/' + settings.username + '?max=' + settings.max + '&token=' + settings.token + '&perfType=blitz,rapid,classical&opening=true') | ||
| pgn = str(response.text) | ||
| all_games = open("games.pgn", "w") | ||
| all_games.write(pgn) | ||
| all_games.close() | ||
| logging.debug("Finished. Pgn is in games.pgn ") | ||
|
|
||
| if settings.site == "lichess": | ||
| with open("games.pgn", "w") as new_file: | ||
| myclient = lichess.Client() | ||
| new_file.write(myclient.export_by_user(settings.username, max_games=settings.max)) | ||
| new_file.close() | ||
| logging.debug("Finished. Pgn is in games.pgn ") | ||
| elif settings.site == "chessdotcom": | ||
| url_chessdotcom = "https://api.chess.com/pub/player/" + \ | ||
| settings.username.lower() + "/games" | ||
| url_archive = url_chessdotcom + "/archives" | ||
| http_response = urllib.request.urlopen(url_archive) | ||
|
|
||
| archives = http_response.read().decode("utf-8") | ||
| archives = archives.replace("{\"archives\":[\"", "\",\"") | ||
| archive_dates = archives.split("\",\"" + url_chessdotcom) | ||
| archive_dates[len(archive_dates) - 1] = archive_dates[ | ||
| len(archive_dates) - 1].rstrip("\"]}") | ||
|
|
||
| with open("games.pgn", "w") as new_file: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we will need the |
||
| for i in range(len(archive_dates) - 1): | ||
| cur_url = url_chessdotcom + archive_dates[i + 1] + "/pgn" | ||
| cur_filename = archive_dates[i + 1].replace("/", "-") | ||
| response = requests.get(cur_url, "./" + cur_filename + ".pgn") | ||
| new_file.write(response.text) | ||
| new_file.close() | ||
| logging.debug("Finished. Pgn is in games.pgn ") | ||
| else: | ||
| logging.debug("Invalid argument for site: only arguments allowed are lichess and chessdotcom") | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,3 +1,4 @@ | ||||||||||||||||||
| colorama==0.4.4 | ||||||||||||||||||
| chess==1.5.0 | ||||||||||||||||||
| requests==2.20.0 | ||||||||||||||||||
| lichess>=0.1.9 | ||||||||||||||||||
|
Comment on lines
1
to
+4
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had a problem installing this and had to change it to:
Suggested change
This was the error |
||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be better if the dates are sorted from recent to older. So you can always download the latest games by default.