def convert(args): profile = QWebEngineProfile() page = HeadlessPage(profile) settings = page.settings() settings.setAttribute(settings.JavascriptCanOpenWindows, False) settings.setAttribute(settings.WebGLEnabled, False) settings.setAttribute(settings.AutoLoadIconsForPage, False) settings.setAttribute(settings.ShowScrollBars, False) qurl = QUrl(args['url']) req = QWebEngineHttpRequest(qurl) for name, value in args.get('headers', ()): req.setHeader(name.encode('utf-8'), value.encode('utf-8')) store = page.profile().cookieStore() for name, value in args.get('cookies', ()): cookie = QNetworkCookie(name.encode('utf-8'), value.encode('utf-8')) cookie.setDomain(qurl.host()) store.setCookie(cookie) with prepare_loop(page.loadFinished): page.load(req) for js in args.get('run_script', ()): with prepare_loop() as loop: page.runJavaScript(js, lambda _: loop.quit()) if args['dest'] != '-': with prepare_loop(page.pdfPrintingFinished): page.printToPdf(os.path.abspath(args['dest'])) else: sys.stdout.buffer.write(page.printToPdfAndReturn())
def onCookieAdd(self, cookie: QNetworkCookie): """ :param cookie: QNetworkCookie """ domain = cookie.domain() # '.hf666.net' path = cookie.path() # '/' name = self.bytes2str(cookie.name().data()) # QByteArray-> (char)bytes value = self.bytes2str(cookie.value().data()) self.DomainCookies[name] = value # if domain in self.DomainCookies: # _cookie = self.DomainCookies[domain] # _cookie[name] = value # else: # self.DomainCookies[domain] = { # name: value # } # # -------------------------- ↓ # # # -------------------------- ↑ domain_path = domain + path if domain_path in self.PathCookies: _cookie = self.PathCookies[domain_path] _cookie[name] = value else: self.PathCookies[domain_path] = { name: value }
def on_cookie_added(self, cookie: QtNetwork.QNetworkCookie): """Handle set cookie""" try: self._cookies[_qbytearray_to_str( cookie.name())] = _qbytearray_to_str(cookie.value()) except: logger.exception( f'Failed to set cookie: {cookie.name()}={cookie.value()}')
def __init__(self, cookie: QNetworkCookie, parent: QWidget = None) -> None: super().__init__(parent) self.setupUi(self) self.setAutoFillBackground(True) self.m_nameLabel.setText(cookie.name().data().decode()) self.m_domainLabel.setText(cookie.domain()) self.m_viewButton.clicked.connect(self.viewClicked) self.m_deleteButton.clicked.connect(self.deleteClicked)
def __on_cookie_added(self, cookie): print('on add cookie', cookie.toRawForm(1)) for c in self.__cookies: if c.hasSameIdentifier(cookie): return print('coocke', cookie) print('net cookie', QNetworkCookie(cookie)) self.__cookies.append(QNetworkCookie(cookie))
def cookie_added(self, cookie: QNetworkCookie): if self.check_cookie_domain(cookie): name = cookie.name().data().decode() value = cookie.value().data().decode() self.saved_cookies[name] = value for _name in self.required_cookies: if _name not in self.saved_cookies: break else: print(self.required_cookies, self.saved_cookies) self.succeed.emit(self.saved_cookies)
def handleCookieAdded(self, cookie: QNetworkCookie): if self.containsCookie(cookie): return widget = CookieWidget(cookie) widget.setHighlighted(len(self.m_cookies) % 2) self.m_cookies.append(QNetworkCookie(cookie)) self.m_layout.insertWidget(0, widget) widget.deleteClicked.connect( partial(self._on_deleteClicked, QNetworkCookie(cookie), widget)) widget.viewClicked.connect( partial(self._on_viewClicked, QNetworkCookie(cookie)))
def add(self, cookie_dict, scheme, hostname, path="/"): cookie_list = [] for key, value in cookie_dict.items(): cookie = QNetworkCookie() cookie.setDomain(hostname) cookie.setPath(path) key_b = QByteArray() key_b.append(key) cookie.setName(key_b) value_b = QByteArray() value_b.append(value) cookie.setValue(value_b) cookie_list.append(cookie) self._cookie_jar.setCookiesFromUrl(cookie_list, QUrl(str(scheme)))
def test_set_cookies_accept(config_stub, qtbot, monkeypatch): """Test setCookiesFromUrl with cookies enabled.""" config_stub.data = CONFIG_ALL_COOKIES ram_jar = cookies.RAMCookieJar() cookie = QNetworkCookie(b'foo', b'bar') url = QUrl('http://example.com/') with qtbot.waitSignal(ram_jar.changed): assert ram_jar.setCookiesFromUrl([cookie], url) # assert the cookies are added correctly all_cookies = ram_jar.cookiesForUrl(url) assert len(all_cookies) == 1 saved_cookie = all_cookies[0] expected = cookie.name(), cookie.value() assert saved_cookie.name(), saved_cookie.value() == expected
def __init__(self, parent=None, options=None): """ Load cookies from a file """ super(CookieJar, self).__init__(parent) print("INIT CookieJar") if options['cookie_allow'] is None: self.allowed = [] else: self.allowed = options['cookie_allow'] self.storage = options['cookie_file'] if self.storage is not None: self.storage = expanduser("~/.eilat/cookies/") + self.storage print(self.storage) try: with open(self.storage, "r") as readfile: cookies = [ QNetworkCookie.parseCookies(k) for k in readfile.readlines() ] cookies = [x for y in cookies for x in y] # flatten self.setAllCookies(cookies) # COO02 except IOError: print("LOAD COOKIES: empty?")
def loadCookies(self, cookies): """ Public method to restore the saved cookies. @param cookies byte array containing the saved cookies (QByteArray) @return list of cookies """ if cookies.isEmpty(): return [] cookiesList = [] data = QByteArray(cookies) stream = QDataStream(data, QIODevice.ReadOnly) stream.setVersion(QDataStream.Qt_4_6) version = stream.readUInt16() if version != self.JAR_VERSION: return [] stream.readUInt32() # number of cookies rawCookie = QByteArray() while not stream.atEnd(): stream >> rawCookie newCookies = QNetworkCookie.parseCookies(rawCookie) for newCookie in newCookies: cookiesList.append(newCookie) return cookiesList
def parse_cookies(self): """Parse cookies from lineparser and store them.""" cookies: Sequence[QNetworkCookie] = [] for line in self._lineparser: line_cookies = QNetworkCookie.parseCookies(line) cookies += line_cookies # type: ignore[operator] self.setAllCookies(cookies)
def _on_deleteClicked(self, cookie: QNetworkCookie, widget: QWidget): self.m_store.deleteCookie(QNetworkCookie(cookie)) sip.delete(widget) self.m_cookies.remove(cookie) for i in range(self.m_layout.count() - 1, -1, -1): w = self.m_layout.itemAt(i).widget() if isinstance(w, CookieWidget): w.setHighlighted(i % 2)
def test_set_cookies_accept(config_stub, qtbot, monkeypatch): """Test setCookiesFromUrl with cookies enabled.""" monkeypatch.setattr(objects, 'backend', usertypes.Backend.QtWebKit) config_stub.val.content.cookies.accept = 'all' ram_jar = cookies.RAMCookieJar() cookie = QNetworkCookie(b'foo', b'bar') url = QUrl('http://example.com/') with qtbot.waitSignal(ram_jar.changed): assert ram_jar.setCookiesFromUrl([cookie], url) # assert the cookies are added correctly all_cookies = ram_jar.cookiesForUrl(url) assert len(all_cookies) == 1 saved_cookie = all_cookies[0] expected = cookie.name(), cookie.value() assert saved_cookie.name(), saved_cookie.value() == expected
def restore_cookies(self): if os.path.exists(self.cookie_path): with open(self.cookie_path, 'r') as f: lines = '' for line in f: lines = lines + line allCookies = QNetworkCookie.parseCookies(lines) QNetworkCookieJar.setAllCookies(self, allCookies)
def __init__(self, cookie: QNetworkCookie = None, parent: QWidget = None) -> None: super().__init__(parent) self.setupUi(self) if cookie: self.m_nameLineEdit.setText(cookie.name().data().decode()) self.m_domainLineEdit.setText(cookie.domain()) self.m_valueLineEdit.setText(cookie.value().data().decode()) self.m_pathLineEdit.setText(cookie.path()) self.m_dateEdit.setDate(cookie.expirationDate().date()) self.m_isSecureComboBox.addItem( self.tr("yes") if cookie.isSecure() else self.tr("no")) self.m_isHttpOnlyComboBox.addItem( self.tr("yes") if cookie.isHttpOnly() else self.tr("no")) self.m_addButton.setVisible(False) self.m_cancelButton.setText(self.tr("Close")) else: self.m_nameLineEdit.setReadOnly(False) self.m_domainLineEdit.setReadOnly(False) self.m_valueLineEdit.setReadOnly(False) self.m_pathLineEdit.setReadOnly(False) self.m_dateEdit.setReadOnly(False) self.m_dateEdit.setDate( QDateTime.currentDateTime().addYears(1).date()) self.m_isSecureComboBox.addItem(self.tr("no")) self.m_isSecureComboBox.addItem(self.tr("yes")) self.m_isHttpOnlyComboBox.addItem(self.tr("no")) self.m_isHttpOnlyComboBox.addItem(self.tr("yes"))
def __init__(self, parent=None): super().__init__(parent) datadir = utils.get_standard_dir(QStandardPaths.DataLocation) self._linecp = lineparser.LineConfigParser(datadir, 'cookies', binary=True) cookies = [] for line in self._linecp: cookies += QNetworkCookie.parseCookies(line) self.setAllCookies(cookies)
def __cookie_to_json(self): cookies_list_info = [] for c in self.__cookies: c = QNetworkCookie(c) data = { "name": bytearray(c.name()).decode(), "domain": c.domain(), "value": bytearray(c.value()).decode(), "path": c.path(), "expirationDate": c.expirationDate().toString(Qt.ISODate), "secure": c.isSecure(), "httponly": c.isHttpOnly() } cookies_list_info.append(data) return cookies_list_info
def __init__(self, parent=None): super().__init__(parent) datadir = standarddir.get(QStandardPaths.DataLocation) self._linecp = lineparser.LineConfigParser(datadir, 'cookies', binary=True) cookies = [] for line in self._linecp: cookies += QNetworkCookie.parseCookies(line) self.setAllCookies(cookies) objreg.get('config').changed.connect(self.cookies_store_changed)
def check_cookie_domain(self, cookie: QNetworkCookie): """ 检查 Cookie 的域名 :param cookie: Cookie :type cookie: QNetworkCookie :return: 是否匹配域名 :rtype: bool """ cookie_domain = cookie.domain().lstrip('.') urisegs = urlparse(self.init_uri) return urisegs.hostname.endswith(cookie_domain)
def load_cookie(self): cookies = [] if os.path.isfile(cookie_file): with open(cookie_file, 'rb') as f: for line in f: cookie = QNetworkCookie.parseCookies(line) self.cookiejar.setCookiesFromUrl(cookie, QUrl(longzhu_login_url)) self.page().profile().cookieStore().setCookie(cookie[0]) return True return False
def __init__(self, parent=None): super().__init__(parent) self._lineparser = lineparser.LineParser( standarddir.data(), 'cookies', binary=True, parent=self) cookies = [] for line in self._lineparser: cookies += QNetworkCookie.parseCookies(line) self.setAllCookies(cookies) objreg.get('config').changed.connect(self.cookies_store_changed) objreg.get('save-manager').add_saveable( 'cookies', self.save, self.changed, config_opt=('content', 'cookies-store'))
def on_sigterm(signum, frame): logger.info("Terminate requested.") # Force flush cookieStore to disk. Without this hack the cookieStore may # not be synced at all if the browser lives only for a short amount of # time. Something is off with the call order of destructors as there is no # such issue in C++. # See: https://github.com/qutebrowser/qutebrowser/commit/8d55d093f29008b268569cdec28b700a8c42d761 cookie = QNetworkCookie() QWebEngineProfile.defaultProfile().cookieStore().deleteCookie(cookie) # Give some time to actually save cookies exit_timer = QTimer(app) exit_timer.timeout.connect(QApplication.quit) exit_timer.start(1000) # ms
def __init__(self, parent=None): super().__init__(parent) self._lineparser = lineparser.LineParser(standarddir.data(), 'cookies', binary=True, parent=self) cookies = [] for line in self._lineparser: cookies += QNetworkCookie.parseCookies(line) self.setAllCookies(cookies) objreg.get('config').changed.connect(self.cookies_store_changed) objreg.get('save-manager').add_saveable('cookies', self.save, self.changed, config_opt=('content', 'cookies-store'))
def __update_cookie_store(self, cookie_store): from PyQt5.QtNetwork import QNetworkCookie from PyQt5.QtCore import QByteArray for key, cookie in self.__cookies.items(): # Skip the auth token as we'll get it again after a successful login if key == "_xsrf": continue # Add the cookie to store value = str(cookie) if ":" in value: value = value.split(":", 1)[1].strip() for morsel in QNetworkCookie.parseCookies( QByteArray(value.encode("utf-8"))): cookie_store.setCookie(morsel)
def loadData(): # Load history. global history global geolocation_whitelist global geolocation_blacklist raw_history = data.value("data/History") if type(raw_history) is str: history = json.loads(raw_history) if type(history) is list: new_history = {} for item in history: new_history[item] = {"title": item, "last_visited": 0} history = new_history # Load cookies. try: raw_cookies = json.loads(str(data.value("data/Cookies"))) except: pass else: if type(raw_cookies) is list: cookies = [ QNetworkCookie().parseCookies(QByteArray(cookie))[0] for cookie in raw_cookies ] network.cookie_jar.setAllCookies(cookies) try: wl = json.loads(str(data.value("data/GeolocationWhitelist"))) except: pass else: if type(wl) is list: geolocation_whitelist = wl try: bl = json.loads(str(data.value("data/GeolocationBlacklist"))) except: pass else: if type(bl) is list: geolocation_blacklist = bl
def onCookieAdded(self, cookie): for c in self.cookies: if c.hasSameIdentifier(cookie): return self.cookies.append(QNetworkCookie(cookie)) cookies_list_info = [] for c in self.cookies: data = { "name": bytearray(c.name()).decode(), "domain": c.domain(), "value": bytearray(c.value()).decode(), "path": c.path(), "expirationDate": c.expirationDate().toString(Qt.ISODate), "secure": c.isSecure(), "httponly": c.isHttpOnly() } cookies_list_info.append(data) import json json = json.dumps(cookies_list_info) f = open(lojCookiePath, "w") f.write(json) f.close()
def __init__(self): super().__init__() self.setWindowTitle('My Browser') self.resize(800, 600) url = 'https://www.baidu.com' req_cookies = { 'BD_UPN': '', 'PSTM': '', 'BIDUPSID': '', 'BAIDUID': '', 'BDORZ': '', 'delPer': '', 'BD_CK_SAM': '', 'PSINO': '', 'BDRCVFR[feWj1Vr5u3D]': '', 'ZD_ENTRY': '', 'H_PS_645EC': '', 'BDUSS': '', 'BD_HOME': '', 'H_PS_PSSID': '' } self.webview = QWebEngineView() cookiestore = self.webview.page().profile().cookieStore() cookie = QNetworkCookie() for key, value in req_cookies.items(): # 设置cookie的键 cookie.setName(key.encode()) # 设置cookie的值 cookie.setValue(value.encode()) # 设置cookie的域 cookie.setDomain('.baidu.com') cookiestore.setCookie(cookie) self.webview.load(QUrl(url)) self.setCentralWidget(self.webview)
def __init__(self, parent=None, options=None): """ Load cookies from a file """ super(CookieJar, self).__init__(parent) print("INIT CookieJar") if options['cookie_allow'] is None: self.allowed = [] else: self.allowed = options['cookie_allow'] self.storage = options['cookie_file'] if self.storage is not None: self.storage = expanduser("~/.eilat/cookies/") + self.storage print(self.storage) try: with open(self.storage, "r") as readfile: cookies = [QNetworkCookie.parseCookies(k) for k in readfile.readlines()] cookies = [x for y in cookies for x in y] # flatten self.setAllCookies(cookies) # COO02 except IOError: print("LOAD COOKIES: empty?")
def toPyCookie(qt_cookie: QNetworkCookie) -> Cookie: port = None port_specified = False secure = qt_cookie.isSecure() name = qt_cookie.name().data().decode() value = qt_cookie.value().data().decode() v = qt_cookie.path() path_specified = bool(v != "") path = v if path_specified else None v = qt_cookie.domain() domain_specified = bool(v != "") domain = v if domain_specified: domain_initial_dot = v.startswith(".") else: domain_initial_dot = None v = int(qt_cookie.expirationDate().toTime_t()) expires = 2147483647 if v > 2147483647 else v rest = {"HttpOnly": qt_cookie.isHttpOnly()} discard = False return Cookie( 0, name, value, port, port_specified, domain, domain_specified, domain_initial_dot, path, path_specified, secure, expires, discard, None, None, rest, )
def parse_cookies(self): """Parse cookies from lineparser and store them.""" cookies = [] for line in self._lineparser: cookies += QNetworkCookie.parseCookies(line) self.setAllCookies(cookies)
def load(self): strCookies = self.settings.value('cookies') if strCookies: self.setAllCookies(QNetworkCookie.parseCookies(strCookies))
def har_cookie2qt(cls, cookie): qcookie = QNetworkCookie() qcookie.setName(to_bytes(cookie["name"])) qcookie.setValue(to_bytes(cookie["value"])) if 'domain' in cookie: qcookie.setDomain(cookie["domain"]) if 'httpOnly' in cookie: qcookie.setHttpOnly(cookie["httpOnly"]) if 'secure' in cookie: qcookie.setSecure(cookie["secure"]) if 'path' in cookie: qcookie.setPath(cookie["path"]) if cookie.get('expires'): expires = QDateTime.fromString(cookie["expires"], Qt.ISODate) qcookie.setExpirationDate(expires) return qcookie
def load_cookie(self): with open(self.cookie_file, 'rb+') as store: cookies = store.read() return QNetworkCookie.parseCookies(cookies)
def load(self): data = self.cookie_store.value('cookies', []) if data: self.setAllCookies([QNetworkCookie.parseCookies(c)[0] for c in data])