-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
import-master.py
executable file
·65 lines (51 loc) · 1.63 KB
/
import-master.py
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
#!/usr/bin/env python3
import base64
import chess
import chess.pgn
import hashlib
import json
import requests
import sys
def main(pgn):
session = requests.session()
while True:
game = chess.pgn.read_game(pgn)
if game is None:
break
obj = {
"event": game.headers["Event"],
"site": game.headers["Site"],
"date": game.headers["Date"],
"round": game.headers["Round"],
"white": {
"name": game.headers["White"],
"rating": int(game.headers["WhiteElo"]),
},
"black": {
"name": game.headers["Black"],
"rating": int(game.headers["BlackElo"]),
},
"winner": winner(game),
"moves": " ".join(m.uci() for m in game.end().board().move_stack)
}
obj["id"] = game.headers.get("LichessId") or deterministic_id(obj)
res = session.put("http://localhost:9002/import/masters", json=obj)
if res.status_code != 200:
print(res.text)
else:
print(obj["id"])
def winner(game):
if game.headers["Result"] == "1-0":
return "white"
elif game.headers["Result"] == "0-1":
return "black"
elif game.headers["Result"] == "1/2-1/2":
return None
else:
assert False, "invalid result"
def deterministic_id(obj):
digest = hashlib.sha256()
digest.update(json.dumps(obj, sort_keys=True).encode("utf-8"))
return base64.b64encode(digest.digest(), b"ab")[0:8].decode("utf-8")
if __name__ == "__main__":
main(open(sys.argv[1], errors="ignore"))