Uses memory streams instead of files, closes #6

This commit is contained in:
Natalia Portillo 2017-01-21 21:40:30 +00:00
parent 6ea543b077
commit d7a2db49b8
No known key found for this signature in database
GPG Key ID: 51D20488C724CA9F

76
wrp.py
View File

@ -5,7 +5,7 @@
# with an imagemap of clickable links. This is an adaptation of previous works by # with an imagemap of clickable links. This is an adaptation of previous works by
# picidae.net and Paul Hammond. # picidae.net and Paul Hammond.
__version__ = "1.3" __version__ = "1.4"
# #
# This program is based on the software picidae.py from picidae.net # This program is based on the software picidae.py from picidae.net
@ -59,11 +59,14 @@ import threading
import Queue import Queue
import sys import sys
import logging import logging
import StringIO
# Request queue (URLs go in here) # Request queue (URLs go in here)
REQ = Queue.Queue() REQ = Queue.Queue()
# Response queue (dummy response objects) # Response queue (dummy response objects)
RESP = Queue.Queue() RESP = Queue.Queue()
# Renders dictionary
RENDERS = {}
####################### #######################
### Linux CODEPATH ### ### Linux CODEPATH ###
@ -373,13 +376,12 @@ if sys.platform == "linux" or sys.platform == "linux2":
qBuffer = QBuffer() qBuffer = QBuffer()
image.save(qBuffer, 'jpg', QUALITY) image.save(qBuffer, 'jpg', QUALITY)
output = open(WebkitRenderer.req_jpg, 'w') output = StringIO.StringIO()
output.write(qBuffer.buffer().data()) output.write(qBuffer.buffer().data())
output.close() RENDERS[req[2]] = output
del renderer del renderer
print ">>> done: %s [%d kb]..." % (WebkitRenderer.req_jpg, print ">>> done: %s [%d kb]..." % (WebkitRenderer.req_jpg, output.len/1024)
os.path.getsize(WebkitRenderer.req_jpg)/1024)
RESP.put('') RESP.put('')
@ -470,10 +472,10 @@ elif sys.platform == "darwin":
if frame == webview.mainFrame(): if frame == webview.mainFrame():
view = frame.frameView().documentView() view = frame.frameView().documentView()
bitmapdata = self.captureView(view) output = StringIO.StringIO()
bitmapdata.representationUsingType_properties_( output.write(self.captureView(view).representationUsingType_properties_(
AppKit.NSGIFFileType, None).writeToFile_atomically_( AppKit.NSGIFFileType, None))
WebkitLoad.req_gif, objc.YES) RENDERS[WebkitLoad.req_gif] = output
# url of the rendered page # url of the rendered page
web_url = frame.dataSource().initialRequest().URL().absoluteString() web_url = frame.dataSource().initialRequest().URL().absoluteString()
@ -490,7 +492,7 @@ elif sys.platform == "darwin":
httpout.write("<A HREF=\"http://%s\">" httpout.write("<A HREF=\"http://%s\">"
"<IMG SRC=\"http://%s\" ALT=\"wrp-render\" ISMAP>\n" "<IMG SRC=\"http://%s\" ALT=\"wrp-render\" ISMAP>\n"
"</A>\n" % (WebkitLoad.req_map, WebkitLoad.req_gif)) "</A>\n" % (WebkitLoad.req_map, WebkitLoad.req_gif))
mapfile = open(WebkitLoad.req_map, "w+") mapfile = StringIO.StringIO()
mapfile.write("default %s\n" % (web_url)) mapfile.write("default %s\n" % (web_url))
else: else:
httpout.write("<IMG SRC=\"http://%s\" ALT=\"wrp-render\" USEMAP=\"#map\">\n" httpout.write("<IMG SRC=\"http://%s\" ALT=\"wrp-render\" USEMAP=\"#map\">\n"
@ -525,7 +527,7 @@ elif sys.platform == "darwin":
httpout.write("</BODY>\n</HTML>\n") httpout.write("</BODY>\n</HTML>\n")
if ISMAP == "true": if ISMAP == "true":
mapfile.close() RENDERS[WebkitLoad.req_map] = mapfile
# Return to Proxy thread and Loop... # Return to Proxy thread and Loop...
RESP.put('') RESP.put('')
@ -571,26 +573,23 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
# Serve Rendered GIF # Serve Rendered GIF
if gif_re: if gif_re:
img = gif_re.group(1) img = gif_re.group(1)
print ">>> GIF file request... " + img print ">>> request for rendered gif image... %s [%d kb]" \
% (img, RENDERS[img].len/1024)
self.send_response(200, 'OK') self.send_response(200, 'OK')
self.send_header('Content-type', 'image/gif') self.send_header('Content-type', 'image/gif')
self.end_headers() self.end_headers()
fimg = open(img) httpout.write(RENDERS[img].getvalue())
httpout.write(fimg.read()) del RENDERS[img]
fimg.close()
os.remove(img)
elif jpg_re: elif jpg_re:
img = jpg_re.group(1) img = jpg_re.group(1)
print ">>> request for rendered jpg image... %s [%d kb]" \ print ">>> request for rendered jpg image... %s [%d kb]" \
% (img, os.path.getsize(img)/1024) % (img, RENDERS[img].len/1024)
self.send_response(200, 'OK') self.send_response(200, 'OK')
self.send_header('Content-type', 'image/jpeg') self.send_header('Content-type', 'image/jpeg')
self.end_headers() self.end_headers()
fimg = open(img) httpout.write(RENDERS[img].getvalue())
httpout.write(fimg.read()) del RENDERS[img]
fimg.close()
os.remove(img)
# Process ISMAP Request # Process ISMAP Request
elif map_re: elif map_re:
@ -599,25 +598,24 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
req_y = int(map_re.group(3)) req_y = int(map_re.group(3))
print ">>> ISMAP request... %s [%d,%d] " % (map, req_x, req_y) print ">>> ISMAP request... %s [%d,%d] " % (map, req_x, req_y)
with open(map) as mapf: mapf = RENDERS[map]
goto_url = "none" mapf.seek(0)
for line in mapf.readlines(): goto_url = "none"
if re.match(r"(\S+)", line).group(1) == "default": for line in mapf.readlines():
default_url = re.match(r"\S+\s+(\S+)", line).group(1) if re.match(r"(\S+)", line).group(1) == "default":
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":
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))
max_x = int(rect.group(5)) max_x = int(rect.group(5))
max_y = int(rect.group(6)) max_y = int(rect.group(6))
if (req_x >= min_x) and \ if (req_x >= min_x) and \
(req_x <= max_x) and \ (req_x <= max_x) and \
(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)
mapf.close()
if goto_url == "none": if goto_url == "none":
goto_url = default_url goto_url = default_url