Last active
February 5, 2019 00:08
-
-
Save beefy/fc3d22cbdc1b843c8c8858da29ca12ff to your computer and use it in GitHub Desktop.
parse chess pgn metadata into python dict
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
import datetime | |
import numpy as np | |
from datetime import datetime | |
games = [] | |
rec= {} | |
keys = [ | |
"[Event ","[Site ","[Date ","[Round ","[White ","[Black ", | |
"[Result ","[UTCDate ","[UTCTime ","[WhiteElo ","[BlackElo ", | |
"[Variant ","[TimeControl ","[ECO ","[Termination ", | |
"[WhiteRatingDiff ","[BlackRatingDiff " | |
] | |
# parse pgn data into dict | |
parse = lambda key, line: line.replace(key,"").replace('"',"").replace("]","").replace("\n","") | |
for line in open("lichess.pgn","rb"): | |
for key in keys: | |
if key in line: | |
rec[key.replace("[","").replace(" ","")] = parse(key,line) | |
if not any([key in line for key in keys]) and len(rec.keys()) > 0: | |
games.append(rec) | |
rec = {} | |
has_key = lambda key: key in game.keys() | |
has_keys = lambda keys: all([has_key(key) for key in keys]) | |
# rating by date and variant | |
ret = {} | |
plots = {} | |
for game in games: | |
if has_keys(["UTCDate","UTCTime","White","Black","WhiteElo","BlackElo","Event"]): | |
if "Rated" in game["Event"] and "tournament" not in game["Event"]: | |
if game["Event"] not in ret.keys(): | |
ret[game["Event"]] = {} | |
plots[game["Event"]] = {"x":[],"y":[]} | |
date_str = game["UTCDate"] + " " + game["UTCTime"] | |
date = datetime.strptime(date_str,'%Y.%m.%d %H:%M:%S') | |
if game["White"] == "beefybeefy": | |
ret[game["Event"]][date] = game["WhiteElo"] | |
else: | |
ret[game["Event"]][date] = game["BlackElo"] | |
if date > datetime(2018,10,01,0,0) and date < datetime(2018,11,01): | |
plots[game["Event"]]["x"].append(date) | |
plots[game["Event"]]["y"].append(ret[game["Event"]][date]) | |
for variant in plots.keys(): | |
x = np.array(plots[variant]["x"]) | |
y = np.array(plots[variant]["y"]) | |
rapid = "Rapid" in variant | |
ultrabullet = "UltraBullet" in variant | |
classical = "Classical" in variant | |
blitz = "Blitz" in variant | |
bullet = "Bullet" in variant | |
horde = "Horde" in variant | |
crazyhouse = "Crazyhouse" in variant | |
correspondence = "Correspondence" in variant | |
if rapid or ultrabullet or classical or blitz or bullet or horde or crazyhouse or correspondence: | |
print str(variant)+", "+str(len(plots[variant]["x"])) | |
plt.plot(x,y) | |
# add legend | |
legend = [key.replace("Rated ","").replace(" game","") for key in plots.keys()] | |
plt.legend(legend,loc="upper left") | |
# add horizontal lines | |
ax = plt.axes() | |
ax.yaxis.grid() | |
# size window | |
plt.gcf().set_size_inches(20, 10, forward=True) | |
plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
games = [] | |
rec= {} | |
keys = [ | |
"[Event ","[Site ","[Date ","[Round ","[White ","[Black ", | |
"[Result ","[UTCDate ","[UTCTime ","[WhiteElo ","[BlackElo ", | |
"[Variant ","[TimeControl ","[ECO ","[Termination ", | |
"[WhiteRatingDiff ","[BlackRatingDiff " | |
] | |
# parse pgn data into dict | |
parse = lambda key, line: line.replace(key,"").replace('"',"").replace("]","").replace("\n","") | |
for line in open("lichess_beefybeefy_2018-09-03.pgn","rb"): | |
for key in keys: | |
if key in line: rec[key.replace("[","").replace(" ","")] = parse(key,line) | |
else: | |
if len(rec.keys()) > 0: | |
games.append(rec) | |
rec = {} | |
# aggregate by date | |
ret = {} | |
for game in games: | |
if "UTCDate" in game.keys(): | |
date = game["UTCDate"] | |
if date not in ret.keys(): ret[date] = 1 | |
else: ret[date] += 1 | |
print ret |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
import datetime | |
import numpy as np | |
from datetime import datetime | |
games = [] | |
rec= {} | |
keys = [ | |
"[Event ","[Site ","[Date ","[Round ","[White ","[Black ", | |
"[Result ","[UTCDate ","[UTCTime ","[WhiteElo ","[BlackElo ", | |
"[Variant ","[TimeControl ","[ECO ","[Termination ", | |
"[WhiteRatingDiff ","[BlackRatingDiff " | |
] | |
# parse pgn data into dict | |
parse = lambda key, line: line.replace(key,"").replace('"',"").replace("]","").replace("\n","") | |
for line in open("lichess_beefybeefy_2018-09-03.pgn","rb"): | |
for key in keys: | |
if key in line: | |
rec[key.replace("[","").replace(" ","")] = parse(key,line) | |
if not any([key in line for key in keys]) and len(rec.keys()) > 0: | |
games.append(rec) | |
rec = {} | |
has_key = lambda key: key in game.keys() | |
has_keys = lambda keys: all([has_key(key) for key in keys]) | |
# rating by date and variant | |
ret = {} | |
plots = {} | |
for game in games: | |
if has_keys(["UTCDate","UTCTime","White","Black","WhiteElo","BlackElo","Event"]): | |
if "Rated" in game["Event"] and "tournament" not in game["Event"]: | |
if game["Event"] not in ret.keys(): | |
ret[game["Event"]] = {} | |
plots[game["Event"]] = {"x":[],"y":[]} | |
date_str = game["UTCDate"] + " " + game["UTCTime"] | |
date = datetime.strptime(date_str,'%Y.%m.%d %H:%M:%S') | |
if game["White"] == "beefybeefy": | |
ret[game["Event"]][date] = game["WhiteElo"] | |
else: | |
ret[game["Event"]][date] = game["BlackElo"] | |
plots[game["Event"]]["x"].append(date) | |
plots[game["Event"]]["y"].append(ret[game["Event"]][date]) | |
for variant in plots.keys(): | |
x = np.array(plots[variant]["x"]) | |
y = np.array(plots[variant]["y"]) | |
plt.plot(x,y) | |
# add legend | |
legend = [key.replace("Rated ","").replace(" game","") for key in plots.keys()] | |
plt.legend(legend,loc="upper left") | |
# add horizontal lines | |
ax = plt.axes() | |
ax.yaxis.grid() | |
# size window | |
plt.gcf().set_size_inches(20, 10, forward=True) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To get games:
https://lichess.org/api#operation/apiGamesUser
https://lichess.org/api/games/user/{username}