Last active
May 23, 2016 17:06
-
-
Save tokland/22602afff6448ded53ce211fe96887e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import signal | |
from PyQt4 import QtCore, QtGui, QtWebKit | |
from PyQt4.QtNetwork import QNetworkAccessManager, QNetworkCookieJar, QNetworkCookie | |
class ExtendedNetworkCookieJar(QNetworkCookieJar): | |
def mozillaCookies(self): | |
""" | |
Return all cookies in Mozilla text format | |
# domain domain_flag path secure_connection expiration name value | |
.firefox.com TRUE / FALSE 946684799 MOZILLA_ID 100103 | |
""" | |
def get_cookie_line(cookie): | |
def bool2str(value): | |
return {True: "TRUE", False: "FALSE"}[value] | |
domain_flag = str(cookie.domain()).startswith(".") | |
return "\t".join([ | |
str(cookie.domain()), | |
bool2str(domain_flag), | |
str(cookie.path()), | |
bool2str(cookie.isSecure()), | |
str(cookie.expirationDate().toTime_t()), | |
str(cookie.name()), | |
str(cookie.value()), | |
]) | |
header = ["# Netscape HTTP Cookie File", ""] | |
lines = [get_cookie_line(cookie) for cookie in self.allCookies()] | |
return "\n".join(header + lines) | |
def setMozillaCookies(self, string_cookies): | |
"""Set all cookies from Mozilla test format string. | |
.firefox.com TRUE / FALSE 946684799 MOZILLA_ID 100103 | |
""" | |
def str2bool(value): | |
return {"TRUE": True, "FALSE": False}[value] | |
def get_cookie(line): | |
fields = [s.strip() for s in line.split("\t")] | |
if len(fields) != 7: | |
return | |
domain, domain_flag, path, is_secure, expiration, name, value = fields | |
cookie = QNetworkCookie(name, value) | |
cookie.setDomain(domain) | |
cookie.setPath(path) | |
cookie.setSecure(str2bool(is_secure)) | |
cookie.setExpirationDate(QtCore.QDateTime.fromTime_t(int(expiration))) | |
return cookie | |
cookies = [get_cookie(line) for line in string_cookies.splitlines() | |
if line.strip() and not line.strip().startswith("#")] | |
self.setAllCookies([c for c in cookies if c]) | |
class NetworkAccessManager(QNetworkAccessManager): | |
def createRequest(self, op, req, outgoingData): | |
print(req.url().toString()) | |
return QNetworkAccessManager.createRequest(self, op, req, outgoingData) | |
def page_finished(ok): | |
sys.exit() | |
def open_browser(url, cookies_path): | |
app = QtGui.QApplication([]) | |
manager = NetworkAccessManager() | |
browser = QtWebKit.QWebView() | |
webpage = QtWebKit.QWebPage() | |
webpage.setNetworkAccessManager(manager) | |
browser.setPage(webpage) | |
webpage.loadFinished.connect(page_finished) | |
if cookies_path: | |
cookie_jar = ExtendedNetworkCookieJar() | |
cookie_jar.setMozillaCookies(open(cookies_path).read()) | |
manager.setCookieJar(cookie_jar) | |
browser.setUrl(QtCore.QUrl.fromEncoded(url)) | |
browser.show() | |
app.exec_() | |
def main(args): | |
if not args: | |
print("Usage: qt-requests.py URL [COOKIES_FILE]") | |
sys.exit(1) | |
signal.signal(signal.SIGINT, signal.SIG_DFL) | |
url = args[0] | |
cookies_path = (args[1] if len(args) > 1 else None) | |
open_browser(url, cookies_path) | |
if __name__ == '__main__': | |
sys.exit(main(sys.argv[1:])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment