-
Notifications
You must be signed in to change notification settings - Fork 3
/
uploader.py
executable file
·648 lines (515 loc) · 21.4 KB
/
uploader.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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
#!/usr/bin/env python3
import argparse
import os
import sys
import time
import traceback
import undetected_chromedriver as uc
import webview
from natsort import natsorted
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.wait import WebDriverWait
delay = 0.1
timeout = 10
open_tabs = []
marktplaats_upload_url = "https://www.marktplaats.nl/plaats"
tweakers_login_url = "https://tweakers.net/my.tnet/login/"
tweakers_upload_url = "https://tweakers.net/aanbod/nieuw/aanbod/"
def printt(*argss, **kwargs):
if args.verbose:
to_print = " ".join(map(str, argss))
print(to_print, **kwargs)
def warn(*argss, **kwargs):
to_print = " ".join(map(str, argss))
to_print = "WARNING: " + to_print
print(to_print, **kwargs)
# SELENIUM UTIL
########################################################################################################################
def select_dropdown(xpath, option_text):
try:
dropdown = driver.find_element(By.XPATH, xpath)
center_element(dropdown)
time.sleep(delay)
select = Select(dropdown)
select.select_by_visible_text(option_text)
time.sleep(delay)
except Exception:
warn(f"Could not select option '{option_text}'")
def sleep_until_url_change():
try:
url = driver.current_url
while True:
if url != driver.current_url:
break
time.sleep(1)
except:
traceback.print_exc()
warn(f"URL not changed but breaking because of above exception")
# only support for 2 tabs: 1 and 2
def switch_to_tab(tab_number):
amount_of_tabs = len(driver.window_handles)
printt(f"Switching to tab {tab_number}/{amount_of_tabs}")
# opening tab whilst another page loads introduces a big delay
WebDriverWait(driver, timeout).until(lambda x: driver.execute_script("return document.readyState") == "complete")
# save the original tab reference
if len(open_tabs) == 0:
tab1_handle = driver.current_window_handle
open_tabs.append(tab1_handle)
# create the second tab if it does not exist
if tab_number > 1 and amount_of_tabs == 1:
print(f"Creating new tab")
driver.switch_to.new_window('tab')
WebDriverWait(driver, timeout).until(EC.number_of_windows_to_be(2))
for window_handle in driver.window_handles:
if window_handle != open_tabs[0]:
driver.switch_to.window(window_handle)
open_tabs.append(window_handle) # tab2_handle
# switch tab
if tab_number == 1:
driver.switch_to.window(open_tabs[0])
else:
driver.switch_to.window(open_tabs[1])
def tab_and_get(url: str, tab_number: int):
switch_to_tab(tab_number)
driver.get(url)
def remove_hidden_attribute(xpath):
printt(f"Removing hidden tag on element with xpath={xpath}")
try:
element = driver.find_element(By.XPATH, xpath)
driver.execute_script(
"arguments[0].removeAttribute('hidden')", element)
except Exception:
warn(f"Could not remove hidden tag.")
def center_element(element):
printt(f"Centering element: {element}")
try:
driver.execute_script(
"arguments[0].scrollIntoView({'block':'center','inline':'center'})", element)
except Exception:
warn(f"Failed to center element {element}")
def enter_field(xpath, text):
printt(f"Entering text in field: '{text}'")
try:
element = WebDriverWait(driver, timeout).until(
EC.presence_of_element_located((By.XPATH, xpath)))
if element is None:
quit(f"Could not find element with {xpath} to enter {text}")
center_element(element)
time.sleep(delay)
element.send_keys(text)
time.sleep(delay)
except Exception:
warn(f"Failed to enter '{text}'")
def click_button(xpath):
printt(f"Clicking button: {xpath}")
try:
button = WebDriverWait(driver, timeout).until(
EC.presence_of_element_located((By.XPATH, xpath)))
center_element(button)
time.sleep(delay)
button.click()
time.sleep(delay)
except Exception:
warn(f"Could not click button.")
########################################################################################################################
# SELENIUM UTIL
# MARKTPLAATS
########################################################################################################################
def login_marktplaats(username, password):
printt(f"Logging in to Marktplaats with {username}, {password}")
cookie_button_xpath = '//*[@id="gdpr-consent-banner-accept-button"]'
email_field_xpath = '//*[@id="email"]'
password_field_xpath = '//*[@id="password"]'
submit_button_xpath = '//*[@id="account-login-button"]'
tab_and_get(marktplaats_upload_url, 1)
click_button(cookie_button_xpath)
enter_field(email_field_xpath, username)
enter_field(password_field_xpath, password)
click_button(submit_button_xpath)
time.sleep(2)
printt(f"Marktplaats logged in")
def marktplaats_enter_title(title):
title_field_xpath = '//*[@id="category-keywords"]'
enter_field(title_field_xpath, title)
find_category_button_xpath = '//*[@id="find-category"]'
click_button(find_category_button_xpath)
time.sleep(delay)
submit_title_button_xpath = '//*[@id="category-selection-submit"]'
click_button(submit_title_button_xpath)
def marktplaats_enter_description(description_text):
description_iframe_xpath = '//*[@id="description_nl-NL_ifr"]'
description_field_xpath = '//*[@id="tinymce"]'
# wait for the description element to load, as it is within an iframe
WebDriverWait(driver, timeout).until(
EC.frame_to_be_available_and_switch_to_it((By.XPATH, description_iframe_xpath)))
# wait for text field to load
description_field = WebDriverWait(driver, timeout).until(
EC.element_to_be_clickable((By.XPATH, description_field_xpath)))
# scroll to element (for user to see)
center_element(description_field)
time.sleep(delay)
# enter description
description_field.send_keys(description_text)
# switch focus back from the iframe to the main frame
driver.switch_to.default_content()
time.sleep(delay)
def marktplaats_upload_photos(image_paths):
upload_xpath = "//input[@tabindex = '-1']"
count = 0
for file_path in image_paths:
count += 1
printt(f"Uploading photo {count}/{len(image_paths)}: {file_path}")
upload_elements = driver.find_elements(By.XPATH, upload_xpath)
last_upload_element = upload_elements[-1]
center_element(last_upload_element)
last_upload_element.send_keys(file_path)
# wait for next upload box to appear
count = 0
while True:
if len(driver.find_elements(By.XPATH, upload_xpath)) > len(upload_elements):
break
time.sleep(0.1)
count += 1
if count > 100:
break
def upload_marktplaats(ad):
printt(f"Placing advertisement on Marktplaats: {ad['title']}")
tab_and_get(marktplaats_upload_url, 1)
# title
marktplaats_enter_title(ad['title'])
# photos
marktplaats_upload_photos(ad['image_paths'])
# description
marktplaats_enter_description(ad['description'])
# state
state_xpath = '//*[@id="syi-attribute-condition"]/div/select'
select_dropdown(state_xpath, "Gebruikt")
# price
asking_price_xpath = '//*[@id="syi-bidding-price"]/input'
enter_field(asking_price_xpath, ad['price'])
# allow bidding
bidding_toggle_xpath = '//*[@id="syi-bidding-switch"]'
click_button(bidding_toggle_xpath)
# min price
# min_price_xpath = '//*[@id="syi-bidding-minimumprice"]/input'
# enter_field(min_price_xpath, ad['min_price'])
# zip code
zip_code_xpath = '//*[@id="postCode"]/input'
enter_field(zip_code_xpath, ad['zip_code'])
# delivery method
delivery_method_xpath = '//*[@id="deliveryMethod"]/div/select'
delivery_method_alt_xpath = '//*[@id="Verzenden"]' # soms veranderd de pagina
select_dropdown(delivery_method_xpath, "Verzenden")
click_button(delivery_method_alt_xpath)
# advertisement plan
plan_xpath = '//*[contains(text(), "Standaard zichtbaarheid")]'
click_button(plan_xpath)
print(f"Verify the details and click upload to place the advertisement to Marktplaats.")
sleep_until_url_change()
print(f"Uploaded to Marktplaats!")
time.sleep(delay)
########################################################################################################################
# MARKTPLAATS
# TWEAKERS
########################################################################################################################
def login_tweakers(username, password):
printt(f"Logging in to Tweakers with {username}, {password}")
tab_and_get(tweakers_login_url, 2)
username_xpath = '//*[@id="tweakers_login_form_user"]'
password_xpath = '//*[@id="tweakers_login_form_password"]'
enter_field(username_xpath, username)
enter_field(password_xpath, password)
time.sleep(delay)
password_field = driver.find_element(By.XPATH, password_xpath)
password_field.send_keys(Keys.ENTER)
time.sleep(2)
printt(f"Tweakers logged in")
def tweakers_upload_photos(image_paths):
upload_xpath = '//*[@id="row_advertisement_form_images"]/div[2]/twk-multi-image-upload/div[1]/input[1]'
count = 0
# this is fast but will take a while to show up
for file_path in image_paths:
count += 1
printt(f"Uploading photo {count}/{len(image_paths)}: {file_path}")
enter_field(upload_xpath, file_path)
def upload_tweakers(ad):
printt(f"Placing advertisement on Tweakers: {ad['title']}")
tab_and_get(tweakers_upload_url, 2)
# category (user should configure this himself, too many options)
category_xpath = '//*[@id="advertisement_form_relatedTweakbase_searchProduct"]'
enter_field(category_xpath, ad['title'])
# title
title_xpath = '//*[@id="advertisement_form_title"]'
enter_field(title_xpath, ad['title'])
# description
description_xpath = '//*[@id="advertisement_form_description"]'
enter_field(description_xpath, ad['description'])
# price
price_xpath = '//*[@id="advertisement_form_price_price"]'
enter_field(price_xpath, ad['price'])
# state
state_xpath = '//*[@id="advertisement_form_productState"]'
select_dropdown(state_xpath, "gebruikssporen")
# photos
tweakers_upload_photos(ad["image_paths"])
# zip code # already entered if one ad was uploaded ever
# zip_code_xpath = '//*[@id="advertisement_form_address_postalCode"]'
# enter_field(zip_code_xpath, ad['zip_code'])
# delivery cost
delivery_costs_xpath = '//*[@id="advertisement_form_deliveryCosts"]'
select_dropdown(delivery_costs_xpath, "Niet van toepassing")
# delivery method
# delivery_methods_xpath = '//*[@id="advertisement_form_deliveryMethods_1"]'
# click_button(delivery_methods_xpath) # checkbox
# payment method
# payment_method_xpath = '//*[@id="advertisement_form_paymentOptions_7"]'
# click_button(payment_method_xpath) # checkbox
# allow reactions
# allow_reactions_xpath = '//*[@id="advertisement_form_allowResponses"]'
# click_button(allow_reactions_xpath) # checkbox
print(f"Verify the details and click upload to place the advertisement to Tweakers.")
sleep_until_url_change()
print(f"Uploaded to Tweakers!")
########################################################################################################################
# TWEAKERS
# UTIL
########################################################################################################################
def ask_folders():
# returns None if no folder is choosen, otherwise a list of paths
dir_paths = None
def open_file_dialog(w):
nonlocal dir_paths
try:
dir_paths = w.create_file_dialog(dialog_type=webview.FOLDER_DIALOG,
allow_multiple=True,
directory=os.getcwd()
)
except TypeError:
pass # user exited file dialog without picking
finally:
w.destroy()
printt(f"Launching folder picker")
window = webview.create_window("", hidden=True)
webview.start(open_file_dialog, window)
printt(f"You picked these folders: {dir_paths}")
# dir_paths = ['/home/jort/Pictures/project_sell/chromecast_4k'] # TODO remove after testing
return dir_paths
def get_folder_paths():
if not args.folder_paths or len(args.folder_paths) == 0:
printt("No folders supplied as argument, launching folder picker")
folder_paths = ask_folders()
if not folder_paths:
quit("You picked an invalid folder")
else:
folder_paths = args.folder_paths
for folder_path in folder_paths:
if not folder_path or not os.path.isdir(folder_path):
quit("You picked an invalid folder")
# sanitize trailing slashes
folder_paths = [os.path.normpath(folder_path) for folder_path in folder_paths]
if len(folder_paths) == 0:
quit(f"No folders found at this depth level. Try lowering it.")
printt(f"Uploading items in folders: {folder_paths}")
return folder_paths
def read_credential(argument=None, credential_path=None):
if argument is not None and argument != "":
credential = argument
printt(f"Read credential '{credential}' from arguments")
return credential
if credential_path is None:
quit(f"No credentials supplied.")
if not os.path.exists(credential_path):
credential_path = os.path.join(sys.path[0], credential_path)
if not os.path.exists(credential_path):
quit(f"Cannot find credential file on {credential_path}")
with open(credential_path) as file:
file_content = file.read()
credential = file_content.strip()
if not credential or credential == "":
quit(f"Empty credential file: {credential_path}")
printt(f"Read credential '{credential}' from {credential_path}")
return credential
def get_driver():
# instantiate chrome
options = uc.ChromeOptions()
options.add_experimental_option(
"prefs",
{
"credentials_enable_service": False,
"profile.password_manager_enabled": False,
},
)
options.add_argument("--disable-notifications") # not needed
options.add_argument("--disable-popup-blocking") # disable are you sure to leave this page? does not work
options.set_capability('unhandledPromptBehavior', 'accept') # default is dismiss, which confuses all the code
driver = uc.Chrome(options=options)
return driver
########################################################################################################################
# UTIL
# AD PARSER
########################################################################################################################
def assemble_advertisement_info(dir_path):
ad = {}
# find .txt file
txt_files = [os.path.join(dir_path, x) for x in os.listdir(dir_path) if x.endswith(".txt")]
if len(txt_files) != 1:
quit(f"Expected 1 description .txt file but found {len(txt_files)}")
# read .txt file
with open(txt_files[0]) as file:
file_lines = file.readlines()
# title
title = file_lines[0].strip()
ad["title"] = title
# price
asking_price = file_lines[1].strip()
if args.default_price != "":
asking_price = args.default_price
ad["price"] = asking_price
# min price
min_price = args.minimum_price
if min_price == "":
min_price = "0"
ad["min_price"] = min_price
# description text
description_text = ""
for line in file_lines[2:]:
description_text += line
description_text.strip()
default_description = args.default_description.replace("\\n", Keys.ENTER) # append default description footer
description_text += default_description
ad["description"] = description_text
# zip code
ad["zip_code"] = zip_code # from args / file
# folder path
ad["dir_path"] = dir_path
# image paths
image_paths = []
for filename in os.listdir(dir_path):
file_path = os.path.join(dir_path, filename)
if not os.path.isfile(file_path):
continue
if any(substring in file_path.lower() for substring in (".jpg", ".png", ".jpeg")):
image_paths.append(file_path)
image_paths = natsorted(image_paths, key=lambda y: y.lower()) # sort items same as the file manager does
ad["image_paths"] = image_paths
printt(f"Parsed advertisement: {ad}")
return ad
########################################################################################################################
# AD PARSER
# CLI
########################################################################################################################
parser = argparse.ArgumentParser(
prog="Advertisement uploader",
description="Upload your advertisement to multiple sites.",
epilog="Created by Jort: github.com/jort-dev")
parser.add_argument("-v", "--verbose",
help="prints more messages about the process",
action="store_true",
)
parser.add_argument("-p", "--default-price",
help="the price enter for the advertisements, ignoring the one defined in the description file",
type=str,
default=""
)
parser.add_argument("-d", "--default-description",
help="the text to append after the description of the advertisements",
type=str,
default=""
)
parser.add_argument("-m", "--minimum-price",
help="the minimum price to enter for the advertisements",
type=str,
default=""
)
parser.add_argument("-mu", "--marktplaats-username",
help="your Marktplaats login",
type=str,
default=""
)
parser.add_argument("-mp", "--marktplaats-password",
help="your Marktplaats password",
type=str,
default=""
)
parser.add_argument("-tu", "--tweakers-username",
help="your Tweakers username",
type=str,
default=""
)
parser.add_argument("-tp", "--tweakers-password",
help="your Tweakers password",
type=str,
default=""
)
parser.add_argument("-zc", "--zip_code",
help="your zip code, for example 1234AB",
type=str,
default=""
)
parser.add_argument("folder_paths",
nargs=argparse.REMAINDER,
help="the paths to the advertisement folder(s),"
" where each folder has a description.txt file and photos for the advertisement",
)
args = parser.parse_args()
printt(args)
marktplaats_username = read_credential(
credential_path="credentials/marktplaats_username.txt",
argument=args.marktplaats_username
)
marktplaats_password = read_credential(
credential_path="credentials/marktplaats_password.txt",
argument=args.marktplaats_password
)
tweakers_username = read_credential(
credential_path="credentials/tweakers_username.txt",
argument=args.tweakers_username
)
tweakers_password = read_credential(
credential_path="credentials/tweakers_password.txt",
argument=args.tweakers_password
)
zip_code = read_credential(
credential_path="credentials/zip_code.txt",
argument=args.zip_code
)
########################################################################################################################
# CLI
# PROGRAM :')
########################################################################################################################
def upload_ads():
folder_paths = get_folder_paths()
login_marktplaats(marktplaats_username, marktplaats_password)
login_tweakers(tweakers_username, tweakers_password)
for folder_path in folder_paths:
advertisement_info = assemble_advertisement_info(folder_path)
upload_marktplaats(advertisement_info)
upload_tweakers(advertisement_info)
########################################################################################################################
# PROGRAM :')
# PROGRAM WRAPPER
########################################################################################################################
driver = get_driver()
try:
try:
upload_ads()
except Exception as e:
traceback.print_exc()
print(f"Program finished.")
while True:
cmd = input("Enter: ")
try:
eval(cmd)
except Exception as e:
print(f"Exception: {e}")
traceback.print_exc()
count = 0
except KeyboardInterrupt:
print(f"Program stopped manually.")
driver.quit()
quit(0)
########################################################################################################################
# PROGRAM WRAPPER