Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to 0.9.0 for Python 3 #70

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 25 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
**webkit2png**
==============
# webkit2png

About
------
## About
Python script that takes screenshots (browsershots) using webkit

##Installation
Ubuntu
------
- Add following packages: ``apt-get install python-qt4 libqt4-webkit xvfb``
- Install the flash plugin to screenshot Adobe Flash files: ``apt-get install flashplugin-installer``

Automated installation via ```pip```
-------------------------------------
- Install pip: ```apt-get install python-pip```
- Install webkit2png: ```pip install webkit2png```

Manual installation via Git
-----------------------------
- Install git: ``apt-get install git-core``
- Create directory: ``mkdir python-webkit2png``
## Requirement
python >= 3.7
PyQt >= 5.13
PyQtWebEngine >= 5.13

> Notice: Since Qt5, QtWebkit has been deprecated by QtWebEngine using Blink Engine from Chromium Project.

## Installation

### Debian/Ubuntu
- Add following packages: ``apt-get install libqt5core5a python3-pip``

#### Automated installation via ```pip```
- Install webkit2png: ```pip3 install webkit2png```

#### Manual installation via Git
- Install git: ``apt-get install git``
- Clone the project: ``git clone https://github.com/adamn/python-webkit2png.git python-webkit2png``
- Install with: ``python python-webkit2png/setup.py install``
- Install with: ``python3 python-webkit2png/setup.py install``
- If the requirement install failed, satified with: ``pip3 install -r requirements.txt``

FreeBSD
-------
- install qt4 webkit: ```www/py-qt4-webkit, www/qt4-webkit, devel/py-qt4```
### FreeBSD
- install qt5 webkit: ```www/py-qt5-webkit, www/qt5-webkit, devel/py-qt5```
- install pip: ``devel/py-pip``
- install via: ``pip install webkit2png``

Usage
=====
- For help run: ``python scripts/webkit2png -h``
## Usage
- For help run: ``python3 -m webkit2png -h``

