Skip to content

Commit

Permalink
Added option to pass cookies, that will be used when retrieving the p…
Browse files Browse the repository at this point in the history
…age.
  • Loading branch information
marcon2 committed Feb 8, 2013
1 parent 6488a1f commit 0db2279
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
6 changes: 5 additions & 1 deletion scripts/webkit2png
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ if __name__ == '__main__':
parser.add_option("-F", "--feature", dest="features", action="append", type="choice",
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")
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")
parser.add_option("-t", "--timeout", dest="timeout", default=0, type="int",
Expand Down Expand Up @@ -184,6 +186,8 @@ if __name__ == '__main__':
renderer.grabWholeWindow = options.window
renderer.renderTransparentBackground = options.transparent
renderer.encodedUrl = options.encoded_url
if options.cookies:
renderer.cookies = options.cookies

if options.scale:
renderer.scaleRatio = options.ratio
Expand All @@ -210,4 +214,4 @@ if __name__ == '__main__':
signal.signal(signal.SIGINT, signal.SIG_DFL)

QTimer.singleShot(0, __main_qt)
sys.exit(app.exec_())
sys.exit(app.exec_())
23 changes: 21 additions & 2 deletions webkit2png/webkit2png.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def __init__(self,**kwargs):
self.ignorePrompt = kwargs.get('ignorePrompt', True)
self.interruptJavaScript = kwargs.get('interruptJavaScript', True)
self.encodedUrl = kwargs.get('encodedUrl', False)
self.cookies = kwargs.get('cookies', [])

# Set some default options for QWebPage
self.qWebSettings = {
Expand Down Expand Up @@ -160,6 +161,19 @@ def render_to_bytes(self, url):
image.save(qBuffer, format)
return qBuffer.buffer().data()

## @brief The CookieJar class inherits QNetworkCookieJar to make a couple of functions public.
class CookieJar(QNetworkCookieJar):
def __init__(self, cookies, qtUrl, parent=None):
QNetworkCookieJar.__init__(self, parent)
for cookie in cookies:
QNetworkCookieJar.setCookiesFromUrl(self, QNetworkCookie.parseCookies(QByteArray(cookie)), qtUrl)

def allCookies(self):
return QNetworkCookieJar.allCookies(self)

def setAllCookies(self, cookieList):
QNetworkCookieJar.setAllCookies(self, cookieList)

class _WebkitRendererHelper(QObject):
"""This helper class is doing the real work. It is required to
allow WebkitRenderer.render() to be called "asynchronously"
Expand Down Expand Up @@ -268,9 +282,14 @@ def _load_page(self, url, width, height, timeout):
self.__loading = True
self.__loadingResult = False # Default
if self.encodedUrl:
self._page.mainFrame().load(QUrl.fromEncoded(url))
qtUrl = QUrl.fromEncoded(url)
else:
self._page.mainFrame().load(QUrl(url))
qtUrl = QUrl(url)
# Set the required cookies, if any
self.cookieJar = CookieJar(self.cookies, qtUrl)
self._page.networkAccessManager().setCookieJar( self.cookieJar )
# Load the page
self._page.mainFrame().load(qtUrl)
while self.__loading:
if timeout > 0 and time.time() >= cancelAt:
raise RuntimeError("Request timed out on %s" % url)
Expand Down

0 comments on commit 0db2279

Please sign in to comment.