-
Notifications
You must be signed in to change notification settings - Fork 21
/
gui.py
486 lines (401 loc) · 17.4 KB
/
gui.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
# gui.py
import Tkinter
import tkFileDialog
import tkSimpleDialog
import tkMessageBox
import requests
import subprocess
import os
import functools
wraps = functools.wraps
import settings
import scrape
import itertools
SELECTION_COLOR = '#9999ff'
BLANK_COLOR = '#ffffff'
def gui(fn):
@wraps(fn)
def updater(self, *args, **kwargs):
val = fn(self, *args, **kwargs)
self.update_gui()
return val
return updater
class ScraperWindow(object):
def __init__(self):
self.root = self.get_root()
self.state = GUIState()
self.filetype = None
self.settings = settings.Settings()
self.add_elements()
self.update_gui()
self.root.after(100, self.center_window)
self.start_timer()
self.root.mainloop()
def start_timer(self):
self.prev_state = self.state.tuple()
self.prev_entry = ''
self.root.after(100, self.timer)
def timer(self):
if self.number_of_files_entry.get() != self.prev_entry:
num = self._int_in_entry(self.number_of_files_entry)
if num is not None:
self.subreddit.num_files = num
self._entry_update(self.number_of_files_entry, self.subreddit.num_files if
self.subreddit else '')
self.prev_entry = self.number_of_files_entry.get()
self.grouping_listbox.selection_clear(0, Tkinter.END)
self.subreddit_listbox.selection_clear(0, Tkinter.END)
self.file_types_listbox.selection_clear(0, Tkinter.END)
if not self.prev_state == self.state.tuple():
self.update_gui()
self.prev_state = self.state.tuple()
self.root.after(100, self.timer)
def get_root(self):
"""Create a new window."""
root = Tkinter.Tk()
root.title("Reddit Scraper")
return root
def center_window(self):
w = float(self.root.winfo_screenwidth())
h = float(self.root.winfo_screenheight())
rootsize = tuple(int(i) for i in self.root.geometry().split('+')[0].split('x'))
x = w/2 - rootsize[0]/2
y = h/2 - rootsize[1]/2
self.root.geometry("%dx%d+%d+%d" % (rootsize + (x, y)))
def scrape_all(self):
self._scrapes(None, None)
def scrape_current_sub(self):
if self.subreddit:
self._scrapes([self.subreddit.name], None)
else:
tkMessageBox.askokcancel("", "Please select a subreddit to scrape.")
def scrape_current_dir(self):
if self.grouping:
self._scrapes(None, [self.grouping.name])
else:
tkMessageBox.askokcancel("", "Please select a directory to scrape.")
def _scrapes(self, include_sub, include_dir, expose=True, alert_when_done=True):
try:
count = 0
for x in scrape.scrape(self.settings, include_sub=include_sub, include_dir=include_dir):
if isinstance(x, int):
count += x
continue
if expose:
reveal(x)
except requests.ConnectionError:
tkMessageBox.askokcancel("Connection Error",
"Could not connect to Reddit. Check your internet settings, "
"and make sure Reddit isn't down.")
else:
tkMessageBox.askokcancel("", "Scrape Complete! %d files downloaded." % count)
def add_elements(self):
root = self.root
self.add_groupings_pane(root)
self.add_subreddits_pane(root)
self.add_details_pane(root)
def add_groupings_pane(self, root):
pane = Tkinter.Frame(root)
# List of directories,
self.grouping_listbox = Tkinter.Listbox(pane)
self.grouping_listbox.grid(row=0, column=0, sticky=Tkinter.N)
self.grouping_listbox.bind('<Button-1>', self.grouping_listbox_click)
row_1_frame = Tkinter.Frame(pane)
# Add directory button,
add_dir_button = Tkinter.Button(row_1_frame, text=" + ", command=self.add_directory)
add_dir_button.pack(side=Tkinter.LEFT)
# Reomve directory button,
del_dir_button = Tkinter.Button(row_1_frame, text=" - ", command=self.del_directory)
del_dir_button.pack(side=Tkinter.LEFT)
# Enable/Disable button
self.enable_button = Tkinter.Button(row_1_frame, text="Disable", command=self.enable)
self.enable_button.pack(side=Tkinter.LEFT)
row_1_frame.grid(row=1, column=0)
# Scrape all button
scrape_all_button = Tkinter.Button(pane, text="Scrape All", command=self.scrape_all)
scrape_all_button.grid(row=2, column=0)
row_3_frame = Tkinter.Frame(pane)
# Load/Save
load_button = Tkinter.Button(row_3_frame, text="Load", command=self.load)
load_button.pack(side = Tkinter.LEFT)
save_button = Tkinter.Button(row_3_frame, text="Save", command=self.save)
save_button.pack(side=Tkinter.LEFT)
row_3_frame.grid(row=3, column=0)
pane.grid(row=0, column=0)
def add_subreddits_pane(self, root):
pane = Tkinter.Frame(root)
# List of subreddits,
self.subreddit_listbox = Tkinter.Listbox(pane)
self.subreddit_listbox.grid(row=0, column=0, sticky=Tkinter.N)
self.subreddit_listbox.bind('<Button-1>', self.subreddit_listbox_click)
row_1_frame = Tkinter.Frame(pane)
# Add subreddit button
add_sub_button = Tkinter.Button(row_1_frame, text=" + ", command=self.add_subreddit)
add_sub_button.pack(side=Tkinter.LEFT)
# Remove subreddit button
del_sub_button = Tkinter.Button(row_1_frame, text=" - ", command=self.del_subreddit)
del_sub_button.pack(side=Tkinter.LEFT)
# Enable/Disable button
self.enable_sub_button = Tkinter.Button(row_1_frame, text="Disable", command=self.enable_sub)
self.enable_sub_button.pack(side=Tkinter.LEFT)
row_1_frame.grid(row=1, column=0)
self.scrape_dir_button = Tkinter.Button(pane, text="Scrape Directory", command=self.scrape_current_dir)
self.scrape_dir_button.grid(row=2, column=0)
# Create folder for each subreddit (checkbox)
self.persub = Tkinter.Checkbutton(pane, command=self.persub)
self.persub.grid(row=3, column=0)
pane.grid(row=0, column=1)
def add_details_pane(self, root):
pane = Tkinter.Frame(root, width=500)
# Subreddit name
self.subreddit_name_label = Tkinter.Label(pane, text='', anchor=Tkinter.N)
self.subreddit_name_label.grid(row=0, column=0)
# Number of files to download (INC/DEC)
row_1_frame = Tkinter.Frame(pane)
self.number_of_files_label = Tkinter.Label(row_1_frame, text='Files to Download:')
self.number_of_files_label.pack(side=Tkinter.LEFT)
self.number_of_files_entry = Tkinter.Entry(row_1_frame, width=3)
self.number_of_files_entry.pack(side=Tkinter.LEFT)
row_1_frame.grid(row=1, column=0)
# File types to include (list box?) (ADD/REM)
self.file_types_label = Tkinter.Label(pane, text="Include these extensions:")
self.file_types_label.grid(row=2, column=0)
self.file_types_listbox = Tkinter.Listbox(pane, height=5)
self.file_types_listbox.grid(row=3, column=0)
self.file_types_listbox.bind('<Button-1>', self.file_types_listbox_click)
row_4_frame = Tkinter.Frame(pane)
add_ext_button = Tkinter.Button(row_4_frame, text=' + ', command=self.add_ext)
add_ext_button.pack(side=Tkinter.LEFT)
del_ext_button = Tkinter.Button(row_4_frame, text=' - ', command=self.del_ext)
del_ext_button.pack(side=Tkinter.LEFT)
row_4_frame.grid(row=4, column=0)
# Last scraped (LABEL)
# Open folder in finder/explorer (BUTTON)
# Scrape now button
self.scrape_now_button = Tkinter.Button(pane, text="Scrape Subreddit", command=self.scrape_current_sub)
self.scrape_now_button.grid(row=5, column=0)
pane.grid(row=0, column=2)
@gui
def grouping_listbox_click(self, event):
index = self.grouping_listbox.nearest(event.y)
self.state.grouping = self.grouping_listbox.get(index)
@gui
def subreddit_listbox_click(self, event):
if self.grouping is None:
return
index = self.subreddit_listbox.nearest(event.y)
self.state.subreddit = self.subreddit_listbox.get(index)
@gui
def file_types_listbox_click(self, event):
if self.subreddit is None:
return
index = self.file_types_listbox.nearest(event.y)
self.filetype = self.file_types_listbox.get(index)
if self.filetype == '<Extensions>':
self.filetype = None
def persub(self):
print "BAM"
print self.persub.cget('onvalue')
print self.persub.cget('variable')
@property
def grouping(self):
if self.state.grouping is None: return None
return self.settings[self.state.grouping]
@property
def subreddit(self):
if self.state.subreddit is None: return None
return self.grouping[self.state.subreddit]
def update_gui(self):
## Grouping listbox
desired_grouping_listbox = [group.shortname for group in self.settings.groupings]
if not desired_grouping_listbox:
desired_grouping_listbox = ['<Directories>']
self._listbox_update(self.grouping_listbox, desired_grouping_listbox)
# Color
for i in xrange(self.grouping_listbox.size()):
grp = self.settings[self.grouping_listbox.get(i)]
if grp is None or grp.enabled:
fg_color = 'black'
else:
fg_color = 'gray'
self.grouping_listbox.itemconfig(i, bg=BLANK_COLOR, fg=fg_color)
if self.state.grouping:
index = self.grouping_listbox.get(0, Tkinter.END).index(self.state.grouping)
self.grouping_listbox.itemconfig(index, bg=SELECTION_COLOR)
self.grouping_listbox.selection_clear(0, Tkinter.END)
## Enable buttons
self.enable_button.config(text=' ' if not self.grouping else
'Disable' if self.grouping.enabled else 'Enable')
self.enable_sub_button.config(text=' ' if not self.subreddit else
'Disable' if self.subreddit.enabled else 'Enable')
## Subreddit listbox
if self.grouping is None:
desired_subreddit_listbox = ['<Subreddits>']
else:
desired_subreddit_listbox = [sub.name for sub in self.grouping.subreddits]
if not desired_subreddit_listbox:
desired_subreddit_listbox = ['<Subreddits>']
self._listbox_update(self.subreddit_listbox, desired_subreddit_listbox)
# Color
for i in xrange(self.subreddit_listbox.size()):
sub = None if not self.grouping else self.grouping[self.state.subreddit]
if sub is None or sub.enabled:
fg_color = 'black'
else:
fg_color = 'gray'
self.subreddit_listbox.itemconfig(i, bg=BLANK_COLOR, fg=fg_color)
if self.state.subreddit:
index = self.subreddit_listbox.get(0, Tkinter.END).index(self.state.subreddit)
self.subreddit_listbox.itemconfig(index, bg=SELECTION_COLOR)
self.subreddit_listbox.selection_clear(0, Tkinter.END)
## Details Frame
if self.subreddit is not None:
self.subreddit_name_label.config(text='/r/'+self.state.subreddit)
#self._entry_update(self.number_of_files_entry, self.ubreddit.num_files)
desired_filetypes = self.subreddit.file_types
self._listbox_update(self.file_types_listbox, desired_filetypes)
else:
self.subreddit_name_label.config(text='')
self.filetype = None
#self._entry_update(self.number_of_files_entry, '')
self._listbox_update(self.file_types_listbox, ['<Extensions>'])
## Color
for i in xrange(self.file_types_listbox.size()):
self.file_types_listbox.itemconfig(i, bg=BLANK_COLOR)
if self.filetype:
index = self.file_types_listbox.get(0, Tkinter.END).index(self.filetype)
self.file_types_listbox.itemconfig(index, bg=SELECTION_COLOR)
def _listbox_update(self, listbox, desired_values):
current_values = listbox.get(0, Tkinter.END)
to_remove = set(current_values) - set(desired_values)
to_add = set(desired_values) - set(current_values)
for item in to_remove:
idx = listbox.get(0, Tkinter.END).index(item)
listbox.delete(idx)
for item in to_add:
listbox.insert(Tkinter.END, item)
def _entry_update(self, entry, desired_text):
desired_text = str(desired_text)
if entry.get() != desired_text:
entry.delete(0, Tkinter.END)
entry.insert(0, desired_text)
def _int_in_entry(self, entry):
contents = entry.get()
if contents == '': return 0
contents = ''.join(c for c in contents if c in '0123456789.')
if contents == '': return None
return int(float(contents))
@gui
def add_subreddit(self):
if self.grouping is None:
tkMessageBox.askokcancel("", "Please select a directory first.")
else:
s = tkSimpleDialog.askstring("Add a Subreddit", "Enter the name of the subreddit: /r/")
if not s:
return
if s.startswith('/r/'):
s = s[len('/r/'):]
self.grouping.add_subreddit(s)
@gui
def del_subreddit(self):
if self.subreddit:
current_items = list(self.subreddit_listbox.get(0, Tkinter.END))
current_idx = current_items.index(self.subreddit.name)
del self.grouping[self.subreddit.name]
del current_items[current_idx]
if current_items:
new_idx = max(0, min(len(current_items)-1, current_idx))
self.state.subreddit = current_items[new_idx]
else:
self.state.subreddit = None
else:
tkMessageBox.askokcancel("", "Please select a subreddit to remove.")
@gui
def add_ext(self):
if self.subreddit is None:
tkMessageBox.askokcancel("", "Please select a subreddit first.")
else:
s = tkSimpleDialog.askstring("Add an extension", "Enter the extension:")
if not s:
return
s = s.upper().strip('.')
self.subreddit.add_file_type(s)
@gui
def del_ext(self):
if self.filetype:
current_items = list(self.file_types_listbox.get(0, Tkinter.END))
current_idx = current_items.index(self.filetype)
self.subreddit.rm_filetype(self.filetype)
del current_items[current_idx]
if current_items:
new_idx = max(0, min(len(current_items)-1, current_idx))
self.filetype = current_items[new_idx]
else:
self.filetype = None
else:
tkMessageBox.askokcancel("", "Please select an extension to remove.")
@gui
def save(self):
self.settings.save()
@gui
def load(self, explicit=True):
try:
self.settings.load(explicit)
except ValueError:
reset = tkMessageBox.askokcancel("", "Cannot read your config file. Reset?")
if reset:
self.settings.reset()
except NameError:
reset = tkMessageBox.askokcancel("", "No config file found. Create a new one?")
if reset:
self.settings.reset()
self.state = GUIState()
@gui
def add_directory(self):
dirname = tkFileDialog.askdirectory()
if dirname:
self.settings.add_grouping(dirname)
self.state.grouping = self.settings[dirname].shortname
@gui
def del_directory(self):
if self.grouping:
current_items = list(self.grouping_listbox.get(0, Tkinter.END))
current_idx = current_items.index(self.grouping.shortname)
del self.settings[self.state.grouping]
del current_items[current_idx]
if current_items:
new_idx = max(0, min(len(current_items)-1, current_idx))
self.state.grouping = current_items[new_idx]
else:
self.state.grouping = None
else:
tkMessageBox.askokcancel("", "Please select a directory to remove.")
@gui
def enable(self):
if self.grouping:
self.grouping.enable()
@gui
def enable_sub(self):
if self.subreddit:
self.subreddit.enable()
class GUIState(object):
def __init__(self, grouping=None, subreddit=None):
self.grouping = grouping
self.subreddit = subreddit
def tuple(self):
return (self.grouping, self.subreddit)
def reveal(direc, raised=[False]):
if raised[0]:
# No need to try again - we already know an exception will
# be raised.
return
if not os.path.exists(direc):
raise ValueError("Could not find %s" % direc)
try:
if os.path.isdir(direc):
subprocess.call(['open', direc])
else:
subprocess.call(['open', '-R', direc])
except:
# Non-unixy
raised[0] = True