Commit 258e58ae authored by Melroy van den Berg's avatar Melroy van den Berg

Convert to python 3.x

parent 3347ef0f
#!/usr/bin/env python
#!/usr/bin/env python3
# Marktplaats batch script
# By: Melroy van den Berg
......@@ -8,20 +8,21 @@ from mozrepl import Mozrepl
# Shipping trick only works in Chrome not in Firefox! Arrrg! Try setting hidden fields via javascript instead or other hacking when required.
# Shipping data: https://api.marktplaats.nl/v1/postnl/catalog
try:
m = Mozrepl()
# Is user loggedin?
login = m.check_marktplaats_login()
if login == 0:
# Not yet logged in
# TODO: target can maybe be used in future
m.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")
print "Succesful login (hopefully), continue..."
m=None
#try:
m = Mozrepl()
# Is user loggedin?
login = m.check_marktplaats_login()
if login == 0:
# Not yet logged in
# TODO: target can maybe be used in future
m.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")
print ("Succesful login (hopefully), continue...")
m.load_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&attribute[delivery]=35&shippingDetails.price=650&shippingDetails.provider=postnl&shippingDetails.productCode=3000&shippingDetails.catalogName=parcels&shippingDetails.averageWeight=5000")
except (KeyboardInterrupt, SystemExit):
print "Exit, bye bye."
if m:
# close connection
m.close()
m.load_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&attribute[delivery]=35&shippingDetails.price=650&shippingDetails.provider=postnl&shippingDetails.productCode=3000&shippingDetails.catalogName=parcels&shippingDetails.averageWeight=5000")
#except (KeyboardInterrupt, SystemExit):
# print ("Exit, bye bye.")
# if m:
# # close connection
# m.close()
......@@ -2,8 +2,28 @@
from time import sleep
import telnetlib
import socket
from subprocess import Popen, PIPE
NR_RETRIES=800 # 240 sec (4 min. time-out)/ 0.3 sec = 800 tries
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
def startFirefox():
"""
Start Ffirefox
"""
proc = Popen('firefox https://www.marktplaats.nl', shell=True, stdout=PIPE, bufsize=1)
with proc.stdout:
tries=0
for line in iter(proc.stdout.readline, b''):
if b'MOZREPL' in line:
break
elif tries == NR_RETRIES_FIREFOX:
print ("Firefox is not started (Extra->Mozrepl->Start) or MOZREPL plugin is not activated or not installed (Install: https://addons.mozilla.org/nl/firefox/addon/mozrepl/). Exit.")
exit(1)
else:
tries+=1
sleep(0.5)
print ("Waiting to start Firefox and Mozrepl plugin (Extra->Mozrepl->Start)...")
class Mozrepl:
def __init__(self):
......@@ -11,19 +31,26 @@ class Mozrepl:
Contructor, only create a telnet connection
"""
# Default socket name (repl)
self.SOCKET_NAME = "repl"
self.SOCKET_NAME = b'repl'
# $ telnet localhost 4242
try:
self.t = telnetlib.Telnet("localhost", 4242)
except socket.error:
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."
exit(1)
print ("Maybe firefox is not running? Trying to start Firefox.")
startFirefox()
# Try one time again
try:
self.t = telnetlib.Telnet("localhost", 4242)
except socket.error:
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.")
exit(1)
conn_log = self.readAllData()
split_log = conn_log.splitlines()
sockname = split_log[-1].split(">")[0].strip()
sockname = split_log[-1].split(b'>')[0].strip()
if self.SOCKET_NAME != sockname:
self.SOCKET_NAME = sockname
self.READ_UNTIL_CHARS=self.SOCKET_NAME+">"
self.READ_UNTIL_CHARS=self.SOCKET_NAME+b'>'
def check_marktplaats_login(self):
"""
......@@ -41,16 +68,16 @@ class Mozrepl:
# run in context of content (enter(content)), use readData(true) when applicable and check the valid flag
# Check logout url availability
self.writeData("function checkLogin(){for(var n=0,t=document.links.length;n<t;n++)if(document.links[n].href.startsWith(\"https://auth.marktplaats.nl/accounts/authentication/logout\"))return 1;return null}")
self.writeData(b"function checkLogin(){for(var n=0,t=document.links.length;n<t;n++)if(document.links[n].href.startsWith(\"https://auth.marktplaats.nl/accounts/authentication/logout\"))return 1;return null}")
self.skipData()
self.writeData("checkLogin()")
self.writeData(b"checkLogin()")
(loggedin, valid) = self.readData()
if loggedin == "1":
print "Already logged-in."
self.writeData("alert('U bent reeds ingelogd in Marktplaats.')")
print ("Already logged-in.")
self.writeData(b"alert('U bent reeds ingelogd in Marktplaats.')")
return 1
else:
print "First login into Markplaats!"
print ("First login into Markplaats!")
return 0
def load_page_blocking(self, url, blocking_url=""):
......@@ -82,17 +109,18 @@ class Mozrepl:
if blocking_url == "":
# When no blocking url is defined use input URL instead
blocking_url = url
blocking_url = b'"' + blocking_url.encode('utf-8')
while True:
self.writeData("window.location.href")
self.writeData(b"window.location.href")
# Be-sure we run in content always (see valid flag)
(cur_url, valid) = self.readData(True)
# Only look at cur_url when there is valid data
if valid:
if not cur_url.startswith('"' + blocking_url):
if not cur_url.startswith(blocking_url):
# Page, url is changed (page submitted?)
break
else:
print "Required page change not yet happend. Waiting for user."
print ("Required page change not yet happend. Waiting for user.")
sleep(3)
def wait_until_page_complete(self):
......@@ -102,30 +130,34 @@ class Mozrepl:
tries=0
while True:
# Get the page load state
self.writeData("document.readyState")
self.writeData(b"document.readyState")
(state, valid) = self.readData()
sleep(0.2)
if state == "\"complete\"":
if state == b"\"complete\"":
# When completed wait a bit longer, in order to set repl again + HTML is updated fully, strange right?
sleep(2)
break
tries+=1
if tries == NR_RETRIES:
print "ERROR: Too many retries. Do you have internet connection at all? Exit." # Time-out
if tries == NR_RETRIES_DOCUMENT:
print ("ERROR: Too many retries. Do you have internet connection at all? Exit.") # Time-out
exit(1)
def open_url(self, url):
"""
Trigger a change of URL in current tab
"""
self.writeData("content.location.href='"+url.strip()+"'")
self.writeData(b"content.location.href='" + url.strip().encode('utf-8') + b"'")
self.skipData()
def writeData(self, str_buffer):
def writeData(self, byte_buffer):
"""
Write data via write. New line at the end.
"""
self.t.write(str_buffer + "\n")
try:
self.t.write(byte_buffer + b'\n')
except socket.error:
print ("Connection problem. Did you disable the plugin or closed Firefox??? Exit")
exit(1)
def readData(self, inContentImportant=False):
"""
......@@ -140,12 +172,17 @@ class Mozrepl:
"""
valid=True
i = len(self.READ_UNTIL_CHARS)
output = self.t.read_until(self.READ_UNTIL_CHARS)[:-i].strip()
try:
output = self.t.read_until(self.READ_UNTIL_CHARS)[:-i].strip()
except socket.error:
print ("Connection problem. Did you disable the plugin or closed Firefox??? Exit")
exit(1)
# During a context switch (page change) repl will go back to home,
# should the read command be part of the Document context (eg. is enter(content) required in order to work)
if inContentImportant:
# If a context uploading is seen, switch back to the content again: enter(content).
if output.startswith("Host context unloading"):
if output.startswith(b"Host context unloading"):
self.switchToContent()
# Mark as invalid, since the data is not what you expect to be
# re-execute the previous writeData() agaom/
......@@ -153,23 +190,31 @@ class Mozrepl:
return (output,valid)
def switchToContent(self):
self.writeData(self.SOCKET_NAME + ".enter(content)")
self.writeData(self.SOCKET_NAME + b".enter(content)")
self.skipData()
def readAllData(self):
return self.t.read_until("> ")
try:
return self.t.read_until(b"> ")
except socket.error:
print ("Connection problem. Did you disable the plugin or closed Firefox??? Exit")
exit(1)
def skipData(self):
"""
Also read data until, but ignore the output,
get the buffer clean
"""
self.t.read_until(self.READ_UNTIL_CHARS)
try:
self.t.read_until(self.READ_UNTIL_CHARS)
except socket.error:
print ("Connection problem. Did you disable the plugin or closed Firefox??? Exit")
exit(1)
def close(self):
"""
Close active connection
"""
#self.writeData(self.SOCKET_NAME + ".quit()")
#self.writeData(self.SOCKET_NAME + b".quit()")
self.t.close()
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