-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.py
158 lines (138 loc) · 5.99 KB
/
window.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
import tkinter as tk
import time
from tkinter import filedialog
from crawler import crawlerMain, getTotalWorks
class CrawlerWindow(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.font_style = ("Helvetica", 16)
self.master = master
self.master.title("Pixiv Crawler")
self.master.geometry("600x450")
self.PAD = 5
self.work_amount = 0
self.pack()
self.createWindow()
def selectDirectory(self):
# Everytime when user press select directory button, clean the entry bar first
self.selectDir_entry.delete("0", "end")
# Pop up the files explorer window
self.selectedDir = filedialog.askdirectory()
# Insert the directory path into the entry bar
self.selectDir_entry.insert("end", self.selectedDir)
def startToCraw(self):
# Three places have to be checked: "Directory", "Keyword", "Amount"
dir_OK = keyword_OK = amount_OK = False
if self.getTargetDirectory():
dir_OK = True
self.dir_warn.configure(bg="SystemButtonFace", text="")
else:
self.dir_warn.configure(
bg="red", text="Target directory path cannot be empty!"
)
if self.getKeyword():
keyword_OK = True
self.keyword_warn.configure(bg="SystemButtonFace", text="")
else:
self.keyword_warn.configure(bg="red", text="Keyword cannot be empty!")
if self.getUserAmounts():
if self.getUserAmounts().isdigit():
user_amount = int(self.getUserAmounts())
if user_amount <= self.work_amount and user_amount > 0:
amount_OK = True
self.amount_warn.configure(bg="SystemButtonFace", text="")
else:
self.amount_warn.configure(
bg="red",
text="The integer must equal or less than total works!",
)
else:
self.amount_warn.configure(
bg="red", text="Please input positive integer!"
)
else:
self.amount_warn.configure(bg="red", text="Amount cannot be empty!")
if dir_OK and keyword_OK and amount_OK:
crawlerMain(self.getTargetDirectory(), self.getKeyword(), user_amount)
def getWorkAmounts(self):
if self.getKeyword():
self.work_amount = getTotalWorks(self.getKeyword())
self.amount_work_label.configure(
bg="SystemButtonFace",
text="Total {} works found".format(self.work_amount),
)
else:
self.amount_work_label.configure(bg="red", text="Keyword cannot be empty!")
def getTargetDirectory(self):
return self.selectDir_entry.get()
def getKeyword(self):
return self.search_entry.get()
def getUserAmounts(self):
return self.amount_entry.get()
def createWindow(self):
# Initialize header label
self.header_label = tk.Label(self, text="Pixiv Crawler")
self.header_label.pack()
# Initialize directory path group
self.selectDir_frame = tk.Frame(self)
self.selectDir_frame.pack(side=tk.TOP, pady=self.PAD)
self.selectDir_label = tk.Label(
self.selectDir_frame,
text="Select your directory for saving pictures:",
font=self.font_style,
)
self.selectDir_label.pack(side=tk.TOP)
self.selectDir_entry = tk.Entry(self.selectDir_frame, width=60)
self.selectDir_entry.pack(side=tk.LEFT, padx=self.PAD)
self.selectDir_button = tk.Button(
self.selectDir_frame, text="choose", command=self.selectDirectory
)
self.selectDir_button.pack(side=tk.RIGHT)
# Initialize search group
self.search_frame = tk.Frame(self)
self.search_frame.pack(side=tk.TOP, pady=self.PAD)
self.search_label = tk.Label(
self.search_frame,
text="Type your keyword to get the amount of works:",
font=self.font_style,
)
self.search_label.pack(side=tk.TOP)
self.search_entry = tk.Entry(self.search_frame, width=40)
self.search_entry.pack(side=tk.LEFT, padx=self.PAD)
self.search_button = tk.Button(
self.search_frame, text="search", command=self.getWorkAmounts
)
self.search_button.pack(side=tk.RIGHT)
# Initialize amount_work group
self.amount_work = tk.Frame(self)
self.amount_work.pack(side=tk.TOP, pady=self.PAD)
self.amount_work_label = tk.Label(
self.amount_work,
text="Total {} works found".format(self.work_amount),
font=self.font_style,
)
self.amount_work_label.pack(side=tk.TOP)
# Initialize amount group
self.amount_frame = tk.Frame(self)
self.amount_frame.pack(side=tk.TOP, pady=self.PAD)
self.amount_label = tk.Label(
self.amount_frame,
text="How many works you want to download?",
font=self.font_style,
)
self.amount_label.pack(side=tk.TOP, pady=self.PAD)
self.amount_entry = tk.Entry(self.amount_frame, width=8)
self.amount_entry.pack(side=tk.TOP, pady=self.PAD)
# Initialize download group
self.download_frame = tk.Frame(self)
self.download_frame.pack(side=tk.TOP, pady=self.PAD)
self.download_button = tk.Button(
self.download_frame, text="download", command=self.startToCraw
)
self.download_button.pack(side=tk.TOP, pady=self.PAD)
self.dir_warn = tk.Label(self.download_frame, font=self.font_style)
self.dir_warn.pack(side=tk.TOP, pady=self.PAD)
self.keyword_warn = tk.Label(self.download_frame, font=self.font_style)
self.keyword_warn.pack(side=tk.TOP, pady=self.PAD)
self.amount_warn = tk.Label(self.download_frame, font=self.font_style)
self.amount_warn.pack(side=tk.TOP, pady=self.PAD)