-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotebook_database.py
More file actions
72 lines (52 loc) · 1.57 KB
/
Copy pathnotebook_database.py
File metadata and controls
72 lines (52 loc) · 1.57 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""Notebook to database module."""
import pandas as pd
import re
from chessdotcom_api import Player_Profile
import chess
def get_games(username, year, month):
"""Get games from chess.com api."""
player = Player_Profile(username)
df = player.games(year, month)
games_pgn = []
for i in df["pgn"]:
games_pgn.append(i.split("\n")[-2])
return games_pgn, df
def get_moves(game_pgn):
"""Get moves from game_pgn."""
pattern = r"\{.*?\}"
pattern2 = r"\d+\.{3}"
pattern3 = r"\d+\.{1}"
# pattern matches everything between curly brackets
new_string = re.sub(pattern, "", game_pgn)
new_string = re.sub(pattern2, "", new_string)
new_string = re.sub(pattern3, "", new_string)
# replaces all matches with an empty string
return new_string.split()[:-1]
username = "fe_lipe_br"
games, df = get_games(username, 2022, 10)
white = df["white.username"].iloc[0]
black = df["black.username"].iloc[0]
moves_pgn = get_moves(games[0])
board_fen = []
move_list = []
board = chess.Board()
board_fen.append(board.fen())
for move in moves_pgn:
board.push_san(move)
board_fen.append(board.fen())
move_list.append(move)
white_len = [white] * len(board_fen)
black_len = [black] * len(board_fen)
white_len
black_len
df_db = pd.DataFrame(
{"white_username": white_len, "black_username": black_len, "fen": board_fen}
)
df_db
board.fen()
board.fen()
# 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
board.push_san("e4")
board.fen()
# 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1'
df.to_feather("games.feather")