1
+ # NOTES:
2
+ # it might be easier to write my own parser. python-lichess does it themselves, using these lines of code:
3
+ # https://github.com/cyanfish/python-lichess/blob/8deea5dda8965b90915c744d65f2a4bc3ee085e5/lichess/api.py#L293
4
+ # https://github.com/cyanfish/python-lichess/blob/8deea5dda8965b90915c744d65f2a4bc3ee085e5/lichess/api.py#L89
5
+ # there's something with specifying the object_type=lichess.format.GAME_STREAM_OBJECT and passing it to the parser
6
+
7
+ import pandas as pd
8
+ import lichess .api
9
+ import requests
10
+
11
+ # we want to create a table for user's games, that stores: color, points-earned, opening
12
+ def process_game_dict (game_dict , username ):
13
+ """
14
+ Processes a single game dictionary, as it comes from the result of lichess.api.user_games().
15
+ """
16
+ opening_name = game_dict ["opening" ]["name" ]
17
+ opening_name_simple = opening_name .split (':' )[0 ]
18
+ opening_code = game_dict ["opening" ]["eco" ]
19
+ color = "white" if game_dict ["players" ]["white" ]["user" ]["id" ]== username else "black"
20
+ if "winner" not in game_dict :
21
+ points = .5
22
+ elif game_dict ["winner" ]== color :
23
+ points = 1
24
+ else :
25
+ points = 0
26
+ return {"opening_name" : opening_name ,
27
+ "opening_name_simple" : opening_name_simple ,
28
+ "opening_code" : opening_code ,
29
+ "color" : color ,
30
+ "points" : points }
31
+
32
+ # load the games
33
+ user = 'madmaxmatze'
34
+ query = {
35
+ "opening" : True ,
36
+ "moves" : False ,
37
+ "sort" : "dateDesc" ,
38
+ # "max": 300
39
+ }
40
+ games = [process_game_dict (g , user ) for g in lichess .api .user_games (user , ** query )]
41
+
42
+ # test
43
+ games = pd .DataFrame (games )
44
+ print (games .head (5 ))
45
+ for color in ["white" , "black" ]:
46
+ display_games = \
47
+ (games
48
+ [games ["color" ]== color ]
49
+ .groupby ("opening_name_simple" )
50
+ ["points" ]
51
+ .agg (["count" , "mean" ])
52
+ .sort_values ("count" , ascending = False )
53
+ .rename ({"opening_name_simple" : "opening" , "count" : "num_games" , "mean" : "avg_points_per_game" })
54
+ .head (10 ))
55
+ print (display_games )
0 commit comments