This repository has been archived by the owner on Oct 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
247 lines (198 loc) · 8.27 KB
/
main.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
import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QPushButton, QAction, QLineEdit, QMessageBox, QTableWidget, QTableWidgetItem, QHBoxLayout, QVBoxLayout, QTabWidget, QLabel, QComboBox
from PyQt5.QtGui import QIcon, QPixmap, QIntValidator
from PyQt5.QtCore import *
from PyQt5 import QtCore
from Shoe import Shoe
import IScraper
from NewBalanceScraper import NewBalanceScraper
from PumaScraper import PumaScraper
from UnderArmourScraper import UnderArmourScraper
from RunRepeatScraper import RunRepeatScraper
from RunningShoesGuruScraper import RunningShoesGuruScraper
class App(QWidget):
sources = [NewBalanceScraper(), PumaScraper(), UnderArmourScraper()]
ratingSources = [RunRepeatScraper(), RunningShoesGuruScraper()]
def __init__(self):
super().__init__()
self.title = 'SneakerFinder'
size = QtWidgets.QDesktopWidget().screenGeometry(-1)
width = size.width()
height = size.height()
self.left = int(width / 4)
self.top = int(height / 6)
self.width = self.left * 2
self.height = self.top * 4
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.setFixedSize(self.width, self.height)
self.layout = QVBoxLayout(self)
self.layout.setAlignment(QtCore.Qt.AlignCenter)
self.tabs = QTabWidget()
self.tabs.setTabsClosable(True)
self.tabs.tabCloseRequested.connect(self.closeTab)
self.createMainTab()
self.layout.addWidget(self.tabs)
self.show()
def createMainTab(self):
self.tab1 = QWidget()
self.tab1.layout = QHBoxLayout(self)
container = QVBoxLayout(self)
# icon
label = QLabel(self)
pixmap = QPixmap('icon.jpg')
pixmap = pixmap.scaled(int(self.width / 3), int(self.width / 3), QtCore.Qt.KeepAspectRatio)
label.setPixmap(pixmap)
container.addWidget(label, 1)
# application name
nameLabel = QLabel('SneakerFinder', self)
nameLabel.setAlignment(QtCore.Qt.AlignCenter)
nameLabel.setStyleSheet("font: 30pt Comic Sans MS")
container.addWidget(nameLabel)
container.addStretch(1)
# name
nameContainer = QHBoxLayout(self)
nameLabel = QLabel('Name', self)
self.tab1.name = QLineEdit(self)
nameContainer.addWidget(nameLabel)
nameContainer.addWidget(self.tab1.name)
container.addLayout(nameContainer)
# gender
self.tab1.gender = QComboBox(self)
self.tab1.gender.addItems(['', 'Men', 'Women'])
container.addWidget(self.tab1.gender)
# sport
self.tab1.sport = QComboBox(self)
self.tab1.sport.addItems(['', 'Lifestyle', 'Running', 'Training', 'Baseball', 'Basketball', 'Soccer'])
container.addWidget(self.tab1.sport)
# price range
rangeContainer = QHBoxLayout(self)
rangeLabel = QLabel('Price range', self)
onlyInt = QIntValidator(0, 100000)
self.tab1.minPrice = QLineEdit(self)
self.tab1.minPrice.setValidator(onlyInt)
self.tab1.maxPrice = QLineEdit(self)
self.tab1.maxPrice.setValidator(onlyInt)
rangeContainer.addWidget(rangeLabel)
rangeContainer.addWidget(self.tab1.minPrice, 1)
rangeContainer.addWidget(self.tab1.maxPrice, 1)
container.addLayout(rangeContainer)
# search button
button = QPushButton('Search', self)
button.clicked.connect(self.on_click)
container.addWidget(button, 1)
# processing text
self.tab1.loading = QLabel(self)
self.tab1.loading.setAlignment(QtCore.Qt.AlignCenter)
container.addWidget(self.tab1.loading)
container.addStretch(1)
self.tab1.layout.addLayout(QVBoxLayout(self), 1)
self.tab1.layout.addLayout(container, 1)
self.tab1.layout.addLayout(QVBoxLayout(self), 1)
self.tab1.setLayout(self.tab1.layout)
self.tabs.addTab(self.tab1, "SneakerFinder")
@QtCore.pyqtSlot()
def on_click(self):
name = self.tab1.name.text()
sport = self.tab1.sport.currentText()
gender = self.tab1.gender.currentText()
if name == '' and (sport == '' or gender == ''):
self.tab1.loading.setText('Either name is not empty or\nboth sport and gender cannot be empty')
return
self.tab1.loading.setText('Searching...')
self.tab1.loading.repaint()
tabName = (gender + '/' if gender != '' else '') + (sport + '/' if sport != '' else '') + name
tab = self.createTab(tabName)
shoes = []
for source in self.sources:
shoes.extend(source.getShoes(name, gender, sport))
# for shoe in shoes:
# print(shoe)
# shoes = [Shoe('name 1','Men','$1','1','NewBalance'),Shoe('name 2','Women','$2','2','Puma'),Shoe('name 3','Kids','$3','3','UnderArmour')]
shoes = self.filterPrice(shoes)
# ratings
if name != '':
shoes = self.populateRatings(shoes, name)
else:
shoes = [(shoe, 0) for shoe in shoes]
table = self.populateTable(shoes)
tab.layout.addWidget(table)
# hide score column if name is empty
if name == '':
table.setColumnHidden(4, True)
self.tab1.loading.setText('')
def filterPrice(self, shoes):
try:
minPrice = float(self.tab1.minPrice.text())
except ValueError:
minPrice = 0
try:
maxPrice = float(self.tab1.maxPrice.text())
except ValueError:
maxPrice = 100000
return list(filter(lambda shoe: shoe.price >= minPrice and shoe.price <= maxPrice, shoes))
def populateRatings(self, shoes, name):
ratings = []
for source in self.ratingSources:
ratings.extend(source.getShoes(name))
result = []
for shoe in shoes:
added = False
for rating in ratings:
if shoe.name == rating.name and shoe.brand == rating.brand:
result.append((shoe, rating.score))
added = True
if added is False:
result.append((shoe, ''))
return result
def createTab(self, title):
tab = QWidget()
tab.layout = QVBoxLayout(self)
tab.layout.setAlignment(QtCore.Qt.AlignCenter)
tab.setLayout(tab.layout)
self.tabs.addTab(tab, title)
self.tabs.setCurrentIndex(self.tabs.count() - 1)
return tab
def populateTable(self, shoes):
table = self.createTable(len(shoes))
self.fillTable(table, shoes)
table.resizeColumnsToContents()
table.resizeRowsToContents()
return table
def createTable(self, rows):
table = QTableWidget()
table.setRowCount(rows)
table.setColumnCount(5)
table.setHorizontalHeaderItem(0, QTableWidgetItem('Name'));
table.setHorizontalHeaderItem(1, QTableWidgetItem('Price'));
table.setHorizontalHeaderItem(2, QTableWidgetItem('Colors'));
table.setHorizontalHeaderItem(3, QTableWidgetItem('Brand'));
table.setHorizontalHeaderItem(4, QTableWidgetItem('Score'));
return table
def fillTable(self, table, shoes):
for i in range(len(shoes)):
shoe = shoes[i][0]
score = shoes[i][1]
table.setItem(i, 0, QTableWidgetItem(shoe.name))
price = '$%.2f' % shoe.price
table.setItem(i, 1, QTableWidgetItem(price))
if shoe.numberOfColors != 0:
colorItem = QTableWidgetItem(str(shoe.numberOfColors))
colorItem.setTextAlignment(QtCore.Qt.AlignCenter)
table.setItem(i, 2, colorItem)
table.setItem(i, 3, QTableWidgetItem(shoe.brand))
if score != '':
scoreItem = QTableWidgetItem(str(score))
scoreItem.setTextAlignment(QtCore.Qt.AlignCenter);
table.setItem(i, 4, scoreItem)
def closeTab(self, currentIndex):
self.tabs.removeTab(currentIndex)
if self.tabs.count() == 0:
QCoreApplication.quit()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())