Skip to content

Instantly share code, notes, and snippets.

Created August 18, 2021 22:54
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Extract Lichess game URLs from a PGN download for games where Black played the Pirc Defence
#! /usr/bin/env python3
# Copyright (C) 2021 Sebastian Pipping <>
# Licensed under the Apache 2.0 License
# Version 2021-08-19 00:51 UTC+2
import argparse
from typing import List
import chess
import chess.pgn #
def is_turn(board, for_white: bool) -> bool:
return board.turn == for_white
def matches_opening(opening_moves: List[str], game_moves: List[chess.Move], for_white: bool) -> bool:
for i, expected_uci in enumerate(opening_moves):
move_index = 2 * i + int(not for_white)
if game_moves[move_index] != chess.Move.from_uci(expected_uci):
return False
except IndexError:
return False
return True
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('pgn_file', metavar='PGN_FILE')
config = parser.parse_args()
default_board = chess.Board()
moves_pirc_defence = [
with open(config.pgn_file) as f:
while True:
game = chess.pgn.read_game(f)
if game is None:
url = game.headers['Site']
board = game.board()
if board != default_board or not is_turn(board, for_white=True):
moves = list(game.mainline_moves())
if matches_opening(moves_pirc_defence, moves, for_white=False):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment