-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpgn_to_json.py
119 lines (92 loc) · 2.92 KB
/
pgn_to_json.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import chess.pgn
import re
import sys
import os.path
import pathlib
import logging
from datetime import datetime
import sys, traceback
log = logging.getLogger().error
for i in [1, 2]:
dir_ = sys.argv[i]
if not os.path.exists(dir_):
raise Exception(dir_ + ' not found')
is_join = False
if len(sys.argv) == 4:
if sys.argv[3] == 'join':
is_join = True
inp_dir = pathlib.Path(sys.argv[1])
out_dir = pathlib.Path(sys.argv[2])
def get_file_list(local_path):
tree = os.walk(str(local_path))
file_list = []
out = []
test = r'.+pgn$'
for i in tree:
file_list = i[2]
for name in file_list:
if(len(re.findall(test, name))):
out.append(str(local_path / name))
return out
def get_data(pgn_file):
node = chess.pgn.read_game(pgn_file)
while node is not None:
data = node.headers
data["moves"] = []
while node.variations:
next_node = node.variation(0)
data["moves"].append(
re.sub("\{.*?\}", "", node.board().san(next_node.move)))
node = next_node
node = chess.pgn.read_game(pgn_file)
out_dict = {}
for key in data.keys():
out_dict[key] = data.get(key)
# log(data.get('Event'))
yield out_dict
def convert_file(file_path):
file_name = file_path.name.replace(file_path.suffix, '') + '.json'
log('convert file '+file_path.name)
out_list = []
try:
json_file = open(str(out_dir / file_name), 'w')
pgn_file = open(str(file_path), encoding='ISO-8859-1')
for count_d, data in enumerate(get_data(pgn_file), start=0):
log(file_path.name+' '+str(count_d))
out_list.append(data)
log(' save '+file_path.name)
json.dump(out_list, json_file)
json_file.close()
log('done')
except Exception as e:
log(traceback.format_exc(10))
log('ERROR file '+file_name+' not converted')
def create_join_file(file_list):
log(' create_join_file ')
name = str(out_dir / 'join_data.json')
open(name, 'w').close()
json_file = open(str(out_dir / 'join_data.json'), 'a')
json_file.write('[')
for count_f, file in enumerate(file_list, start=0):
pgn_file = open(file, encoding='ISO-8859-1')
for count_d, data in enumerate(get_data(pgn_file), start=0):
log(str(count_f)+' '+str(count_d))
if count_f or count_d:
json_file.write(',')
data_str = json.dumps(data)
json_file.write(data_str)
log(pathlib.Path(file).name)
json_file.write(']')
json_file.close()
file_list = get_file_list(inp_dir)
start_time = datetime.now()
if not is_join:
for file in file_list:
convert_file(pathlib.Path(file))
else:
create_join_file(file_list)
end_time = datetime.now()
log('time '+str(end_time-start_time))