Commit e19eb839 authored by Melroy van den Berg's avatar Melroy van den Berg

latest version

parent 98e18479
File mode changed from 100755 to 100644
......@@ -7,4 +7,5 @@ Reference
* https://developer.mozilla.org/en/docs/Working_with_windows_in_chrome_code
* https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIDOMChromeWindow
* https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIDOMWindowUtils
* https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIFilePicker
......@@ -2,13 +2,20 @@
# Marktplaats batch script
# By: Melroy van den Berg
from marktplaats import Marktplaats
__version__= 1.0
# https://www.marktplaats.nl/syi/201/228/plaatsAdvertentie.html?bucket=38&complete=true&origin=HEADER&l1=201&l2=228&title=Test&attribute[condition]=31&attribute[level]=1950&attribute[orientation]=1955&price.bidding=free-bidding&description=blah&price.value=25,50&acceptPaypal=true&attribute[delivery]=35&shippingDetails.price=650&shippingDetails.provider=postnl&shippingDetails.productCode=3000&shippingDetails.catalogName=parcels&shippingDetails.averageWeight=5000
# Shipping trick only works in Chrome not in Firefox! Arrrg! Try setting hidden fields via javascript instead or other hacking when required.
# True, user need to press the button him/herself. False, the system automatically press the button after uploading pictures.
USER_FINAL_APPROVAL_REQUIRED=True
USER_FINAL_APPROVAL_REQUIRED=False
print ("Running Marktplaats Batch %s" % str(__version__))
if USER_FINAL_APPROVAL_REQUIRED:
print ("INFO: User approval still required before placing the advertisement.\n")
else:
print ("INFO: User approval not required, maximal automatisation.\n")
mp=None
try:
......@@ -21,7 +28,7 @@ try:
mp.load_page_blocking("https://www.marktplaats.nl/account/login.html?target=https%3A%2F%2Fwww.marktplaats.nl%2F", "https://www.marktplaats.nl/account/login.html", "https://www.marktplaats.nl/cookiewall")
print ("Succesful login (hopefully), continue...")
mp.load_form_page_blocking("https://www.marktplaats.nl/syi/201/228/plaatsAdvertentie.html?bucket=38&complete=true&origin=HEADER&l1=201&l2=228&title=Test&attribute[condition]=31&attribute[level]=1950&attribute[orientation]=1955&price.bidding=free-bidding&description=blah&price.value=25,50&acceptPaypal=true&contactInformation.sellerName=Melroy&contactInformation.phoneNumber=06400&contactInformation.postCode=6605DW")
mp.load_form_page_blocking("https://www.marktplaats.nl/syi/201/228/plaatsAdvertentie.html?bucket=38&complete=true&origin=HEADER&l1=201&l2=228&title=Test&attribute[condition]=31&attribute[level]=1950&attribute[orientation]=1955&price.bidding=free-bidding&description=blah&price.value=25,50&acceptPaypal=true&contactInformation.sellerName=Melroy&contactInformation.phoneNumber=0628814764&contactInformation.postCode=6605DW")
except (KeyboardInterrupt, SystemExit):
print ("Exit, bye bye.")
if mp:
......
......@@ -38,9 +38,10 @@ class Marktplaats(MozRepl):
def load_form_page_blocking(self, url, blocking_url="", exception_url=None):
"""
Load the markplaats 'Plaats advertensie' page, busy waiting until submitted
url - page to load
blocking_url - url (beginning of url) that waits until changed differently (blocking until changed to another URL)
Load the markplaats 'Plaats advertensie' page, do some magic tricks here
Input(s): url - page to load
blocking_url - url (beginning of url) that waits until changed differently (blocking until changed to another URL)
"""
# Open url in current tab/window
self.open_url(url)
......@@ -57,45 +58,92 @@ class Marktplaats(MozRepl):
self.change_delivery_method_to_send_or_pickup()
self.change_delivery_method_to_parcel_30kg()
# Trigger file picker
self.open_image_picker()
while True:
# TODO: Detect if user press the OK to upload button or cancel button iso pop-up
self.close_pop_up_frontpage()
sleep(2)
print("Looping here now...")
# When no blocking url is defined use input URL instead
if blocking_url == "":
blocking_url = url
blocking_url = b'"' + blocking_url.encode('utf-8')
# Blocking wait until the current URL is different from the 'blocking url' (form submitted)
self.wait_until_url_changed(blocking_url, exception_url)
# Trigger file picker
# TODO: Check either:
# 1. If a file picker (nsIFilePicker) is currently active in FF, or:
# 2. Connect some onchange event of the click, and write something like document.temp=1 on the onclick event of the file picker. So you know when the user closed this windows
# 3. Try to test FormData? https://stackoverflow.com/questions/7296426/how-can-i-check-if-the-browser-support-html5-file-upload-formdata-object
# 4. Doesn't really work: http://jsfiddle.net/elizeubh2006/1w711q0y/5/ https://stackoverflow.com/questions/4628544/how-to-detect-when-cancel-is-clicked-on-file-input
success_file_picker_open = self.open_image_picker()
if not success_file_picker_open:
# If FF is not focussed, it will also result in a unsuccess open...
print("Allow pop-ups of 'marktplaats.nl', in order to open the file picker automatically (Instellingen->Content-> Pop-up Uitzonderingen of druk op 'Toestaan').\nOr focus Firefox next time to fix this.")
# Busy wait
is_focus_action_triggered=False
while True:
self.close_pop_up_frontpage(success_file_picker_open) # Actively check in a loop for any spam pop-up
if success_file_picker_open:
# You know for sure the pop-up works, so just wait until the document get focus again, meaning the file picker is closed
# Check if the document has focus again (file picker is closed)
has_focus = self.has_document_focus()
if has_focus:
if not is_focus_action_triggered:
is_focus_action_triggered=True
# Document is focused again
if self.approval_req:
# Document is focused again, only scroll-down to the bottom of the page. User interaction still needed.
self.scroll_down_page()
else:
# Just wait a bit longer in order to process the images
sleep(0.5)
# Document is focused again, pop-up must be closed now. Place the ad!
self.place_advertisement()
# Check if URL is changed
(changed, valid) = self.is_url_changed(blocking_url, exception_url)
if changed:
# URL changed! Next!
break
if valid:
# Otherwise, sleep a bit, when data was valid
sleep(1)
sleep(0.5)
def open_image_picker(self):
"""
Trigger Open file picker (multiple files) in Marktplaats
return True if open-up appeared (document has no focus, but the popup has) otherwise False
"""
success=False
sleep(0.5)
has_previous_focus = self.has_document_focus()
self.write_data(b"document.getElementById('file-picker-0').click()")
self.skip_data()
sleep(0.6)
have_currently_focus = self.has_document_focus()
if has_previous_focus:
if not have_currently_focus:
success=True
return success
def close_pop_up_frontpage(self):
def close_pop_up_frontpage(self, success_popup):
"""
Close spam, and directly scroll down
success_popup - True if the pop-up was working correctly (user get file picker automatically)
"""
self.write_data(b"function checkPopUp() { if(document.getElementById('blikvanger-dialog'))return document.getElementById('blikvanger-dialog').style.display;return \"none\"}; checkPopUp()")
(display,valid) = self.read_data()
if display != b'"none"':
self.write_data(b"document.getElementById('blikvanger-dialog').style.display = 'none'; document.getElementById('overlay').style.display = 'none';")
self.skip_data()
# Properly done adding pictures, just go down to the 'Plaats je advertentie' button
if self.approval_req:
self.scroll_down_page()
else:
# TODO: How do I know when a user DID NOT upload any picture at all (on purpose)???!
# Directly press the button
self.place_advertisement()
# Only execute this part during the close pop-up window if the pop-up didn't work
if not success_popup:
# Properly done adding pictures
if self.approval_req:
# Just scroll-down to the bottom of the page
self.scroll_down_page()
else:
# Directly trigger to place the advertisement.
# NOTE: This only works if the user actually did upload a picture (otherwise you don't get this spam pop-up in the first place)
self.place_advertisement()
def place_advertisement(self):
"""
......
......@@ -61,6 +61,7 @@ class MozRepl(object):
if blocking_url == "":
blocking_url = url
blocking_url = b'"' + blocking_url.encode('utf-8')
# Blocking wait until the current URL is different from the 'blocking url'
self.wait_until_url_changed(blocking_url, exception_url)
......@@ -84,36 +85,49 @@ class MozRepl(object):
print ("ERROR: Too many retries. Do you have internet connection at all? Exit.") # Time-out
exit(1)
def wait_until_url_changed(self, blocking_url, exception_url):
"""
Busy wait until the until the current URL is different from the 'blocking url'
blocking_url - Blocking url until this is different
exception_url - Where it should not trigger on (eg. cookie pop-up URL), which is a false positive trigger
Busy wait around is_url_changed(). Only breaks when URL is changed.
"""
print_wait=False
while True:
self.write_data(b"window.location.href")
# Be-sure we run in content always (see valid flag)
(cur_url, valid) = self.read_data(True)
# Only look at cur_url when there is valid data (data running in the current content)
# Check if URL is changed
(changed, valid) = self.is_url_changed(blocking_url, exception_url)
if changed:
# URL changed! Move on.
break
if valid:
if exception_url is not None:
if cur_url.startswith(b'"' + exception_url.encode('utf-8')):
# Ignore the exception_url (if applicable)
valid=False
if valid:
if not cur_url.startswith(blocking_url):
# Page, url is changed, exit wait
break
if not print_wait:
print("Waiting for user input")
print_wait=True
sys.stdout.write('.')
sys.stdout.flush()
# Otherwise, sleep a bit, when data was valid
sleep(3)
def is_url_changed(self, blocking_url, exception_url):
"""
Check if the current URL is changed, and is different from the blocking url.
input(s): print_wait - Boolean which only prints once a message
blocking_url - Blocking url until this is different
exception_url - Where it should not trigger on (eg. cookie pop-up URL), which is a false positive trigger
return (changed, valid) - + Was the check valid (containing valid data)? True was valid.
"""
changed=False
self.write_data(b"window.location.href")
# Be-sure we run in content always (see valid flag)
(cur_url, valid) = self.read_data(True)
# Only look at cur_url when there is valid data (data running in the current content)
if valid:
if exception_url is not None:
if cur_url.startswith(b'"' + exception_url.encode('utf-8')):
# Ignore the exception_url (if applicable)
valid=False
if valid:
if not cur_url.startswith(blocking_url):
# Page/url is changed
changed=True
sys.stdout.write('.')
sys.stdout.flush()
return (changed,valid)
def open_url(self, url):
"""
Trigger a change of URL in current tab
......@@ -129,6 +143,19 @@ class MozRepl(object):
Show pop-up to user in browser
"""
self.write_data(b"alert('" + message.encode('utf-8') + b"')")
self.skip_data()
def has_document_focus(self):
"""
Return if the document has focus or not
return True if it has focus otherwise False
"""
self.write_data(b"document.hasFocus()")
(focus, valid) = self.read_data()
if focus == b"false":
return False
else:
return True
def write_data(self, byte_buffer):
"""
......
File mode changed from 100755 to 100644
File mode changed from 100755 to 100644
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment