-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChess.com Game Downloader(GUI).pyw
254 lines (200 loc) · 8.42 KB
/
Chess.com Game Downloader(GUI).pyw
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import tkinter as tk
from tkinter import ttk
from codecs import open
from datetime import date
import os.path
import requests
import os
import sys, time
dictn = {'All Games': 'All Games', 'Bullet': 'bullet', 'Blitz': 'blitz', 'Rapid': 'rapid', 'Daily Chess': 'daily'}
dictb = {'Small, Only Wins': 0, 'Small, All Games': 1, 'Large, Only Wins': 2,'Large, All Games': 3}
def resource_path(relative_path):
""" Get the absolute path to the resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
exedir = os.getcwd()
updir = resource_path("")
def download_games():
user=name_entry.get()
if(opp_entry.get() == ""):
where = os.path.join(loc_entry.get(), "%s's %s Archive %s" % (user, event.get(), date.today()))
else:
where = os.path.join(loc_entry.get(), "%s's vs %s %s Archive %s" % (user, opp_entry.get(), event.get(), date.today()))
if not os.path.exists(where + ".pgn"):
where += ".pgn"
open(where, 'w').close()
else:
i=1
while(os.path.exists(where + "(%s).pgn" % (i))):
i+=1
where = where + "(%d).pgn" % (i)
open(where, 'w').close()
for archive in get('https://api.chess.com/pub/player/%s/games/archives' % user)['archives']:
aa(archive, where)
def mob():
user=name_entry.get()
obwp = os.path.join(updir, "White%s" % (user))
obbp = os.path.join(updir, "Black%s" % (user))
if (loc_entry.get() == ""):
finalfile = os.path.join(exedir, "%s" % (user))
else:
finalfile = os.path.join(loc_entry.get(), "%s" % (user))
if not os.path.exists(obwp + ".pgn"):
obwp += ".pgn"
open(obwp, 'w').close()
else:
i=1
while(os.path.exists(obwp + "(%s).pgn" % (i))):
i+=1
obwp = obwp + "(%d).pgn" % (i)
open(obwp, 'w').close()
if not os.path.exists(obbp + ".pgn"):
obbp += ".pgn"
open(obbp, 'w').close()
else:
i=1
while(os.path.exists(obwp + "(%s).pgn" % (i))):
i+=1
obbp = obbp + "(%d).pgn" % (i)
open(obbp, 'w').close()
for archive in get('https://api.chess.com/pub/player/%s/games/archives' % user)['archives']:
obw(archive, obwp)
obb(archive, obbp)
try:
os.chdir(updir)
os.system("pgn-extract -s -C -N -V -tstartpos.txt -owclean.pgn %s" % (obwp))
os.system("pgn-extract -s -C -N -V -tstartpos.txt -obclean.pgn %s" % (obbp))
if (dictb[bo.get()] == 0 or dictb[bo.get()] == 1):
os.system("polyglot make-book -only-white -pgn wclean.pgn -bin w1.bin -max-ply 32 -min-game 25")
os.system("polyglot make-book -only-white -pgn wclean.pgn -bin w2.bin -max-ply 60 -min-game 5")
os.system("polyglot make-book -only-black -pgn bclean.pgn -bin b1.bin -max-ply 32 -min-game 25")
os.system("polyglot make-book -only-black -pgn bclean.pgn -bin b2.bin -max-ply 60 -min-game 5")
else:
os.system("polyglot make-book -only-white -pgn wclean.pgn -bin w1.bin")
os.system("polyglot make-book -only-white -pgn wclean.pgn -bin w2.bin")
os.system("polyglot make-book -only-black -pgn bclean.pgn -bin b1.bin")
os.system("polyglot make-book -only-black -pgn bclean.pgn -bin b2.bin")
os.system("polyglot merge-book -in1 w1.bin -in2 w2.bin -out w12.bin")
os.system("polyglot merge-book -in1 b1.bin -in2 b2.bin -out b12.bin")
os.system("polyglot merge-book -in1 w12.bin -in2 b12.bin -out %s.bin" % (finalfile))
os.remove(obwp)
os.remove("wclean.pgn")
os.remove("w1.bin")
os.remove("w2.bin")
os.remove("w12.bin")
os.remove(obbp)
os.remove("bclean.pgn")
os.remove("b1.bin")
os.remove("b2.bin")
os.remove("b12.bin")
os.chdir(exedir)
except:
pass
def aa(url, where):
games = get(url)['games']
with open(where, 'a+', encoding='utf-8') as output:
for game in games:
try:
if (game['rules'] == 'chess'):
if (opp_entry.get() == ""):
if (event.get() == "All Games"):
print(game['pgn'], file=output)
print('', file=output)
else:
if(dictn[event.get()] == game['time_class']):
print(game['pgn'], file=output)
print('', file=output)
else:
if (game['white']['username'] == opp_entry.get() or game['black']['username'] == opp_entry.get()):
if (event.get() == "All Games"):
print(game['pgn'], file=output)
print('', file=output)
else:
if(dictn[event.get()] == game['time_class']):
print(game['pgn'], file=output)
print('', file=output)
except:
pass
def obw(url, where):
user = name_entry.get()
games = get(url)['games']
with open(where, 'a+', encoding='utf-8') as output:
for game in games:
try:
if (game['rules'] == 'chess'):
if (game['white']['username'] == user):
if (dictb[bo.get()] == 0 or dictb[bo.get()] == 2):
if (game['white']['result'] == 'win'):
print(game['pgn'], file=output)
print('', file=output)
else:
print(game['pgn'], file=output)
print('', file=output)
except:
pass
def obb(url, where):
user = name_entry.get()
games = get(url)['games']
with open(where, 'a+', encoding='utf-8') as output:
for game in games:
try:
if (game['rules'] == 'chess'):
if (game['black']['username'] == user):
if (dictb[bo.get()] == 0 or dictb[bo.get()] == 2):
if (game['black']['result'] == 'win'):
print(game['pgn'], file=output)
print('', file=output)
else:
print(game['pgn'], file=output)
print('', file=output)
except:
pass
def get(url):
return requests.get(url).json()
window = tk.Tk()
window.title("Chess.com Game Downloader")
frame_a = tk.Frame(padx=20, pady=10)
name = tk.Label(master=frame_a, text="Account Name", padx=10)
name_entry = tk.Entry(master=frame_a)
name.pack(side=tk.LEFT)
name_entry.pack(side=tk.LEFT)
frame_b = tk.Frame(padx=20, pady=10)
opp = tk.Label(master=frame_b, text="Opponent's Name", padx=10)
opp_entry = tk.Entry(master=frame_b)
opp.pack(side=tk.LEFT)
opp_entry.pack(side=tk.LEFT)
frame_c = tk.Frame(padx=20, pady=10)
location = tk.Label(master=frame_c, text="File Destination", padx=10)
loc_entry = tk.Entry(master=frame_c)
location.pack(side=tk.LEFT)
loc_entry.pack(side=tk.LEFT)
frame_d = tk.Frame(padx=20, pady=10)
tr = tk.Label(master=frame_d, text="Time Rules :", padx=10)
event = ttk.Combobox(master=frame_d, state="readonly", values = ['All Games', 'Bullet', 'Blitz', 'Rapid', 'Daily Chess'])
event.current(0)
tr.pack(side=tk.LEFT)
event.pack(side=tk.LEFT)
frame_e = tk.Frame(padx=20, pady=10)
bot = tk.Label(master=frame_e, text="Book Options :", padx=10)
bo = ttk.Combobox(master=frame_e, state="readonly", values = ['Small, Only Wins', 'Small, All Games', 'Large, Only Wins','Large, All Games'])
bo.current(0)
bot.pack(side=tk.LEFT)
bo.pack(side=tk.LEFT)
frame_ob = tk.Frame(padx=20, pady=10)
ob = tk.Button(master=frame_ob, text="Make Opening Book", command=mob)
ob.pack()
frame_end = tk.Frame(padx=20, pady=10)
button = tk.Button(master=frame_end, text="Download Games", command=download_games)
button.pack()
frame_a.pack()
frame_b.pack()
frame_c.pack()
frame_d.pack()
frame_e.pack()
frame_ob.pack()
frame_end.pack()
window.mainloop()