![Alt Text](http://24.media.tumblr.com/tumblr_m9trixXFHn1rxlmf0o1_400.gif)
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
PyQt5==5.13.2
PyQt5-sip==12.7.0
PyQtWebEngine==5.13.2
sip==5.0.0
31 changes: 15 additions & 16 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
#!/usr/bin/env python

from setuptools import setup, find_packages
from setuptools import setup

version = '0.8.3'
version = '0.9.0'

description = "Takes snapshot of webpages using Webkit and Qt4"
description = "Takes snapshot of web pages using Webkit and Qt5"
long_description = description

setup(
name = "webkit2png",
version = version,
url = 'http://github.com/AdamN/python-webkit2png',
license = 'LGPL',
description = description,
long_description = long_description,
author = 'Roland Tapken',
author_email = 'roland at dau-sicher de',
packages = ['webkit2png'],
name="webkit2png",
version=version,
url='http://github.com/AdamN/python-webkit2png',
license='GNU Lesser General Public License',
description=description,
long_description=long_description,
author='Roland Tapken',
author_email='roland at dau-sicher de',
packages=['webkit2png'],
zip_safe=True,
include_package_data=True,
package_dir = [],
package_dir=[],
classifiers=[
'Development Status :: 4 - Beta',
'Environment :: Web Environment',
Expand All @@ -32,10 +32,9 @@
'Topic :: Multimedia :: Graphics :: Capture :: Screen Capture',
'Topic :: Utilities'
],
entry_points = {
entry_points={
'console_scripts': [
'webkit2png = webkit2png.scripts:main',
]
},
}, install_requires=['PyQt5', 'PyQtWebEngine']
)

4 changes: 2 additions & 2 deletions webkit2png/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from webkit2png import WebkitRenderer
__all__ = ['WebkitRenderer']
from .webkit2png import WebkitRenderer
__all__ = ['WebkitRenderer']
70 changes: 39 additions & 31 deletions webkit2png/scripts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# webkit2png.py
#
Expand All @@ -24,24 +24,26 @@
# - Add QTcpSocket support to create a "screenshot daemon" that
# can handle multiple requests at the same time.

from PyQt5.QtWidgets import QApplication

from webkit2png import WebkitRenderer

import sys
import signal
import os
import urlparse
import urllib.parse
import logging
from optparse import OptionParser

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
from PyQt4.QtNetwork import *
from PyQt5.QtCore import *
from PyQt5.QtNetwork import *
from PyQt5.QtWebEngineWidgets import QWebEngineSettings

VERSION="20091224"
VERSION = "20191201"
LOG_FILENAME = 'webkit2png.log'
logger = logging.getLogger('webkit2png')


def init_qtgui(display=None, style=None, qtargs=None):
"""Initiates the QApplication environment using the given args."""
if QApplication.instance():
Expand Down Expand Up @@ -72,7 +74,7 @@ def main():

# Enable HTTP proxy
if 'http_proxy' in os.environ:
proxy_url = urlparse.urlparse(os.environ.get('http_proxy'))
proxy_url = urllib.parse.urlparse(os.environ.get('http_proxy'))
proxy = QNetworkProxy(QNetworkProxy.HttpProxy, proxy_url.hostname, proxy_url.port)
QNetworkProxy.setApplicationProxy(proxy)

Expand All @@ -81,17 +83,18 @@ def main():
# $0 [--xvfb|--display=DISPLAY] [--debug] [--output=FILENAME] <URL>

description = "Creates a screenshot of a website using QtWebkit." \
+ "This program comes with ABSOLUTELY NO WARRANTY. " \
+ "This is free software, and you are welcome to redistribute " \
+ "it under the terms of the GNU General Public License v2."
+ "This program comes with ABSOLUTELY NO WARRANTY. " \
+ "This is free software, and you are welcome to redistribute " \
+ "it under the terms of the GNU General Public License v2."

parser = OptionParser(usage="usage: %prog [options] <URL>",
version="%prog " + VERSION + ", Copyright (c) Roland Tapken",
description=description, add_help_option=True)
parser.add_option("-x", "--xvfb", nargs=2, type="int", dest="xvfb",
help="Start an 'xvfb' instance with the given desktop size.", metavar="WIDTH HEIGHT")
parser.add_option("-g", "--geometry", dest="geometry", nargs=2, default=(0, 0), type="int",
help="Geometry of the virtual browser window (0 means 'autodetect') [default: %default].", metavar="WIDTH HEIGHT")
help="Geometry of the virtual browser window (0 means 'autodetect') [default: %default].",
metavar="WIDTH HEIGHT")
parser.add_option("-o", "--output", dest="output",
help="Write output to FILE instead of STDOUT.", metavar="FILE")
parser.add_option("-f", "--format", dest="format", default="png",
Expand All @@ -104,35 +107,38 @@ def main():
choices=["javascript", "plugins"],
help="Enable additional Webkit features ('javascript', 'plugins')", metavar="FEATURE")
parser.add_option("-c", "--cookie", dest="cookies", action="append",
help="Add this cookie. Use multiple times for more cookies. Specification is value of a Set-Cookie HTTP response header.", metavar="COOKIE")
help="Add this cookie. Use multiple times for more cookies. Specification is value of a Set-Cookie HTTP response header.",
metavar="COOKIE")
parser.add_option("-w", "--wait", dest="wait", default=0, type="int",
help="Time to wait after loading before the screenshot is taken [default: %default]", metavar="SECONDS")
help="Time to wait after loading before the screenshot is taken [default: %default]",
metavar="SECONDS")
parser.add_option("-t", "--timeout", dest="timeout", default=0, type="int",
help="Time before the request will be canceled [default: %default]", metavar="SECONDS")
parser.add_option("-W", "--window", dest="window", action="store_true",
help="Grab whole window instead of frame (may be required for plugins)", default=False)
parser.add_option("-T", "--transparent", dest="transparent", action="store_true",
help="Render output on a transparent background (Be sure to have a transparent background defined in the html)", default=False)
help="Render output on a transparent background (Be sure to have a transparent background defined in the html)",
default=False)
parser.add_option("", "--style", dest="style",
help="Change the Qt look and feel to STYLE (e.G. 'windows').", metavar="STYLE")
parser.add_option("", "--encoded-url", dest="encoded_url", action="store_true",
help="Treat URL as url-encoded", metavar="ENCODED_URL", default=False)
help="Treat URL as url-encoded", metavar="ENCODED_URL", default=False)
parser.add_option("-d", "--display", dest="display",
help="Connect to X server at DISPLAY.", metavar="DISPLAY")
parser.add_option("--debug", action="store_true", dest="debug",
help="Show debugging information.", default=False)
parser.add_option("--log", action="store", dest="logfile", default=LOG_FILENAME,
help="Select the log output file",)
help="Select the log output file", )

