mirror of
https://github.com/tenox7/wrp.git
synced 2025-02-08 15:30:33 +00:00
Merge common code in a single executable that detects corresponding codepath and runs it, closes #12
Signed-off-by: Natalia Portillo <claunia@claunia.com>
This commit is contained in:
parent
4ebed0ad60
commit
966923949b
203
wrp.py
Executable file
203
wrp.py
Executable file
@ -0,0 +1,203 @@
|
||||
#!/usr/bin/env python2.7
|
||||
|
||||
# wrp.py - Web Rendering Proxy
|
||||
# A HTTP proxy service that renders the requested URL in to a GIF image associated
|
||||
# with an imagemap of clickable links. This is an adaptation of previous works by
|
||||
# picidae.net and Paul Hammond.
|
||||
|
||||
__version__ = "1.3"
|
||||
|
||||
#
|
||||
# This program is based on the software picidae.py from picidae.net
|
||||
# It was modified by Antoni Sawicki http://www.tenox.net/out/#wrp
|
||||
#
|
||||
# This program is based on the software webkit2png from Paul Hammond.
|
||||
# It was extended by picidae.net
|
||||
#
|
||||
# Copyright (c) 2013-2014 Antoni Sawicki
|
||||
# Copyright (c) 2012-2013 picidae.net
|
||||
# Copyright (c) 2004-2013 Paul Hammond
|
||||
# Copyright (c) 2017 Natalia Portillo
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
#
|
||||
|
||||
# Configuration options:
|
||||
PORT = 8080
|
||||
WIDTH = 1024
|
||||
HEIGHT = 768
|
||||
ISMAP = "true"
|
||||
|
||||
import re
|
||||
import random
|
||||
import os
|
||||
import time
|
||||
import string
|
||||
import urllib
|
||||
import socket
|
||||
import SocketServer
|
||||
import SimpleHTTPServer
|
||||
import threading
|
||||
import Queue
|
||||
import sys
|
||||
import logging
|
||||
|
||||
# claunia: Check how to use this in macOS
|
||||
#logging.basicConfig(filename='/dev/stdout',level=logging.WARN,)
|
||||
#logger = logging.getLogger('wrp');
|
||||
|
||||
# Request queue (URLs go in here)
|
||||
REQ = Queue.Queue()
|
||||
# Response queue (dummy response objects)
|
||||
RESP = Queue.Queue()
|
||||
|
||||
#######################
|
||||
### COMMON CODEPATH ###
|
||||
#######################
|
||||
class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
|
||||
def do_GET(self):
|
||||
req_url=self.path
|
||||
httpout=self.wfile
|
||||
|
||||
gif_re = re.match("http://(wrp-\d+\.gif).*", req_url)
|
||||
map_re = re.match("http://(wrp-\d+\.map).*?(\d+),(\d+)", req_url)
|
||||
ico_re = re.match("http://.+\.ico", req_url)
|
||||
jpg_re = re.match("http://(wrp-\d+\.jpg).*", req_url)
|
||||
|
||||
# Serve Rendered GIF
|
||||
if (gif_re):
|
||||
img=gif_re.group(1)
|
||||
print ">>> GIF file request... " + img
|
||||
self.send_response(200, 'OK')
|
||||
self.send_header('Content-type', 'image/gif')
|
||||
self.end_headers()
|
||||
fimg=open(img)
|
||||
httpout.write(fimg.read())
|
||||
fimg.close()
|
||||
os.remove(img)
|
||||
|
||||
elif (jpg_re):
|
||||
img=jpg_re.group(1)
|
||||
print ">>> request for rendered jpg image... %s [%d kb]" % (img, os.path.getsize(img)/1024)
|
||||
self.send_response(200, 'OK')
|
||||
self.send_header('Content-type', 'image/jpeg')
|
||||
self.end_headers()
|
||||
fimg = open(img)
|
||||
httpout.write(fimg.read())
|
||||
fimg.close()
|
||||
os.remove(img)
|
||||
|
||||
# Process ISMAP Request
|
||||
elif (map_re):
|
||||
map=map_re.group(1)
|
||||
req_x=int(map_re.group(2))
|
||||
req_y=int(map_re.group(3))
|
||||
print ">>> ISMAP request... %s [%d,%d] " % (map, req_x, req_y)
|
||||
|
||||
with open(map) as mapf:
|
||||
goto_url="none"
|
||||
for line in mapf.readlines():
|
||||
if(re.match("(\S+)", line).group(1) == "default"):
|
||||
default_url=re.match("\S+\s+(\S+)", line).group(1)
|
||||
|
||||
elif(re.match("(\S+)", line).group(1) == "rect"):
|
||||
rect=re.match("(\S+)\s+(\S+)\s+(\d+),(\d+)\s+(\d+),(\d+)", line)
|
||||
min_x=int(rect.group(3))
|
||||
min_y=int(rect.group(4))
|
||||
max_x=int(rect.group(5))
|
||||
max_y=int(rect.group(6))
|
||||
if( (req_x >= min_x) and (req_x <= max_x) and (req_y >= min_y) and (req_y <= max_y) ):
|
||||
goto_url=rect.group(2)
|
||||
|
||||
mapf.close()
|
||||
|
||||
if(goto_url == "none"):
|
||||
goto_url=default_url
|
||||
|
||||
print(">>> ISMAP redirect: %s\n" % (goto_url))
|
||||
|
||||
self.send_response(302, "Found")
|
||||
self.send_header("Location", goto_url)
|
||||
self.send_header("Content-type", "text/html")
|
||||
self.end_headers()
|
||||
httpout.write("<HTML><BODY><A HREF=\"%s\">%s</A></BODY></HTML>\n" % (goto_url, goto_url))
|
||||
|
||||
# ICO files, WebKit crashes on these
|
||||
elif (ico_re):
|
||||
self.send_error(415, "ICO not supported")
|
||||
self.end_headers()
|
||||
|
||||
# Process a web page request and generate image
|
||||
else:
|
||||
print ">>> URL request... " + req_url
|
||||
self.send_response(200, 'OK')
|
||||
self.send_header('Content-type', 'text/html')
|
||||
self.end_headers()
|
||||
|
||||
rnd = random.randrange(0,1000)
|
||||
|
||||
if sys.platform == "linux" or sys.platform == "linux2":
|
||||
import wrp_qt
|
||||
|
||||
"wrp-%s.jpg" % (rnd)
|
||||
"wrp-%s.map" % (rnd)
|
||||
|
||||
# To thread
|
||||
wrp_qt.REQ.put((httpout, req_url, "wrp-%s.jpg" % (rnd), "wrp-%s.map" % (rnd)))
|
||||
# Wait for completition
|
||||
wrp_qt.RESP.get()
|
||||
elif sys.platform == "darwin":
|
||||
import wrp_cocoa
|
||||
|
||||
"wrp-%s.gif" % (rnd)
|
||||
"wrp-%s.map" % (rnd)
|
||||
|
||||
# To WebKit Thread
|
||||
wrp_cocoa.REQ.put((httpout, req_url, "wrp-%s.gif" % (rnd), "wrp-%s.map" % (rnd)))
|
||||
# Wait for completition
|
||||
wrp_cocoa.RESP.get()
|
||||
|
||||
def run_proxy():
|
||||
httpd = SocketServer.TCPServer(('', PORT), Proxy)
|
||||
print "Web Rendering Proxy v%s serving at port: %s" % (__version__, PORT)
|
||||
while 1:
|
||||
httpd.serve_forever()
|
||||
|
||||
def main():
|
||||
# Launch Proxy Thread
|
||||
threading.Thread(target=run_proxy).start()
|
||||
|
||||
if sys.platform == "linux" or sys.platform == "linux2":
|
||||
import signal
|
||||
import PyQt4.QtCore
|
||||
import wrp_qt
|
||||
# Initialize Qt-Application, but make this script
|
||||
# abortable via CTRL-C
|
||||
app = wrp_qt.init_qtgui(display=None, style=None)
|
||||
signal.signal(signal.SIGINT, signal.SIG_DFL)
|
||||
|
||||
PyQt4.QtCore.QTimer.singleShot(0, wrp_qt.__main_qt)
|
||||
sys.exit(app.exec_())
|
||||
elif sys.platform == "darwin":
|
||||
import wrp_cocoa
|
||||
wrp_cocoa.main()
|
||||
else:
|
||||
sys.exit("Unsupported platform: %s. Exiting." % sys.platform)
|
||||
|
||||
if __name__ == '__main__' : main()
|
@ -5,7 +5,7 @@
|
||||
# with an imagemap of clickable links. This is an adaptation of previous works by
|
||||
# picidae.net and Paul Hammond.
|
||||
|
||||
__version__ = "1.2"
|
||||
__version__ = "1.3"
|
||||
|
||||
#
|
||||
# This program is based on the software picidae.py from picidae.net
|
||||
@ -191,94 +191,7 @@ class WebkitLoad (Foundation.NSObject, WebKit.protocols.WebFrameLoadDelegate):
|
||||
RESP.put('')
|
||||
self.getURL(webview)
|
||||
|
||||
class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
|
||||
def do_GET(self):
|
||||
req_url=self.path
|
||||
httpout=self.wfile
|
||||
|
||||
gif_re = re.match("http://(wrp-\d+\.gif).*", req_url)
|
||||
map_re = re.match("http://(wrp-\d+\.map).*?(\d+),(\d+)", req_url)
|
||||
ico_re = re.match("http://.+\.ico", req_url)
|
||||
|
||||
# Serve Rendered GIF
|
||||
if (gif_re):
|
||||
img=gif_re.group(1)
|
||||
print ">>> GIF file request... " + img
|
||||
self.send_response(200, 'OK')
|
||||
self.send_header('Content-type', 'image/gif')
|
||||
self.end_headers()
|
||||
fimg=open(img)
|
||||
httpout.write(fimg.read())
|
||||
fimg.close()
|
||||
os.remove(img)
|
||||
|
||||
# Process ISMAP Request
|
||||
elif (map_re):
|
||||
map=map_re.group(1)
|
||||
req_x=int(map_re.group(2))
|
||||
req_y=int(map_re.group(3))
|
||||
print ">>> ISMAP request... %s [%d,%d] " % (map, req_x, req_y)
|
||||
|
||||
with open(map) as mapf:
|
||||
goto_url="none"
|
||||
for line in mapf.readlines():
|
||||
if(re.match("(\S+)", line).group(1) == "default"):
|
||||
default_url=re.match("\S+\s+(\S+)", line).group(1)
|
||||
|
||||
elif(re.match("(\S+)", line).group(1) == "rect"):
|
||||
rect=re.match("(\S+)\s+(\S+)\s+(\d+),(\d+)\s+(\d+),(\d+)", line)
|
||||
min_x=int(rect.group(3))
|
||||
min_y=int(rect.group(4))
|
||||
max_x=int(rect.group(5))
|
||||
max_y=int(rect.group(6))
|
||||
if( (req_x >= min_x) and (req_x <= max_x) and (req_y >= min_y) and (req_y <= max_y) ):
|
||||
goto_url=rect.group(2)
|
||||
|
||||
mapf.close()
|
||||
|
||||
if(goto_url == "none"):
|
||||
goto_url=default_url
|
||||
|
||||
print(">>> ISMAP redirect: %s\n" % (goto_url))
|
||||
|
||||
self.send_response(302, "Found")
|
||||
self.send_header("Location", goto_url)
|
||||
self.send_header("Content-type", "text/html")
|
||||
self.end_headers()
|
||||
httpout.write("<HTML><BODY><A HREF=\"%s\">%s</A></BODY></HTML>\n" % (goto_url, goto_url))
|
||||
|
||||
# ICO files, WebKit crashes on these
|
||||
elif (ico_re):
|
||||
self.send_error(415, "ICO not supported")
|
||||
self.end_headers()
|
||||
|
||||
# Process a web page request and generate image
|
||||
else:
|
||||
print ">>> URL request... " + req_url
|
||||
self.send_response(200, 'OK')
|
||||
self.send_header('Content-type', 'text/html')
|
||||
self.end_headers()
|
||||
|
||||
rnd = random.randrange(0,1000)
|
||||
|
||||
"wrp-%s.gif" % (rnd)
|
||||
"wrp-%s.map" % (rnd)
|
||||
|
||||
# To WebKit Thread
|
||||
REQ.put((httpout, req_url, "wrp-%s.gif" % (rnd), "wrp-%s.map" % (rnd)))
|
||||
# Wait for completition
|
||||
RESP.get()
|
||||
|
||||
def run_proxy():
|
||||
httpd = SocketServer.TCPServer(('', PORT), Proxy)
|
||||
print "Web Rendering Proxy v%s serving at port: %s" % (__version__, PORT)
|
||||
while 1:
|
||||
httpd.serve_forever()
|
||||
|
||||
def main():
|
||||
# Launch Proxy Thread
|
||||
threading.Thread(target=run_proxy).start()
|
||||
|
||||
# Launch NS Application
|
||||
AppKit.NSApplicationLoad();
|
||||
app = AppKit.NSApplication.sharedApplication()
|
||||
@ -298,6 +211,3 @@ def main():
|
||||
loaddelegate.options = [""]
|
||||
webview.setFrameLoadDelegate_(loaddelegate)
|
||||
app.run()
|
||||
|
||||
if __name__ == '__main__' : main()
|
||||
|
@ -40,7 +40,7 @@ HEIGHT = 0 # eg.: 480, 600, 768, 0 for auto
|
||||
WAIT = 1 # sleep for 1 second to allow javascript renders
|
||||
QUALITY = 80 # jpeg image quality 0-100
|
||||
|
||||
__version__ = "1.1qt"
|
||||
__version__ = "1.3"
|
||||
|
||||
import re
|
||||
import random
|
||||
@ -184,8 +184,10 @@ class _WebkitRendererHelper(QObject):
|
||||
else:
|
||||
image = QPixmap.grabWidget(self._window)
|
||||
|
||||
httpout = WebkitRenderer.httpout
|
||||
|
||||
# Write URL map
|
||||
httpout.write("<!-- Web Rendering Proxy v%s by Antoni Sawicki -->\n<html>\n<body>\n<img src=\"http://%s\" alt=\"webrender\" usemap=\"#map\">\n<map name=\"map\">\n" % (__version__, IMG))
|
||||
httpout.write("<!-- Web Rendering Proxy v%s by Antoni Sawicki -->\n<html>\n<body>\n<img src=\"http://%s\" alt=\"webrender\" usemap=\"#map\">\n<map name=\"map\">\n" % (__version__, WebkitRenderer.req_jpg))
|
||||
frame = self._view.page().currentFrame()
|
||||
for x in frame.findAllElements('a'):
|
||||
value = x.attribute('href')
|
||||
@ -318,105 +320,49 @@ REQ = Queue.Queue()
|
||||
# Response queue (dummy response objects)
|
||||
RESP = Queue.Queue()
|
||||
|
||||
#import pdb; pdb.set_trace()
|
||||
# Technically, this is a QtGui application, because QWebPage requires it
|
||||
# to be. But because we will have no user interaction, and rendering can
|
||||
# not start before 'app.exec_()' is called, we have to trigger our "main"
|
||||
# by a timer event.
|
||||
def __main_qt():
|
||||
# Render the page.
|
||||
# If this method times out or loading failed, a
|
||||
# RuntimeException is thrown
|
||||
try:
|
||||
while True:
|
||||
req = REQ.get()
|
||||
WebkitRenderer.httpout = req[0]
|
||||
rurl = req[1]
|
||||
WebkitRenderer.req_jpg = req[2]
|
||||
WebkitRenderer.req_map = req[3]
|
||||
if rurl == "http://wrp.stop/":
|
||||
print ">>> Terminate Request Received"
|
||||
break
|
||||
|
||||
class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
|
||||
def do_GET(self):
|
||||
req_url=self.path
|
||||
global httpout
|
||||
httpout=self.wfile
|
||||
self.send_response(200, 'OK')
|
||||
# Initialize WebkitRenderer object
|
||||
renderer = WebkitRenderer()
|
||||
renderer.logger = logger
|
||||
renderer.width = WIDTH
|
||||
renderer.height = HEIGHT
|
||||
renderer.timeout = 60
|
||||
renderer.wait = WAIT
|
||||
renderer.grabWholeWindow = False
|
||||
|
||||
jpg_re = re.compile("http://webrender-[0-9]+\.jpg")
|
||||
ico_re = re.compile(".+\.ico")
|
||||
image = renderer.render(rurl)
|
||||
qBuffer = QBuffer()
|
||||
image.save(qBuffer, 'jpg', QUALITY)
|
||||
|
||||
if (jpg_re.search(req_url)):
|
||||
img=req_url.split("/")
|
||||
print ">>> request for rendered jpg image... %s [%d kb]" % (img[2], os.path.getsize(img[2])/1024)
|
||||
self.send_header('Content-type', 'image/jpeg')
|
||||
self.end_headers()
|
||||
fimg = open(img[2])
|
||||
httpout.write(fimg.read())
|
||||
fimg.close()
|
||||
os.remove(img[2])
|
||||
output = open(WebkitRenderer.req_jpg, 'w')
|
||||
output.write(qBuffer.buffer().data())
|
||||
output.close()
|
||||
|
||||
del renderer
|
||||
print ">>> done: %s [%d kb]..." % (WebkitRenderer.req_jpg, os.path.getsize(WebkitRenderer.req_jpg)/1024)
|
||||
|
||||
elif (ico_re.search(req_url)):
|
||||
print ">>> request for .ico file - skipping"
|
||||
self.send_error(404, "ICO not supported")
|
||||
self.end_headers()
|
||||
|
||||
else:
|
||||
print ">>> request for url: " + req_url
|
||||
self.send_header('Content-type', 'text/html')
|
||||
self.end_headers()
|
||||
RESP.put('')
|
||||
|
||||
global IMG
|
||||
IMG = "webrender-%s.jpg" % (random.randrange(0,1000))
|
||||
|
||||
# To thread
|
||||
REQ.put(req_url)
|
||||
# Wait for completition
|
||||
RESP.get()
|
||||
|
||||
def run_proxy():
|
||||
httpd = SocketServer.TCPServer(('', PORT), Proxy)
|
||||
print "Web Rendering Proxy v%s serving port: %s" % (__version__, PORT)
|
||||
while 1:
|
||||
httpd.serve_forever()
|
||||
|
||||
def main():
|
||||
# Launch Proxy Thread
|
||||
threading.Thread(target=run_proxy).start()
|
||||
|
||||
# Technically, this is a QtGui application, because QWebPage requires it
|
||||
# to be. But because we will have no user interaction, and rendering can
|
||||
# not start before 'app.exec_()' is called, we have to trigger our "main"
|
||||
# by a timer event.
|
||||
def __main_qt():
|
||||
# Render the page.
|
||||
# If this method times out or loading failed, a
|
||||
# RuntimeException is thrown
|
||||
try:
|
||||
while True:
|
||||
rurl = REQ.get()
|
||||
if rurl == "http://wrp.stop/":
|
||||
print ">>> Terminate Request Received"
|
||||
break
|
||||
|
||||
# Initialize WebkitRenderer object
|
||||
renderer = WebkitRenderer()
|
||||
renderer.logger = logger
|
||||
renderer.width = WIDTH
|
||||
renderer.height = HEIGHT
|
||||
renderer.timeout = 60
|
||||
renderer.wait = WAIT
|
||||
renderer.grabWholeWindow = False
|
||||
|
||||
image = renderer.render(rurl)
|
||||
qBuffer = QBuffer()
|
||||
image.save(qBuffer, 'jpg', QUALITY)
|
||||
|
||||
output = open(IMG, 'w')
|
||||
output.write(qBuffer.buffer().data())
|
||||
output.close()
|
||||
|
||||
del renderer
|
||||
print ">>> done: %s [%d kb]..." % (IMG, os.path.getsize(IMG)/1024)
|
||||
|
||||
RESP.put('')
|
||||
|
||||
QApplication.exit(0)
|
||||
except RuntimeError, e:
|
||||
logger.error("main: %s" % e)
|
||||
print >> sys.stderr, e
|
||||
QApplication.exit(1)
|
||||
|
||||
# Initialize Qt-Application, but make this script
|
||||
# abortable via CTRL-C
|
||||
app = init_qtgui(display=None, style=None)
|
||||
signal.signal(signal.SIGINT, signal.SIG_DFL)
|
||||
|
||||
QTimer.singleShot(0, __main_qt)
|
||||
sys.exit(app.exec_())
|
||||
|
||||
if __name__ == '__main__' : main()
|
||||
QApplication.exit(0)
|
||||
except RuntimeError, e:
|
||||
logger.error("main: %s" % e)
|
||||
print >> sys.stderr, e
|
||||
QApplication.exit(1)
|
Loading…
x
Reference in New Issue
Block a user