diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 5008ddf..0000000 Binary files a/.DS_Store and /dev/null differ diff --git a/README.md b/README.md index d0e6441..82a3a30 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,10 @@ ## About -This is a python application dedicated to creating chess puzzles/tactics from a pgn file. -Also it can download your games from lichess.org and use that file. +This is a Python application dedicated to creating chess puzzles/tactics from a pgn file. +Also, it can download your games from Lichess.org or Chess.com and use that file to create the chess puzzles/tactics. -It's based on the great [https://github.com/clarkerubber/Python-Puzzle-Creator](https://github.com/clarkerubber/Python-Puzzle-Creator) by @clarkerubber +It is based on the great [https://github.com/clarkerubber/Python-Puzzle-Creator](https://github.com/clarkerubber/Python-Puzzle-Creator) by @clarkerubber Things that I changed: - Use a local pgn file with games as a source. @@ -21,14 +21,14 @@ It uses a different approach to create tactics, so probably it will generate a d ## Installation -This script requires the *Requests* and *Python-Chess* libraries to run, as well as a copy of *Stockfish* -Is recommended that you use Python 3 and pip3. But it could work with Python 2.7 and pip (probably you will need to install futures `pip install futures` ) +This script requires a copy of `Stockfish` and the `Requests`, `Python-Chess`, and `Lichess` libraries to run. +Is recommended that you use Python 3 and pip3, but it could also work with Python 2.7 and pip (you will likely need to install futures `pip install futures`). Please, take a look at [development doc](DEVELOPMENT.md) for details. ### Install requirements -`pip3 install -r requirements.txt --user` +`pip3 install -r requirements.txt` ### Setup @@ -37,22 +37,27 @@ MacOS / Linux : `sh build-stockfish.sh` to obtain the current lichess Stockfish ## Launching Application ### Downloading games for a specific user -You can download games from a specific user using this command: -`python3 download_games.py ` +You can download games from a specific user from either Lichess.org or Chess.com. +For a Chess.com user: +- `python3 download_games.py --site chessdotcom` -By default, it will download the last 60 games from blitz, rapid and classical. +
-**Arguments** +For a Lichess user: +- `python3 download_games.py --site lichess` -You can use the `max` argument to get more games and use the lichess api token with the `token` argument to make the download faster. https://lichess.org/api#operation/apiGamesUser +or -It will save the games in the `games.pgn` file +- `python3 download_games.py ` +
+ +**Arguments** -**Example to get 100 games using the token** +You can use the `max` argument to adjust the amount of games to query for Lichess.org (by default the max is 60 games) -`python3 download_games.py --max 100 --token 123456789` +`python3 download_games.py --max 150` ### Downloading games from tournaments You can download games from multiple tournaments using this command: @@ -94,8 +99,25 @@ The `result header` is the tactic result and not the game result. It can be load ## Problems? -#### Stockfish errors +### Stockfish errors - If you have problems building Stockfish try downloading Stockfish directly https://stockfishchess.org/download/ +### ssl.SSLCertVerificationError +If you receive the following error: +``` +ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate +verify failed: unable to get local issuer certificate +``` +**MacOS Solution** +1. Check which Python version you're using +`$ python3 --version` +2. Go to the current Python version's directory like so: + +"Macintosh HD" >> "Applications" >> folder of Python version from last step + +(If you are having trouble finding the Macintosh HD directory read [this](https://discussions.apple.com/thread/5207145)) +3. Double click on `Install Certificates.command` file + + ## Want to see all my chess related projects? -Check [My projects](http://vitomd.com/blog/projects/) for a full detailed list. +Check [my projects](http://vitomd.com/blog/projects/) for a full detailed list. diff --git a/download_games.py b/download_games.py index 71ce254..e8da102 100644 --- a/download_games.py +++ b/download_games.py @@ -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: + 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") + + diff --git a/requirements.txt b/requirements.txt index 280c1c6..ebad54d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ colorama==0.4.4 chess==1.5.0 requests==2.20.0 +lichess>=0.1.9