Skip to main content

Simple python library to fetch games directly into Postgres database from Lichess.org using it's API or pgn file containing games.

Project description

chess2postgres

Simple python library to fetch games directly into Postgres database from Lichess.org using it's API or pgn file containing games.

Installation.

  $ pip install chess2postgres

Usage.

  from chess2postgres import lichess_pgn2db, lichess_api2db

About.

Main goal while creating this library was creating possibility of fetching chess games directly into your Postgres database as chess games db's are getting popular lately.

Worth knowing.

Lichess API and pgn files may differ a little in some aspects. One of them is termination - while using api to fetch games, end of the game is descirbed more precisely - mate, resign, out of time, variant end (in RacingKings for example). On the other hand, pgn files call them 'abandoned', 'time forfeit' and 'standard'. I decided not to mess with it and leave it how it is. Despite this fact, it is possible to keep both games from pgn files and those fetched directly from Lichess in a same db.

Functions.

Currently, library supports 2 formats of inserting data into the database. You can do it from pgn file already downloaded and stored on your device or you can use Lichess API to directly fetch games from Lichess.org into the database.

lichess_pgn2db

This function works when you have already downloaded pgn file from Lichess.org and want to fetch it into the database. To sucesfully call this function, you have to specify parameters such as:

  • dbname : Name of the database in Postgres.
  • user : Name of the database user.
  • password : User's assword to the database.
  • host : Host of the database.
  • port : Port of the database.
  • pgn : A path to the pgn file stored on your device.

Predefined parameters:

  • tablename : name of the table to which you want to insert all the data. By default it creates a new table called 'lichess_games'.

By default, Lichess pgn files contain '?' in situations where a data is missing. It can happen when the game was abandoned (opening, ECO code, are marked as '?' and the game is just a empty string) or one of the players did not have an account (name, elo, RatingDiff are marked as '?'). In situatuons where the pgn file contains '?', it is added to the database this way. On the other hand, when the data is lacking (for example pgn file does not contain info about openings), they are marked as 'unknown'. Thats why some of the columns are labeled as text, even they could be integer or boolean.

lichess_api2db

This function is useful when you want to fetch games directly from Lichess to your database without downloading the pgn file. Sucesfull call of this function requires passing parameters such as:

  • dbname : Name of the database in Postgres.
  • user : Name of the database user.
  • password : User's password to the database.
  • host : Host of the database.
  • port : Port of the database.
  • api_token : Your Lichess API token
  • player_name : Account name of player whose games you want to fetch.
  • amount : Amount of games to be fetched into the db.

Predefined parameters:

  • tablename: name of the table to which you want to insert all the data. By default it creates a new table called 'lichess_games'
  • perf_type : Type of the games (blitz,bullet,rapid) you want to fetch. Default is None.
  • color : Color of pieces that the player has played the games with. Default is None.
  • pgn_moves : If true, the game will contain move chars (1.e4 e5 2.Nf3 Nf6). If false, the game will not contain them at all. Default is True.
  • rated : If True, the games will be rated. If False, the games will be unrated. Default is None.

Same as in lichess_pgn2db, this func describes all data that is lacking as 'unknown'. Consider that sometimes opening, ECO etc are not appropiate due to gametype.

sample usage:

   lichess_api2db('postgres', 'postgres', 'pass123','localhost', 5432, 'api_token777', 'Player01', 5, tablename='rapid_games', perf_type='rapid', rated='True')

It connects to the db, fetches 'Player01' rapid, ranked games from Lichess to your Postgres databse using specified api token. Done.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chess2postgres-0.0.3.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

chess2postgres-0.0.3-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file chess2postgres-0.0.3.tar.gz.

File metadata

  • Download URL: chess2postgres-0.0.3.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for chess2postgres-0.0.3.tar.gz
Algorithm Hash digest
SHA256 c745905220e801f22dc5a218de898449884cdc2a9db0185c44e0411bf53f715e
MD5 736f00ef58480308cf2f47eba13c5c2d
BLAKE2b-256 eb1dd347d712be8821ae2913869ba8d8e82ed6f97fa6336322f238f2129e5313

See more details on using hashes here.

File details

Details for the file chess2postgres-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for chess2postgres-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 58e679d2ee9040fcb43914d3719cab9fee151de496c796aa4238de079f6a7f81
MD5 ae59ef294da9d6df7c561d9bd135d203
BLAKE2b-256 a48381d163ee1ad6ea5cb0b58ba2ac0b9b0d77095e4152cdeec0d511f0137bc7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page