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

color terminal text, fancy!

parent c27d842a
......@@ -2,6 +2,7 @@
# Marktplaats batch script
# By: Melroy van den Berg
from marktplaats import Marktplaats
from text_color import TextColor
__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
......@@ -11,11 +12,11 @@ __version__= 1.0
USER_FINAL_APPROVAL_REQUIRED=True
print ("Running Marktplaats Batch %s" % str(__version__))
print (TextColor.OKGREEN + "INFO: Running Marktplaats Batch %s" % str(__version__) + TextColor.ENDC)
if USER_FINAL_APPROVAL_REQUIRED:
print ("INFO: User approval still required before placing the advertisement.\n")
print (TextColor.OKGREEN + "INFO: User approval still required before placing the advertisement.\n" + TextColor.ENDC)
else:
print ("INFO: User approval not required, maximal automatisation.\n")
print (TextColor.OKGREEN + "INFO: User approval not required, maximal automatisation.\n" + TextColor.ENDC)
mp=None
try:
......@@ -26,11 +27,11 @@ try:
# Not yet logged in
# TODO: target can maybe be used in future
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...")
print (TextColor.OKGREEN + "INFO: Succesful login (hopefully), continue..." + TextColor.ENDC)
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.")
print (TextColor.OKGREEN + "Exit, bye bye.")
if mp:
# close connection
mp.close()
......
......@@ -3,6 +3,7 @@ Marktplaats specific methods, an extenstion of the MozRepl class
"""
from mozrepl import MozRepl
from time import sleep
from text_color import TextColor
class Marktplaats(MozRepl):
def __init__(self, approval_req):
......@@ -29,11 +30,11 @@ class Marktplaats(MozRepl):
# 1 is logout url found, 0 is not found
(loggedin, valid) = self.read_data()
if loggedin == b'1':
print ("Already logged in at Marktplaats.")
print (TextColor.OKGREEN + "INFO: Already logged in at Marktplaats." + TextColor.ENDC)
#self.create_popup("U bent reeds ingelogd in Marktplaats.")
return 1
else:
print ("First login into Markplaats!")
print (TextColor.OKGREEN + "INFO: First login into Markplaats!" + TextColor.ENDC)
return 0
def load_form_page_blocking(self, url, blocking_url="", exception_url=None):
......@@ -43,6 +44,11 @@ class Marktplaats(MozRepl):
Input(s): url - page to load
blocking_url - url (beginning of url) that waits until changed differently (blocking until changed to another URL)
"""
# When no blocking url is defined use input URL instead
if blocking_url == "":
blocking_url = url
blocking_url = b'"' + blocking_url.encode('utf-8')
# Open url in current tab/window
self.open_url(url)
......@@ -52,104 +58,93 @@ class Marktplaats(MozRepl):
# run in context of content (enter(content)), use read_data(true) when applicable and check the valid flag
# Wait a bit longer to load the page fully
sleep(0.5)
sleep(0.2)
# Change delivery method
self.change_delivery_method_to_send_or_pickup()
self.change_delivery_method_to_parcel_30kg()
# When no blocking url is defined use input URL instead
if blocking_url == "":
blocking_url = url
blocking_url = b'"' + blocking_url.encode('utf-8')
# Trigger file picker
# TODO: I could ask the user if he/she wants to upload an image in the first place? If not, directly submit the form.
# Otherwise, we can look for the change event of the hidden fields (image.ids, ..)
# If the user want to upload an image can already be put in the Excel sheet! So no additional pop-up required if filled-in.
# 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:
data = {
'title': "Example",
'delivery_method': "parcel_30kg",
'description': "blablabla"
}
# TODO: Read Excel sheet to determ this question, so pop-ups would be required only if undefined
image_required = self.ask_image_upload(data['title'])
if image_required:
print(TextColor.OKBLUE + "NOTE: Please, be-sure you accept pop-ups of 'marktplaats.nl'. Press allow." + TextColor.ENDC)
self.open_image_picker()
# Busy wait
is_focus_action_triggered=False
while True:
# I could also look at the change event of the hidden image id fields
image_uploaded=self.close_pop_up_frontpage()
if image_uploaded:
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.
# 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(2.5)
# Document is focused again, pop-up must be closed now. Place the ad!
# 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)
# 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)
else:
sleep(0.2)
if self.approval_req:
# Only scroll-down to the bottom of the page. User interaction still needed.
self.scroll_down_page()
else:
# Place the ad!
self.place_advertisement()
def ask_image_upload(self, title):
"""
Ask user if an upload is needed.
return true if he/she wants to upload an image. False if not.
"""
needed=True
self.write_data(b"function isUploadNeeded() { return confirm(\"Wilt u foto(s) uploaden voor '" + title.encode('utf-8') + b"'?\") } isUploadNeeded()")
(needed,valid) = self.read_data()
if needed == b"false":
needed=False
return needed
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, success_popup):
def close_pop_up_frontpage(self):
"""
Close spam, and directly scroll down or place advertisement
success_popup - True if the pop-up was working correctly (user get file picker automatically)
"""
image_uploaded=False
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()
# 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)
# Just wait a bit longer in order to process the images
sleep(2.5)
self.place_advertisement()
image_uploaded=True
return image_uploaded
def place_advertisement(self):
"""
......
......@@ -7,8 +7,9 @@ import telnetlib
import socket
import sys
# Internal module
# Internal modules
import process
from text_color import TextColor
NR_RETRIES_DOCUMENT=1000 # 200 sec (3.3 min. time-out)/ 0.2 sec = 1000 tries
NR_RETRIES_FIREFOX=360 # 180 sec (3 min. time-out)/ 0.5 sec = 360 tries
......@@ -24,14 +25,14 @@ class MozRepl(object):
try:
self.t = telnetlib.Telnet("localhost", 4242)
except OSError as e:
print ("Maybe firefox is not running? Trying to start Firefox.")
print (TextColor.ERROR + "WARNING: Maybe firefox is not running? Trying to start Firefox." + TextColor.ENDC)
status = process.startFirefox()
if status == 0:
# Try one time again
try:
self.t = telnetlib.Telnet("localhost", 4242)
except OSError as e:
print ("ERROR: Could not connect to Firefox. Did you install Mozrepl plugin in Firefox? (Install: https://addons.mozilla.org/nl/firefox/addon/mozrepl/).\nIf so, did you also start the service. In the menu: Extra->Mozrepl->Start.")
print (TextColor.ERROR + "ERROR: Could not connect to Firefox. Did you install Mozrepl plugin in Firefox? (Install: https://addons.mozilla.org/nl/firefox/addon/mozrepl/).\nIf so, did you also start the service. In the menu: Extra->Mozrepl->Start." + TextColor.ENDC)
exit(1)
else:
print ("Exit")
......@@ -52,16 +53,16 @@ class MozRepl(object):
# Open url in current tab/window
self.open_url(url)
# Switch to the document active tab (web-page)
self.switch_to_content()
# From this moment on, the write_data/read_data calls COULD be important to
# run in context of content (enter(content)), use read_data(true) when applicable and check the valid flag
# When no blocking url is defined use input URL instead
if blocking_url == "":
blocking_url = url
blocking_url = b'"' + blocking_url.encode('utf-8')
# Switch to the document active tab (web-page)
self.switch_to_content()
# From this moment on, the write_data/read_data calls COULD be important to
# run in context of content (enter(content)), use read_data(true) when applicable and check the valid flag
# Blocking wait until the current URL is different from the 'blocking url'
self.wait_until_url_changed(blocking_url, exception_url)
......@@ -82,7 +83,7 @@ class MozRepl(object):
break
tries+=1
if tries == NR_RETRIES_DOCUMENT:
print ("ERROR: Too many retries. Do you have internet connection at all? Exit.") # Time-out
print (TextColor.ERROR + "ERROR: Too many retries. Do you have internet connection at all? Exit." + TextColor.ENDC) # Time-out
exit(1)
......@@ -164,7 +165,7 @@ class MozRepl(object):
try:
self.t.write(byte_buffer + b'\n')
except socket.error:
print ("Connection problem. Did you disable the plugin or closed Firefox??? Exit")
print (TextColor.ERROR + "ERROR: Connection problem. Did you disable the plugin or closed Firefox? Exit." + TextColor.ENDC)
exit(1)
def read_data(self, inContentImportant=False):
......@@ -183,7 +184,7 @@ class MozRepl(object):
try:
output = self.t.read_until(self.READ_UNTIL_CHARS)[:-i].strip()
except OSError as e:
print ("Connection problem. Did you disable the plugin or closed Firefox? Exit")
print (TextColor.ERROR + "ERROR: Connection problem. Did you disable the plugin or closed Firefox? Exit." + TextColor.ENDC)
exit(1)
# During a context switch (page change) repl will go back to home,
......@@ -220,7 +221,7 @@ class MozRepl(object):
try:
return self.t.read_until(b"> ")
except OSError as e:
print ("Connection problem. Did you disable the plugin or closed Firefox??? Exit")
print (TextColor.ERROR + "ERROR: Connection problem. Did you disable the plugin or closed Firefox? Exit." + TextColor.ENDC)
exit(1)
def skip_data(self):
......@@ -231,7 +232,7 @@ class MozRepl(object):
try:
self.t.read_until(self.READ_UNTIL_CHARS)
except OSError as e:
print ("Connection problem. Did you disable the plugin or closed Firefox??? Exit")
print (TextColor.ERROR + "ERROR: Connection problem. Did you disable the plugin or closed Firefox? Exit." + TextColor.ENDC)
exit(1)
def close(self):
......
......@@ -6,6 +6,7 @@ import asyncio
import sys
from time import sleep
from asyncio.subprocess import PIPE, STDOUT
from text_color import TextColor
TIMEOUT_TO_ACTIVATE_PLUGIN=25 #secs
......@@ -33,7 +34,7 @@ async def run_command(*args, timeout=None):
if mozrepl_found:
pass
else:
print("Mozrepl was not detected in time (time-out)!\nDid you install and enable Mozrepl plugin in Firefox? (Install: https://addons.mozilla.org/nl/firefox/addon/mozrepl/).\n\nTry again.")
print(TextColor.ERROR + "ERROR: Plugin was not detected in time (time-out)!\nDid you install and enable Mozrepl plugin in Firefox? (Install: https://addons.mozilla.org/nl/firefox/addon/mozrepl/).\n\nTry again." + TextColor.ENDC)
try:
process.kill()
await process.wait()
......@@ -47,11 +48,11 @@ async def run_command(*args, timeout=None):
try:
process.kill()
await process.wait()
print("You closed Firefox. That was not the idea...")
print(TextColor.WARNING + "WARNING: You closed Firefox. That was not the idea...")
return_code=1
break
except ProcessLookupError:
print("Firefox process has already started earlier, be sure you enable Mozrepl. Waiting " + str(TIMEOUT_TO_ACTIVATE_PLUGIN) + " seconds before I try to connect to mozrepl...")
print(TextColor.WARNING + "WARNING: Firefox process has already started earlier, be sure you enable Mozrepl. Waiting " + str(TIMEOUT_TO_ACTIVATE_PLUGIN) + " seconds before I try to connect to mozrepl..." + TextColor.ENDC)
sleep(TIMEOUT_TO_ACTIVATE_PLUGIN)
break
elif process_line(line):
......
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