-
Notifications
You must be signed in to change notification settings - Fork 39
/
Converter.py
105 lines (93 loc) · 3.17 KB
/
Converter.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
#!/bin/python
# encoding=utf8
import sys
import platform
def kifu_converter(index_name, english_user_id, kifu_folder, save_folder):
f = open(index_name)
stack = []
save_index = ""
for line in f:
line = line.decode('utf-8')
line_r = line.split('\t')
if line_r[6] != english_user_id and line_r[3] != english_user_id:
continue
save_index += line
Num = line_r[0]
fn_sgf = line_r[1][1:5] + "-" + line_r[1][6:8]
DT = line_r[1][1:]
if line_r[2] == line_r[3]:
PW = line_r[2]
else:
PW = line_r[2] + ' ' + line_r[3]
if line_r[5] == line_r[6]:
PB = line_r[5]
else:
PB = line_r[5] + ' ' + line_r[6]
RE = line_r[8]
RO = line_r[9]
TM = line_r[10].split(' ')[1][0:-2] + " min"
OT = line_r[10].split(' ')[0]
OT = OT.split('/')[1] + "x " + OT.split('/')[0] + " sec"
SGF = "(;CA[UTF-8]GM[1]FF[4]AP[converter]\nSZ[19]"
SGF += "GN[" + Num + "]\n"
SGF += "DT[" + DT + "]\n"
SGF += "PB[" + PB + "]" + "BR[9d]\n"
SGF += "PW[" + PW + "]" + "WR[9d]\n"
SGF += "RE[" + RE + "]"
SGF += "RO[" + RO + "]"
SGF += "KM[6.5]"
SGF += "RU[Japanese]"
SGF += "TM[" + TM + "]" + "OT[" + OT + "]\n"
SGF += "PC[TYGEM]"
stack.append([SGF, fn_sgf, Num])
f.close()
writer = open(english_user_id + ".index", "w")
writer.write(save_index.encode('utf-8'))
writer.close()
if platform.platform() == "Windows":
if kifu_folder[-1] != "\\":
kifu_folder += "\\"
if save_folder[-1] != "\\":
save_folder += "\\"
else:
if kifu_folder[-1] != "/":
kifu_folder += "/"
if save_folder[-1] != "/":
save_folder += "/"
i = 0
stack_size = len(stack)
while i < stack_size:
info = stack[i]
SGF = info[0]
fn_sgf = info[1]
fn_open = fn_sgf
Num = info[2]
f_sgf = open(kifu_folder + fn_sgf)
for line_sgf in f_sgf:
line_sgf = line_sgf.decode('utf-8')
split = line_sgf.split('\t')
if split[0] == Num:
SGF += split[1] + ")"
writer = open(save_folder + Num + ".SGF", "w")
writer.write(SGF.encode('utf-8'))
writer.close()
if i + 1 >= stack_size:
break
if fn_open != stack[i + 1][1]:
break
i += 1
info = stack[i]
SGF = info[0]
fn_sgf = info[1]
Num = info[2]
f_sgf.close()
i += 1
if len(sys.argv) != 5:
print "usage: python Converter.py kifu.index english_user_id kifu_folder save_folder"
print "example: python Converter.py kifu.index Lurk(P) Kifu Save"
else:
index_name = sys.argv[1]
english_user_id = sys.argv[2]
kifu_folder = sys.argv[3]
save_folder = sys.argv[4]
kifu_converter(index_name, english_user_id, kifu_folder, save_folder)