-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostad.py
More file actions
387 lines (338 loc) · 17.6 KB
/
postad.py
File metadata and controls
387 lines (338 loc) · 17.6 KB
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
import os
from random import choices
import wx
import xmltodict
from helper import show_message
from helper import row_builder
from image import Image
from randdesc import RandomDescription
from kijiji_api import KijijiApiException
from httpcore import ReadTimeout
def get_random_photos(path, num=1): # 2 - количество
files = []
for x in os.listdir(path):
if x.endswith(('.jpg', '.jpeg', '.png', '.PNG', '.gif', '.bmp')):
files.append(x)
photo_names = choices(files, k=num)
# photo_path = path + "\\" + photo_name
return photo_names
class PostAdDialog(wx.Dialog):
def __init__(self, kijiji_api, user_id, email, user_token, config, update_spreadsheet):
"""Constructor"""
super().__init__(None, title="Post Ad", size=wx.Size(480, 530))
self.config = config
self.updateSpreadsheet = update_spreadsheet
self.kijiji_api = kijiji_api
self.user_id = user_id
self.email = email
self.user_token = user_token
self.locations = self.kijiji_api.get_locations(self.user_id, self.user_token)
sub_locations = self.get_sub_locations()
# Категории
self.cats = kijiji_api.get_categories(user_id, user_token)
main_cats = []
for i in self.cats['cat:categories']['cat:category']['cat:category']:
main_cats.append(i['cat:id-name'])
main_cats.pop()
main_sizer = wx.BoxSizer(wx.VERTICAL)
btn_sizer = wx.BoxSizer(wx.HORIZONTAL)
font = wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD)
title_lbl = wx.StaticText(self, label="Title :")
title_lbl.SetFont(font)
self.title = wx.TextCtrl(self, value=self.config['DEFAULT_AD']['TITLE'], size=(420, -1))
self.title.SetMaxLength(64)
main_sizer.Add(row_builder([title_lbl, self.title]))
category_lbl = wx.StaticText(self, label="Main Category :")
category_lbl.SetFont(font)
self.main_cats_list = wx.Choice(self, wx.ID_ANY, choices=main_cats)
if self.config['DEFAULT_AD']['MAIN_CATEGORY']:
self.main_cats_list.SetSelection(main_cats.index(self.config['DEFAULT_AD']['MAIN_CATEGORY']))
else:
self.main_cats_list.SetSelection(0)
self.main_cats_list.Bind(wx.EVT_CHOICE, self.update_subcategories)
main_sizer.Add(row_builder([category_lbl, self.main_cats_list]))
subcategories_lbl = wx.StaticText(self, label="Subcategory :")
subcategories_lbl.SetFont(font)
self.subcategories_list = wx.ComboBox(self, wx.ID_ANY, style=wx.CB_READONLY,
choices=self.get_subcategories(self.main_cats_list.GetCurrentSelection()))
if self.config['DEFAULT_AD']['SUBCATEGORY']:
self.subcategories_list.SetSelection(self.get_subcategories(self.main_cats_list.GetCurrentSelection()).
index(self.config['DEFAULT_AD']['SUBCATEGORY']))
else:
self.subcategories_list.SetSelection(0)
self.subcategories_list.Bind(wx.EVT_COMBOBOX, self.update_categories)
main_sizer.Add(row_builder([subcategories_lbl, self.subcategories_list]))
categories_lbl = wx.StaticText(self, label="Category :")
categories_lbl.SetFont(font)
self.categories_list = wx.ComboBox(self, wx.ID_ANY, size=(200, -1), style=wx.CB_READONLY,
choices=self.get_categories(self.main_cats_list.GetCurrentSelection(),
self.subcategories_list.GetCurrentSelection()))
if self.categories_list.GetCount() < 1:
self.categories_list.Disable()
else:
self.categories_list.Enable()
if self.config['DEFAULT_AD']['CATEGORY']:
self.categories_list.SetSelection(self.get_categories(self.main_cats_list.GetCurrentSelection(),
self.subcategories_list.GetCurrentSelection()).index(
self.config['DEFAULT_AD']['CATEGORY']))
else:
self.categories_list.SetSelection(0)
main_sizer.Add(row_builder([categories_lbl, self.categories_list]))
description_lbl = wx.StaticText(self, label="Description :")
description_lbl.SetFont(font)
self.description = wx.TextCtrl(self, value=RandomDescription(self.config['DEFAULT_AD']['DESCRIPTION_FOLDER']) if
self.config['DEFAULT_AD']['DESCRIPTION_FOLDER'] else "", size=(400, 150), style=wx.TE_MULTILINE)
main_sizer.Add(row_builder([description_lbl, self.description]))
photo_folder_lbl = wx.StaticText(self, label="Folder with Images :")
photo_folder_lbl.SetFont(font)
self.photo_folder = wx.DirPickerCtrl(self, id=wx.ID_ANY, path=self.config['DEFAULT_AD']['IMAGES_FOLDER'] if
self.config['DEFAULT_AD']['IMAGES_FOLDER'] else "",
message="Choose pictures directory", style=wx.DIRP_DEFAULT_STYLE,
size=(400, -1))
main_sizer.Add(row_builder([photo_folder_lbl, self.photo_folder]))
locations_lbl = wx.StaticText(self, label="Location :")
locations_lbl.SetFont(font)
self.locations_list = wx.ComboBox(self, wx.ID_ANY, size=(400, -1), style=wx.CB_READONLY,
choices=self.locs_to_strings(sub_locations))
if self.config['DEFAULT_AD']['LOCATION']:
self.locations_list.SetSelection(
self.locs_to_strings(sub_locations).index(self.config['DEFAULT_AD']['LOCATION']))
else:
self.locations_list.SetSelection(0)
main_sizer.Add(row_builder([locations_lbl, self.locations_list]))
fulladdress_lbl = wx.StaticText(self, label="Full Address :")
fulladdress_lbl.SetFont(font)
self.fulladdress = wx.TextCtrl(self, value=self.config['DEFAULT_AD']['FULL_ADDRESS'], size=(400, -1))
main_sizer.Add(row_builder([fulladdress_lbl, self.fulladdress]))
zip_code_lbl = wx.StaticText(self, label="Zip-Code :")
zip_code_lbl.SetFont(font)
self.zip_code = wx.TextCtrl(self, value=self.config['DEFAULT_AD']['POSTAL_CODE'], size=(100, -1))
main_sizer.Add(row_builder([zip_code_lbl, self.zip_code]))
price_lbl = wx.StaticText(self, label="Price :")
price_lbl.SetFont(font)
self.price = wx.TextCtrl(self, value=self.config['DEFAULT_AD']['PRICE'], size=(80, -1))
if self.main_cats_list.GetCurrentSelection() == 4:
self.price.Disable()
main_sizer.Add(row_builder([price_lbl, self.price]))
post_btn = wx.Button(self, label="Post")
post_btn.Bind(wx.EVT_BUTTON, self.post)
btn_sizer.Add(post_btn, 0, wx.ALL, 5)
main_sizer.Add(btn_sizer, 0, wx.CENTER)
self.SetSizer(main_sizer)
def post(self, event):
photos_list = []
if self.photo_folder.GetPath():
try:
photos_name_list = get_random_photos(self.photo_folder.GetPath(), int(
self.config['DEFAULT_AD']['IMAGES_NUM'])) # второй аргумент - количество картинок
except FileNotFoundError as exception:
show_message(str(exception), 'Error')
return
for i in photos_name_list:
photos_list.append(Image(i, self.photo_folder.GetPath()))
zip_code = self.zip_code.GetValue()
try:
location = self.kijiji_api.geo_location(zip_code)
except KijijiApiException as exception:
show_message(str(exception), 'Error')
return
payload = {
'ad:ad': {
'@xmlns:ad': 'http://www.ebayclassifiedsgroup.com/schema/ad/v1',
'@xmlns:cat': 'http://www.ebayclassifiedsgroup.com/schema/category/v1',
'@xmlns:loc': 'http://www.ebayclassifiedsgroup.com/schema/location/v1',
'@xmlns:attr': 'http://www.ebayclassifiedsgroup.com/schema/attribute/v1',
'@xmlns:types': 'http://www.ebayclassifiedsgroup.com/schema/types/v1',
'@xmlns:pic': 'http://www.ebayclassifiedsgroup.com/schema/picture/v1',
'@xmlns:vid': 'http://www.ebayclassifiedsgroup.com/schema/video/v1',
'@xmlns:user': 'http://www.ebayclassifiedsgroup.com/schema/user/v1',
'@xmlns:feature': 'http://www.ebayclassifiedsgroup.com/schema/feature/v1',
'@id': '',
'cat:category': {'@id': self.get_category_id()},
'loc:locations': {'loc:location': {'@id': self.get_location_id()}},
'ad:ad-type': {'ad:value': 'OFFERED'},
'ad:title': self.title.GetValue(),
'ad:description': self.description.GetValue(),
'ad:price': {'types:price-type': {'types:value': 'SPECIFIED_AMOUNT'}},
'ad:account-id': self.user_id,
'ad:email': self.email,
'ad:poster-contact-email': self.email,
# 'ad:poster-contact-name': None, # Not sent by Kijiji app
'ad:phone': None,
'ad:ad-address': {
'types:radius': 400,
'types:latitude': str(location.latitude),
'types:longitude': str(location.longitude),
'types:full-address': self.fulladdress.GetValue(),
# 'Norfolk County Hwy 59, Port Rowan, ON N0E 1M0'
'types:zip-code': zip_code,
},
'ad:visible-on-map': 'true', # appears to make no difference if set to 'true' or 'false'
'attr:attributes': None,
# 'attr:attributes': {'attr:attribute': [
# {'@localized-label': 'For Sale By', '@type': 'ENUM', '@accessibility-feature': 'false',
# '@name': 'forsaleby', 'attr:value': {'@localized-label': 'Owner', '#text': 'ownr'}},
# {'@localized-label': 'Condition', '@type': 'ENUM', '@accessibility-feature': 'false',
# '@name': 'condition',
# 'attr:value': {'@localized-label': 'Used - Like new', '#text': 'usedlikenew'}}]},
'pic:pictures': self.create_picture_payload(photos_list),
'vid:videos': None,
'ad:adSlots': None,
'ad:listing-tags': None,
}
}
# Set price if dollar amount given
payload['ad:ad']['ad:price'].update({
'types:amount': self.price.GetValue(),
'types:currency-iso-code': {'types:value': 'CAD'}, # Assume Canadian dollars
})
xml_payload = xmltodict.unparse(payload, short_empty_elements=True)
# Submit final payload
try:
ad_id = self.kijiji_api.post_ad(self.user_id, self.user_token, xml_payload)
show_message(f"Ad #{str(ad_id)} has been posted!", 'Posted', wx.ICON_INFORMATION)
self.Close()
self.updateSpreadsheet()
# except KijijiApiException as exception:
except Exception as exception:
show_message(str(exception), 'Error')
def get_category_id(self):
if self.categories_list.IsEnabled():
id_name = self.categories_list.GetString(self.categories_list.GetCurrentSelection())
res = None
for i in \
self.cats['cat:categories']['cat:category']['cat:category'][
self.main_cats_list.GetCurrentSelection()][
'cat:category'][self.subcategories_list.GetCurrentSelection()]['cat:category']:
if i['cat:id-name'] == id_name:
res = i
break
return res['@id']
else:
id_name = self.subcategories_list.GetString(self.subcategories_list.GetCurrentSelection())
res = None
for i in \
self.cats['cat:categories']['cat:category']['cat:category'][
self.main_cats_list.GetCurrentSelection()][
'cat:category']:
if i['cat:id-name'] == id_name:
res = i
break
return res['@id']
def get_location_id(self):
id_name = self.locations_list.GetString(self.locations_list.GetCurrentSelection())
locs = id_name.split(',')
count = id_name.count(',')
res = None
# if count >= 2:
for i in self.locations['loc:locations']['loc:location']['loc:location']:
if i['loc:localized-name'] == locs[0]:
for j in i['loc:location']:
if j['loc:localized-name'] == locs[1][1:]:
if count >= 2:
for k in j['loc:location']:
if k['loc:localized-name'] == locs[2][1:]:
res = k
break
else:
res = j
break
return res['@id']
def update_subcategories(self, event):
if self.main_cats_list.GetCurrentSelection() == 4:
self.price.Disable() # Выключаем поле Price, если выбрана категория Services
else:
self.price.Enable()
self.subcategories_list.Clear()
self.subcategories_list.SetItems(self.get_subcategories(self.main_cats_list.GetCurrentSelection()))
self.subcategories_list.SetSelection(0)
self.update_categories(event)
def update_categories(self, event):
self.categories_list.Clear()
self.categories_list.SetItems(self.get_categories(self.main_cats_list.GetCurrentSelection(),
self.subcategories_list.GetCurrentSelection()))
if self.categories_list.GetCount() < 1:
self.categories_list.Disable()
else:
self.categories_list.Enable()
self.categories_list.SetSelection(0)
def get_categories(self, index, index2):
if 'cat:category' in self.cats['cat:categories']['cat:category']['cat:category'][index]['cat:category'][index2]:
categories = []
for i in self.cats['cat:categories']['cat:category']['cat:category'][index]['cat:category'][index2]['cat:category']:
categories.append(i['cat:id-name'])
return categories
else:
return []
def get_subcategories(self, index):
subcategories = []
for i in self.cats['cat:categories']['cat:category']['cat:category'][index]['cat:category']:
subcategories.append(i['cat:id-name'])
return subcategories
def get_main_locations(self) -> list:
locs = []
for i in self.locations['loc:locations']['loc:location']['loc:location']:
locs.append(i['loc:localized-name'])
return locs
@staticmethod
def locs_to_strings(sub_locs) -> list:
list_of_strings = []
for key, value in sub_locs.items():
for i, j in enumerate(value):
if type(j) is dict:
for key2, value2 in j.items():
for b in value2:
# if len(b) > 1:
list_of_strings.append(str(key) + ", " + key2 + ", " + b)
elif type(j) is str:
list_of_strings.append(str(key) + ", " + j)
return list_of_strings
def get_sub_locations(self) -> dict:
states = self.get_main_locations()
locs = {}
for i in states:
locs[i] = []
keys = list(locs)
for i in range(len(keys)):
for index, j in enumerate(
self.locations['loc:locations']['loc:location']['loc:location'][i]['loc:location']):
if type(j) is dict:
if 'loc:location' not in j:
locs[keys[int(i)]].append(j['loc:localized-name'])
else:
sub_locs = {j['loc:localized-name']: []}
for index2, k in enumerate(j['loc:location']):
if type(k) is dict:
sub_locs[j['loc:localized-name']].append(k['loc:localized-name'])
locs[keys[int(i)]].append(sub_locs)
locs['Territories'][0]['Nunavut'] = ['Iqaluit']
locs['Territories'][1]['Northwest Territories'] = ['Yellowknife']
locs['Territories'][2]['Yukon'] = ['Whitehorse']
island_dict = {'Prince Edward Island': ['Charlottetown', 'Summerside']}
locs['Prince Edward Island'].append(island_dict)
return locs
def create_picture_payload(self, data):
"""Build picture payload dict from file* fields."""
payload = {'pic:picture': []}
# Mapping of image size names to size in px
image_sizes = {
'extraLarge': 800,
'large': 500,
'normal': 400,
'thumbnail': 64,
}
for value in data:
try:
link = self.kijiji_api.upload_image(self.user_id, self.user_token, value)
except ReadTimeout as read_timeout:
show_message(str(read_timeout) + " Try again later.", 'Error')
return
# Add a separate link for each image size
links = []
for size_name, size_px in image_sizes.items():
links.append({
'@rel': size_name,
'@href': f'{link}?rule=kijijica-{size_px}-jpg',
})
payload['pic:picture'].append({'pic:link': links})
return payload if len(payload['pic:picture']) else {}