-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.py
328 lines (289 loc) · 12.1 KB
/
MainWindow.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
import re
import xlwt
import sqlite3
import action
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtCore import QThread, pyqtSignal, QSettings, QSize
from PyQt5.QtGui import QColor
from PyQt5.QtWidgets import QFileDialog, QProgressBar
from pyqtspinner import WaitingSpinner
class RequestThread(QThread):
request_completed = pyqtSignal(str)
def __init__(self, handler):
super().__init__()
self.ui_handler = handler
self.total_count = 350000
def run(self):
self.request_completed.emit("start")
# result = self.ui_handler.product_list_download_from_amazon()
# print(type(result) == str)
# if(type(result) == str):
# self.request_completed.emit(result)
# self.request_completed.emit("stop")
# print(result)
# total = result['total']
# document_id = result['filepath']
# self.request_completed.emit("reading")
# result = self.ui_handler.read_product_list_from_file(document_id)
# if(result != 'success'):
# self.request_completed.emit(result)
# self.request_completed.emit('stop')
cur_position = 0
while cur_position < self.total_count:
if not self.ui_handler.main_window.isStop:
try:
cur_position += 1
if(cur_position == 150000 or cur_position == 300000):
self.ui_handler.cur_page = 0
self.ui_handler.cur_page += 1
# product_list = self.ui_handler.get_product_info_by_product_list(cur_position)
product_list = self.ui_handler.get_products_list(cur_position)
# key_arr = [['4580128895130', '', '', '10000'], ['4580128895383', '', '', '10000'], ['4988067000125', '', '', '10000']]
for product in product_list:
cur_position += 1
if(product[0] != ''):
self.ui_handler.get_product_url(product, cur_position)
progress = 100 / self.total_count * cur_position
self.request_completed.emit(str(progress))
except Exception as e:
self.request_completed.emit(e)
else:
self.request_completed.emit("stop")
self.quit()
break
self.request_completed.emit("stop")
self.quit()
class Ui_MainWindow(object):
keyword_arr = []
def __init__(self):
super().__init__()
self.total_count = 350000
self.spinner = None
self.progressBar = None
self.statusLabel = None
self.tbl_dataview = None
self.btn_start = None
self.horizontalLayout = None
self.horizontalLayout_2 = None
self.horizontalLayout_3 = None
self.btn_export = None
self.btn_history = None
self.gridLayout = None
self.centralwidget = None
self.verticalLayout = None
self.isStop = True
self.settings = None
self.cmb_export_type = None
self.request_thread = None
self.ui_handler = action.ActionManagement(self)
# create GUI
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setWindowModality(QtCore.Qt.ApplicationModal)
MainWindow.setEnabled(True)
MainWindow.resize(820, 600)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
MainWindow.setSizePolicy(sizePolicy)
MainWindow.setMinimumSize(QtCore.QSize(820, 600))
MainWindow.setAcceptDrops(False)
MainWindow.setLayoutDirection(QtCore.Qt.LeftToRight)
MainWindow.setAutoFillBackground(True)
MainWindow.setAnimated(False)
MainWindow.setDocumentMode(False)
MainWindow.setTabShape(QtWidgets.QTabWidget.Triangular)
MainWindow.setDockNestingEnabled(False)
MainWindow.setUnifiedTitleAndToolBarOnMac(False)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setEnabled(True)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.centralwidget.sizePolicy().hasHeightForWidth())
self.centralwidget.setSizePolicy(sizePolicy)
self.centralwidget.setLayoutDirection(QtCore.Qt.LeftToRight)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setSizeConstraint(QtWidgets.QLayout.SetMinAndMaxSize)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setSpacing(12)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.btn_start = QtWidgets.QPushButton(self.centralwidget)
self.btn_start.setMinimumSize(QtCore.QSize(16777215, 30))
self.btn_start.setMaximumSize(QtCore.QSize(16777215, 30))
self.btn_start.setObjectName("btn_start")
self.btn_start.setText("開始")
self.btn_start.clicked.connect(self.handle_btn_start_clicked)
self.horizontalLayout_2.addWidget(self.btn_start)
self.btn_export = QtWidgets.QPushButton(self.centralwidget)
self.btn_export.setMinimumSize(QtCore.QSize(16777215, 30))
self.btn_export.setMaximumSize(QtCore.QSize(16777215, 30))
self.btn_export.setObjectName("btn_export")
self.btn_export.setText("出力")
self.btn_export.setEnabled(False)
self.btn_export.clicked.connect(self.savefile)
self.horizontalLayout_2.addWidget(self.btn_export)
self.btn_history = QtWidgets.QPushButton(self.centralwidget)
self.btn_history.setMinimumSize(QtCore.QSize(16777215, 30))
self.btn_history.setMaximumSize(QtCore.QSize(16777215, 30))
self.btn_history.setObjectName("btn_history")
self.btn_history.setText("以前の履歴出力")
self.btn_history.setEnabled(True)
self.btn_history.clicked.connect(self.savefile)
self.horizontalLayout_2.addWidget(self.btn_history)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.tbl_dataview = QtWidgets.QTableView(self.centralwidget)
self.tbl_dataview.setObjectName("tbl_dataview")
self.tbl_dataview.setMaximumWidth(16777215)
self.tbl_dataview.setMinimumWidth(16777215)
self.tbl_dataview.doubleClicked.connect(self.handle_cell_click)
header_labels = ["JAN", "URL", "在庫", "サイト価格", "Amazonの価格", "価格差"]
model = QtGui.QStandardItemModel(0, 5)
model.setHorizontalHeaderLabels(header_labels)
self.tbl_dataview.setModel(model)
self.horizontalLayout.addWidget(self.tbl_dataview)
self.verticalLayout.addLayout(self.horizontalLayout)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName('horizontalLayout_3')
self.statusLabel = QtWidgets.QLabel(self.centralwidget)
self.statusLabel.setMinimumSize(QtCore.QSize(16777215, 20))
self.statusLabel.setMaximumSize(QtCore.QSize(16777215, 50))
self.statusLabel.setVisible(True)
self.horizontalLayout_3.addWidget(self.statusLabel)
self.verticalLayout.addLayout(self.horizontalLayout_3)
self.progressBar = QProgressBar(self)
self.progressBar.setVisible(False)
self.verticalLayout.addWidget(self.progressBar)
self.gridLayout.addLayout(self.verticalLayout, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.spinner = WaitingSpinner(
parent = self.tbl_dataview,
disable_parent_when_spinning = False,
roundness = 100,
fade = 88,
radius = 29,
lines = 58,
line_length = 11,
line_width = 14,
speed = 1,
color = QColor(0, 0, 0, 255)
)
self.spinner.setObjectName("spinner")
self.retranslateUi(MainWindow)
MainWindow.setWindowIcon(QtGui.QIcon('delivery.ico'))
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.loadSettings()
def closeEvent(self, event):
self.saveSettings()
event.accept()
def loadSettings(self):
# Load the previous settings using QSettings
settings = QSettings("YourCompany", "YourApp")
self.resize(settings.value("WindowSize", QSize(800, 600)))
for col in range(self.tbl_dataview.model().columnCount()):
width = settings.value(f"columnWidth_{col}", type = int, defaultValue = 100) # Set a default width if not found
self.tbl_dataview.setColumnWidth(col, width)
def saveSettings(self):
# Save the current window size and column state using QSettings
settings = QSettings("YourCompany", "YourApp")
settings.setValue("WindowSize", self.size())
for col in range(self.tbl_dataview.model().columnCount()):
width = self.tbl_dataview.columnWidth(col)
settings.setValue(f"columnWidth_{col}", width)
def handle_cell_click(self, index):
row = index.row()
col = index.column()
if col == 1 and self.ui_handler.products_list[row] != "":
import webbrowser
webbrowser.open(self.ui_handler.products_list[row]['url'])
def handle_btn_start_clicked(self):
if self.isStop:
self.btn_start.setText("停止")
self.ui_handler.products_list = []
self.request_thread = RequestThread(self.ui_handler)
self.request_thread.request_completed.connect(self.handle_request_completed)
self.request_thread.start()
self.isStop = False
else:
self.isStop = True
self.statusLabel.setVisible(True)
self.progressBar.setVisible(False)
self.btn_start.setEnabled(False)
self.spinner.stop()
self.request_thread.exit()
def savefile(self):
filename, _ = QFileDialog.getSaveFileName(self, 'Save File', '', ".xls(*.xls)")
if filename:
wbk = xlwt.Workbook()
sheet = wbk.add_sheet("sheet", cell_overwrite_ok = True)
style = xlwt.XFStyle()
font = xlwt.Font()
font.bold = True
style.font = font
model = self.tbl_dataview.model()
sheet.write(0, 0, 'JAN', style = style)
sheet.write(0, 1, 'URL', style = style)
sheet.write(0, 2, '在庫', style = style)
sheet.write(0, 3, 'サイト価格', style = style)
sheet.write(0, 4, 'Amazonの価格', style = style)
sheet.write(0, 5, '価格差', style = style)
row_count = 0
# for r in range(model.rowCount()):
# sheet.write((row_count + 1), 0, model.data(model.index(row_count, 0)), style = style)
# sheet.write((row_count + 1), 1, model.data(model.index(row_count, 1)), style = style)
# sheet.write((row_count + 1), 2, model.data(model.index(row_count, 2)), style = style)
# sheet.write((row_count + 1), 3, model.data(model.index(row_count, 3)), style = style)
# sheet.write((row_count + 1), 4, model.data(model.index(row_count, 4)), style = style)
# sheet.write((row_count + 1), 5, model.data(model.index(row_count, 5)), style = style)
# row_count += 1
conn = sqlite3.connect('database.db')
cur = conn.cursor()
cur.execute("SELECT * FROM history")
rows = cur.fetchall()
conn.close()
for row in rows:
sheet.write((row_count + 1), 0, row[0], style = style)
sheet.write((row_count + 1), 1, row[1], style = style)
sheet.write((row_count + 1), 2, row[2], style = style)
sheet.write((row_count + 1), 3, row[3], style = style)
sheet.write((row_count + 1), 4, row[4], style = style)
sheet.write((row_count + 1), 5, row[5], style = style)
row_count += 1
wbk.save(filename)
return
def handle_request_completed(self, response_text):
if response_text == "start":
self.statusLabel.setText("ダウンロード中...")
self.btn_history.setEnabled(False)
self.spinner.start()
elif response_text == "stop":
self.spinner.stop()
self.btn_start.setText("開始")
self.btn_export.setEnabled(True)
self.btn_start.setEnabled(True)
self.isStop = True
elif response_text == "reading":
self.progressBar.setValue(0)
self.statusLabel.setText("ファイルを読んでいます...")
elif response_text != "start" and response_text != 'stop' and response_text != 'reading' and re.findall(r'\d+', response_text) == False:
self.spinner.stop()
self.statusLabel.setText(response_text)
else:
self.spinner.stop()
cur_position = float(response_text) / (100 / self.total_count)
self.statusLabel.setText(f"{self.total_count} 個中 {round(cur_position)} 個処理済み")
self.progressBar.setVisible(True)
self.btn_export.setEnabled(True)
self.progressBar.setValue(round(float(response_text)))
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Amazon - BookOff"))
self.btn_start.setText(_translate("MainWindow", "開始"))