Implemented ISMAP on Linux, closes #3.

This commit is contained in:
Natalia Portillo 2017-01-22 16:42:32 +00:00
parent e34a63604f
commit 90d1e82b00
No known key found for this signature in database
GPG Key ID: 51D20488C724CA9F

52
wrp.py
View File

@ -210,18 +210,45 @@ if sys.platform == "linux" or sys.platform == "linux2":
httpout = WebkitRenderer.httpout httpout = WebkitRenderer.httpout
frame = self._view.page().currentFrame()
web_url = frame.url().toString()
# Write URL map # Write URL map
httpout.write("<!-- Web Rendering Proxy v%s by Antoni Sawicki -->\n" httpout.write("<!-- Web Rendering Proxy v%s by Antoni Sawicki -->\n"
"<html>\n<body>\n" % (__version__))
"<img src=\"http://%s\" alt=\"webrender\" usemap=\"#map\">\n" httpout.write("<!-- Request for [%s] frame [%s] -->\n"
"<map name=\"map\">\n" % (__version__, WebkitRenderer.req_jpg)) % (WebkitRenderer.req_url, web_url))
frame = self._view.page().currentFrame() httpout.write("<HTML><HEAD><TITLE>WRP%s:%s</TITLE></HEAD>\n<BODY>\n"
% (__version__, web_url))
if ISMAP == "true":
httpout.write("<A HREF=\"http://%s\">"
"<IMG SRC=\"http://%s\" ALT=\"wrp-render\" ISMAP>\n"
"</A>\n" % (WebkitRenderer.req_map, WebkitRenderer.req_jpg))
mapfile = StringIO.StringIO()
mapfile.write("default %s\n" % (web_url))
else:
httpout.write("<IMG SRC=\"http://%s\" ALT=\"wrp-render\" USEMAP=\"#map\">\n"
"<MAP NAME=\"map\">\n" % (WebkitRenderer.req_jpg))
for x in frame.findAllElements('a'): for x in frame.findAllElements('a'):
value = x.attribute('href') turl = QUrl(web_url).resolved(QUrl(x.attribute('href'))).toString()
xmin, ymin, xmax, ymax = x.geometry().getCoords() xmin, ymin, xmax, ymax = x.geometry().getCoords()
httpout.write("<area shape=\"rect\" coords=\"%i,%i,%i,%i\" alt=\"%s\" href=\"%s\">" if ISMAP == "true":
"\n" % (xmin, ymin, xmax, ymax, value, value)) mapfile.write("rect %s %i,%i %i,%i\n" % (turl, xmin, ymin, xmax, ymax))
httpout.write("</map>\n</body>\n</html>\n") else:
httpout.write("<AREA SHAPE=\"RECT\""
" COORDS=\"%i,%i,%i,%i\""
" ALT=\"%s\" HREF=\"%s\">\n"
% (xmin, ymin, xmax, ymax, turl, turl))
if ISMAP != "true":
httpout.write("</MAP>\n")
httpout.write("</BODY>\n</HTML>\n")
if ISMAP == "true":
RENDERS[WebkitRenderer.req_map] = mapfile
return image return image
@ -355,10 +382,10 @@ if sys.platform == "linux" or sys.platform == "linux2":
while True: while True:
req = REQ.get() req = REQ.get()
WebkitRenderer.httpout = req[0] WebkitRenderer.httpout = req[0]
rurl = req[1] WebkitRenderer.req_url = req[1]
WebkitRenderer.req_jpg = req[2] WebkitRenderer.req_jpg = req[2]
WebkitRenderer.req_map = req[3] WebkitRenderer.req_map = req[3]
if rurl == "http://wrp.stop/": if WebkitRenderer.req_url == "http://wrp.stop/":
print ">>> Terminate Request Received" print ">>> Terminate Request Received"
QApplication.exit(0) QApplication.exit(0)
break break
@ -372,7 +399,7 @@ if sys.platform == "linux" or sys.platform == "linux2":
renderer.wait = WAIT renderer.wait = WAIT
renderer.grabWholeWindow = False renderer.grabWholeWindow = False
image = renderer.render(rurl) image = renderer.render(WebkitRenderer.req_url)
qBuffer = QBuffer() qBuffer = QBuffer()
image.save(qBuffer, 'jpg', QUALITY) image.save(qBuffer, 'jpg', QUALITY)
@ -606,6 +633,7 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
default_url = re.match(r"\S+\s+(\S+)", line).group(1) default_url = re.match(r"\S+\s+(\S+)", line).group(1)
elif re.match(r"(\S+)", line).group(1) == "rect": elif re.match(r"(\S+)", line).group(1) == "rect":
try:
rect = re.match(r"(\S+)\s+(\S+)\s+(\d+),(\d+)\s+(\d+),(\d+)", line) rect = re.match(r"(\S+)\s+(\S+)\s+(\d+),(\d+)\s+(\d+),(\d+)", line)
min_x = int(rect.group(3)) min_x = int(rect.group(3))
min_y = int(rect.group(4)) min_y = int(rect.group(4))
@ -616,6 +644,8 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
(req_y >= min_y) and \ (req_y >= min_y) and \
(req_y <= max_y): (req_y <= max_y):
goto_url = rect.group(2) goto_url = rect.group(2)
except AttributeError:
pass
if goto_url == "none": if goto_url == "none":
goto_url = default_url goto_url = default_url