# Parse command line arguments and validate them (as far as we can)
(options,args) = parser.parse_args()
(options, args) = parser.parse_args()
if len(args) != 1:
parser.error("incorrect number of arguments")
if options.display and options.xvfb:
parser.error("options -x and -d are mutually exclusive")
options.url = args[0]

logging.basicConfig(filename=options.logfile,level=logging.WARN,)
logging.basicConfig(filename=options.logfile, level=logging.WARN, )

# Enable output of debugging information
if options.debug:
Expand All @@ -141,30 +147,31 @@ def main():
if options.xvfb:
# Start 'xvfb' instance by replacing the current process
server_num = int(os.getpid() + 1e6)
newArgs = ["xvfb-run", "--auto-servernum", "--server-num", str(server_num), "--server-args=-screen 0, %dx%dx24" % options.xvfb, sys.argv[0]]
newArgs = ["xvfb-run", "--auto-servernum", "--server-num", str(server_num),
"--server-args=-screen 0, %dx%dx24" % options.xvfb, sys.argv[0]]
skipArgs = 0
for i in range(1, len(sys.argv)):
if skipArgs > 0:
skipArgs -= 1
elif sys.argv[i] in ["-x", "--xvfb"]:
skipArgs = 2 # following: width and height
skipArgs = 2 # following: width and height
else:
newArgs.append(sys.argv[i])
logger.debug("Executing %s" % " ".join(newArgs))
try:
os.execvp(newArgs[0],newArgs[1:])
os.execvp(newArgs[0], newArgs[1:])
except OSError:
logger.error("Unable to find '%s'" % newArgs[0])
print >> sys.stderr, "Error - Unable to find '%s' for -x/--xvfb option" % newArgs[0]
sys.stderr.write("Error - Unable to find '{}' for -x/--xvfb option".format(newArgs[0]))
sys.exit(1)

# Prepare output ("1" means STDOUT)
if options.output is None:
options.output = sys.stdout
else:
options.output = open(options.output, "w")
options.output = open(options.output, "wb+")

logger.debug("Version %s, Python %s, Qt %s", VERSION, sys.version, qVersion());
logger.debug("Version %s, Python %s, Qt %s", VERSION, sys.version, qVersion())

# Technically, this is a QtGui application, because QWebPage requires it
# to be. But because we will have no user interaction, and rendering can
Expand Down Expand Up @@ -196,25 +203,26 @@ def __main_qt():

if options.features:
if "javascript" in options.features:
renderer.qWebSettings[QWebSettings.JavascriptEnabled] = True
renderer.qWebSettings[QWebEngineSettings.JavascriptEnabled] = True
if "plugins" in options.features:
renderer.qWebSettings[QWebSettings.PluginsEnabled] = True
renderer.qWebSettings[QWebEngineSettings.PluginsEnabled] = True

renderer.render_to_file(res=options.url, file_object=options.output)
options.output.close()
QApplication.exit(0)
except RuntimeError, e:
except RuntimeError as e:
logger.error("main: %s" % e)
print >> sys.stderr, e
sys.stderr.write(e)
QApplication.exit(1)

# Initialize Qt-Application, but make this script
# abortable via CTRL-C
app = init_qtgui(display = options.display, style=options.style)
# To interrupt via CTRL-C
app = init_qtgui(display=options.display, style=options.style)
signal.signal(signal.SIGINT, signal.SIG_DFL)

QTimer.singleShot(0, __main_qt)
return app.exec_()


if __name__ == '__main__':
sys.exit(main())
Loading