-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAllegroQuery.py
216 lines (185 loc) · 8.43 KB
/
AllegroQuery.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
#kod do wszukiwnia itemów w Pythonie 2.7, wymaga instalacji modulu suds
from suds.client import Client
from suds.plugin import MessagePlugin
#from abstract_interface import DataSource
from record import Record
import time
class AllegroQuery(Record):
def __init__(self, filterContainer, threshold):
DataSource.__init__(self, filterContainer, threshold)
self.webAPI = ''
self.userLogin = ''
self.userPassword = ''
self.countryId = 1 #POLAND
self.createConnection()
self.filterContainer = filterContainer
self.threshold = threshold
def createFilter(self, tablica, minPrice=None, maxPrice=None):
rangeUp = len(tablica)
filter_query = self.client.factory.create('ArrayOfFilteroptionstype')
for i in range(0, rangeUp):
filterr = self.client.factory.create('FilterOptionsType')
filterr.filterId = list(tablica.keys())[i]
#print(filtr.filterId)
filterAOS = self.client.factory.create('ArrayOfString')
filterAOS.item = list(tablica.values())[i]
filterr.filterValueId = filterAOS
filter_query.item.append(filterr)
price_filter = self.client.factory.create('FilterOptionsType')
if minPrice != None and maxPrice != None:
price_filter.filterId = "price"
price_filter.filterValueRange.rangeValueMin = minPrice
price_filter.filterValueRange.rangeValueMax = maxPrice
else:
if minPrice != None:
price_filter.filterId = "price"
price_filter.filterValueRange.rangeValueMin = minPrice
if maxPrice != None:
price_filter.filterId = "price"
price_filter.filterValueRange.rangeValueMax = maxPrice
filter_query.item.append(price_filter)
return filter_query
def createSort(self, tablica):
if(tablica != None):
sort_query = self.client.factory.create('SortOptionsType')
sort_query.sortType = list(tablica.keys())[0]
sort_query.sortOrder = list(tablica.values())[0]
return sort_query
def createConnection(self):
try:
url = 'https://webapi.allegro.pl/service.php?wsdl'
self.client = Client(url, location='https://webapi.allegro.pl/service.php', timeout=60000)
except:
print("No internet connection!")
def sendQuery(self, query):
try:
filter_query = self.createFilter(query.filterOptions, query.minPrice, query.maxPrice)
sort_query = self.createSort(query.sortOptions)
result = self.client.service.doGetItemsList(webapiKey=self.webAPI, filterOptions=filter_query, sortOptions=sort_query,
countryId=self.countryId, resultSize=query.numberOfItems)
itemId = []
itemName = []
offertId = []
userId = []
quantity = []
boughtItem = []
price = []
companyAddress = []
categoryName = []
usedOrNew = []
vatInvoice = []
vatInvoiceMargin = []
companyNip = []
auctionDescription = []
userLogin = []
resultSys = self.client.service.doQuerySysStatus(sysvar=3, countryId=self.countryId,
webapiKey = self.webAPI)
verKey = resultSys.verKey
resultLogin = self.client.service.doLogin(userLogin=self.userLogin,
userPassword=self.userPassword, countryCode=self.countryId, webapiKey=self.webAPI,
localVersion=verKey)
ssessionHandle = resultLogin.sessionHandlePart
for item in result.itemsList[0]:
itemId.append(item.itemId)
itemName.append(item.itemTitle)
userId.append(item.sellerInfo.userId)
userLogin.append(item.sellerInfo.userLogin)
quantity.append(item.leftCount + item.bidsCount)
boughtItem.append(item.bidsCount)
price.append(item.priceInfo[0][0].priceValue)
usedOrNew.append(item.conditionInfo)
for cat in result.categoriesList.categoriesTree[0]:
if cat.categoryId == item.categoryId:
categoryName.append(cat.categoryName)
break
else:
categoryName.append("NO CAT")
a = self.client.factory.create('ArrayOfLong')
a.item = [item.itemId]
resultItem = self.client.service.doGetItemsInfo(sessionHandle=ssessionHandle,
getDesc = 1, getCompanyInfo = 1, itemsIdArray = a)
itemInfo = resultItem[0][0][0]
offertId.append(itemInfo[0].itId)
auctionDescription.append(itemInfo[0].itDescription)
vatInvoice.append(itemInfo[0].itVatInvoice)
vatInvoiceMargin.append(itemInfo[0].itVatMarginInvoice)
address = ""
if itemInfo.itemCompanyInfo.companyFirstName != None:
address += itemInfo.itemCompanyInfo.companyFirstName + " "
if itemInfo.itemCompanyInfo.companyLastName != None:
address += itemInfo.itemCompanyInfo.companyLastName + " "
if itemInfo.itemCompanyInfo.companyName != None:
address += itemInfo.itemCompanyInfo.companyName + "\n"
if itemInfo.itemCompanyInfo.companyAddress != None:
address += itemInfo.itemCompanyInfo.companyAddress + "\n"
if itemInfo.itemCompanyInfo.companyPostcode != None:
address += itemInfo.itemCompanyInfo.companyPostcode + " "
if itemInfo.itemCompanyInfo.companyCity != None:
address += itemInfo.itemCompanyInfo.companyCity + " "
companyAddress.append(address)
companyNip.append(itemInfo.itemCompanyInfo.companyNip)
kwargs = {
"userId": userId,
"itemName" : itemName,
"itemId": itemId,
"boughtItem": boughtItem,
"quantity": quantity,
"companyAddress": companyAddress,
"price": price,
"categoryName": categoryName,
"companyAddress": companyAddress,
"auctionDescription": auctionDescription,
"usedOrNew": usedOrNew,
"vatInvoice": vatInvoice,
"vatInvoiceMargin": vatInvoiceMargin,
"offertId": offertId,
"companyNip": companyNip,
"userLogin": userLogin,
"length": len(userId)
}
return Record(**kwargs)
except Exception as e:
print(e)
kwargs = {
"userId": [],
"itemName" : [],
"itemId": [],
"boughtItem": [],
"quantity": [],
"companyAddress": [],
"price": [],
"categoryName": [],
"companyAddress": [],
"auctionDescription": [],
"usedOrNew": [],
"vatInvoice": [],
"vatInvoiceMargin": [],
"offertId": [],
"userLogin": [],
"companyNip": [],
"length": len(userId)
}
return Record(**kwargs)
def sendItemQuery(self, query):
try:
filter_query = self.createFilter(query.filterOptions)
result = self.client.service.doGetItemsInfo(webapiKey = self.webAPI, filterOptions = filter_querry,
countryId = self.countryId, resultSize=query.numberOfItems)
except Exception as e:
print(e)
class Query:
def __init__(self, filterOptionsDict, **kwargs):
self.filterOptions = filterOptionsDict
for k, v in kwargs.items():
setattr(self, k, v)
if __name__ == "__main__":
filterOptions = {
'search': 'samsung galaxy',
'offerType': 'buyNow',
'offerOptions': 'vatInvoice',
'description': 'true'
}
sort = {'price' : "asc"}
q = AllegroQuery.Query(filterOptions, sort, 5, 1000, 1000000)
allQuery = AllegroQuery(None, 0.5)
allQuery.sendQuery(q)