def __init__(self, *args): QWebView.__init__(self, *args) self.gui = None self.tags = '' self.create_browser = None self._page = NPWebPage() self.setPage(self._page) self.cookie_jar = QNetworkCookieJar() self.page().networkAccessManager().setCookieJar(self.cookie_jar) http_proxy = get_proxies().get('http', None) if http_proxy: proxy_parts = urlparse(http_proxy) proxy = QNetworkProxy() proxy.setType(QNetworkProxy.HttpProxy) if proxy_parts.username: proxy.setUser(proxy_parts.username) if proxy_parts.password: proxy.setPassword(proxy_parts.password) if proxy_parts.hostname: proxy.setHostName(proxy_parts.hostname) if proxy_parts.port: proxy.setPort(proxy_parts.port) self.page().networkAccessManager().setProxy(proxy) self.page().setForwardUnsupportedContent(True) self.page().unsupportedContent.connect(self.start_download) self.page().downloadRequested.connect(self.start_download) self.page().networkAccessManager().sslErrors.connect(self.ignore_ssl_errors)
def genesis(self, gui): self.gui = gui r = self.register r('worker_limit', config, restart_required=True, setting=WorkersSetting) r('enforce_cpu_limit', config, restart_required=True) r('worker_max_time', gprefs) self.opt_worker_limit.setToolTip( textwrap.fill( _('The maximum number of jobs that will run simultaneously in ' 'the background. This refers to CPU intensive tasks like ' ' conversion. Lower this number' ' if you want calibre to use less CPU.'))) self.device_detection_button.clicked.connect( self.debug_device_detection) self.icon_theme_button.clicked.connect(self.create_icon_theme) self.button_open_config_dir.clicked.connect(self.open_config_dir) self.user_defined_device_button.clicked.connect( self.user_defined_device) proxies = get_proxies(debug=False) txt = _('No proxies used') if proxies: lines = [ '<br><code>%s: %s</code>' % (t, p) for t, p in proxies.iteritems() ] txt = _('<b>Using proxies:</b>') + ''.join(lines) self.proxies.setText(txt)
def get_https_resource_securely( url, cacerts='calibre-ebook-root-CA.crt', timeout=60, max_redirects=5, ssl_version=None, headers=None, get_response=False): ''' Download the resource pointed to by url using https securely (verify server certificate). Ensures that redirects, if any, are also downloaded securely. Needs a CA certificates bundle (in PEM format) to verify the server's certificates. You can pass cacerts=None to download using SSL but without verifying the server certificate. ''' if ssl_version is None: try: ssl_version = ssl.PROTOCOL_TLSv1_2 except AttributeError: ssl_version = ssl.PROTOCOL_TLSv1 # old python if cacerts is not None: cacerts = P(cacerts, allow_user_override=False) p = urlparse(url) if p.scheme != 'https': raise ValueError('URL %s scheme must be https, not %r' % (url, p.scheme)) hostname, port = p.hostname, p.port proxies = get_proxies() has_proxy = False for q in ('https', 'http'): if q in proxies: try: h, po = proxies[q].rpartition(':')[::2] po = int(po) if h: hostname, port, has_proxy = h, po, True break except Exception: # Invalid proxy, ignore pass c = HTTPSConnection(ssl_version, hostname, port, cert_file=cacerts, timeout=timeout) if has_proxy: c.set_tunnel(p.hostname, p.port) with closing(c): c.connect() # This is needed for proxy connections path = p.path or '/' if p.query: path += '?' + p.query c.request('GET', path, headers=headers or {}) response = c.getresponse() if response.status in (httplib.MOVED_PERMANENTLY, httplib.FOUND, httplib.SEE_OTHER): if max_redirects <= 0: raise ValueError('Too many redirects, giving up') newurl = response.getheader('Location', None) if newurl is None: raise ValueError('%s returned a redirect response with no Location header' % url) return get_https_resource_securely( newurl, cacerts=cacerts, timeout=timeout, max_redirects=max_redirects-1, ssl_version=ssl_version, get_response=get_response) if response.status != httplib.OK: raise HTTPError(url, response.status) if get_response: return response return response.read()
def __init__(self, log): QNetworkProxyFactory.__init__(self) proxies = get_proxies() self.proxies = {} for scheme, proxy_string in proxies.iteritems(): scheme = scheme.lower() info = get_proxy_info(scheme, proxy_string) if info is None: continue hn, port = info['hostname'], info['port'] if not hn or not port: continue log.debug('JSBrowser using proxy:', pprint.pformat(info)) pt = { 'socks5': QNetworkProxy.Socks5Proxy }.get(scheme, QNetworkProxy.HttpProxy) proxy = QNetworkProxy(pt, hn, port) un, pw = info['username'], info['password'] if un: proxy.setUser(un) if pw: proxy.setPassword(pw) self.proxies[scheme] = proxy self.default_proxy = QNetworkProxy(QNetworkProxy.DefaultProxy)
def _initialize_books(self): self._proxy = False self._http_address = None self._http_port = None http_proxy = get_proxies(debug=False).get('http', None) if http_proxy: self._proxy = True self._http_address = ':'.join(http_proxy.split(':')[:-1]) self._http_port = int(http_proxy.split(':')[-1]) self._aConnection = HTTPConnection(self._http_address, self._http_port) self._aConnection.set_tunnel('www.amazon.com', 80) self._sConnection = HTTPConnection(self._http_address, self._http_port) self._sConnection.set_tunnel('www.shelfari.com', 80) else: self._aConnection = HTTPConnection('www.amazon.com') self._sConnection = HTTPConnection('www.shelfari.com') self._books = [] for book_id in self._book_ids: self._books.append(Book(self._db, book_id, self._aConnection, self._sConnection, formats=self._formats, spoilers=self._spoilers, send_to_device=self._send_to_device, create_xray=self._create_xray, proxy=self._proxy, http_address=self._http_address, http_port=self._http_port)) self._total_not_failing = sum([1 for book in self._books if book.status is not book.FAIL])
def __init__(self, parent, site_customization): InterfaceAction.__init__(self, parent, site_customization) https_proxy = get_proxies(debug=False).get('https', None) if https_proxy: https_address = ':'.join(https_proxy.split(':')[:-1]) https_port = int(https_proxy.split(':')[-1]) goodreads_conn = HTTPSConnection(https_address, https_port) goodreads_conn.set_tunnel('www.goodreads.com', 443) amazon_conn = HTTPSConnection(https_address, https_port) amazon_conn.set_tunnel('www.amazon.com', 443) else: goodreads_conn = HTTPSConnection('www.goodreads.com') amazon_conn = HTTPSConnection('www.amazon.com') self._connections = {'goodreads': goodreads_conn, 'amazon': amazon_conn} self.menu = QMenu(self.gui)
def genesis(self, gui): self.gui = gui r = self.register r('worker_limit', config, restart_required=True, setting=WorkersSetting) r('enforce_cpu_limit', config, restart_required=True) r('worker_max_time', gprefs) self.opt_worker_limit.setToolTip(textwrap.fill( _('The maximum number of jobs that will run simultaneously in ' 'the background. This refers to CPU intensive tasks like ' ' conversion. Lower this number' ' if you want calibre to use less CPU.'))) self.device_detection_button.clicked.connect(self.debug_device_detection) self.button_open_config_dir.clicked.connect(self.open_config_dir) self.user_defined_device_button.clicked.connect(self.user_defined_device) proxies = get_proxies(debug=False) txt = _('No proxies used') if proxies: lines = ['<br><code>%s: %s</code>'%(t, p) for t, p in proxies.iteritems()] txt = _('<b>Using proxies:</b>') + ''.join(lines) self.proxies.setText(txt)
def __init__(self, log): QNetworkProxyFactory.__init__(self) proxies = get_proxies() self.proxies = {} for scheme, proxy_string in proxies.iteritems(): scheme = scheme.lower() info = get_proxy_info(scheme, proxy_string) if info is None: continue hn, port = info['hostname'], info['port'] if not hn or not port: continue log.debug('JSBrowser using proxy:', pprint.pformat(info)) pt = {'socks5':QNetworkProxy.Socks5Proxy}.get(scheme, QNetworkProxy.HttpProxy) proxy = QNetworkProxy(pt, hn, port) un, pw = info['username'], info['password'] if un: proxy.setUser(un) if pw: proxy.setPassword(pw) self.proxies[scheme] = proxy self.default_proxy = QNetworkProxy(QNetworkProxy.DefaultProxy)
def do_mounts(self,session): session.mount('https://', HTTPAdapter(max_retries=self.retries)) session.mount('http://', HTTPAdapter(max_retries=self.retries)) session.mount('file://', FileAdapter()) # logger.debug("Session Proxies Before:%s"%session.proxies) ## try to get OS proxy settings via Calibre try: # logger.debug("Attempting to collect proxy settings through Calibre") from calibre import get_proxies try: proxies = get_proxies() if proxies: logger.debug("Calibre Proxies:%s"%proxies) session.proxies.update(proxies) except Exception as e: logger.error("Failed during proxy collect/set %s"%e) except: pass if self.getConfig('http_proxy'): session.proxies['http'] = self.getConfig('http_proxy') if self.getConfig('https_proxy'): session.proxies['https'] = self.getConfig('https_proxy') if session.proxies: logger.debug("Session Proxies After INI:%s"%session.proxies)
def __init__(self, db, ids, parent): QDialog.__init__(self, parent) self.resize(500,500) self._index = 0 self._book_settings = [] http_proxy = get_proxies(debug=False).get('http', None) if http_proxy: self._proxy = True self._http_address = ':'.join(http_proxy.split(':')[:-1]) self._http_port = int(http_proxy.split(':')[-1]) aConnection = HTTPConnection(self._http_address, self._http_port) aConnection.set_tunnel('www.amazon.com', 80) sConnection = HTTPConnection(self._http_address, self._http_port) sConnection.set_tunnel('www.shelfari.com', 80) else: aConnection = HTTPConnection('www.amazon.com') sConnection = HTTPConnection('www.shelfari.com') for book_id in ids: self._book_settings.append(BookSettings(db, book_id, aConnection, sConnection)) self.v_layout = QVBoxLayout(self) self.setWindowTitle('title - author') # add asin and shelfari url text boxes self.asin_layout = QHBoxLayout(None) self.asin_label = QLabel('ASIN:') self.asin_label.setFixedWidth(75) self.asin_edit = QLineEdit('') self.asin_edit.textEdited.connect(self.edit_asin) self.asin_layout.addWidget(self.asin_label) self.asin_layout.addWidget(self.asin_edit) self.v_layout.addLayout(self.asin_layout) self.shelfari_layout = QHBoxLayout(None) self.shelfari_url = QLabel('Shelfari URL:') self.shelfari_url.setFixedWidth(75) self.shelfari_url_edit = QLineEdit('') self.shelfari_url_edit.textEdited.connect(self.edit_shelfari_url) self.shelfari_layout.addWidget(self.shelfari_url) self.shelfari_layout.addWidget(self.shelfari_url_edit) self.v_layout.addLayout(self.shelfari_layout) self.update_buttons_layout = QHBoxLayout(None) self.update_asin_button = QPushButton('Search for ASIN') self.update_asin_button.setFixedWidth(150) self.update_asin_button.clicked.connect(self.search_for_asin) self.update_buttons_layout.addWidget(self.update_asin_button) self.update_shelfari_url_button = QPushButton('Search for Shelfari URL') self.update_shelfari_url_button.setFixedWidth(150) self.update_shelfari_url_button.clicked.connect(self.search_for_shelfari_url) self.update_buttons_layout.addWidget(self.update_shelfari_url_button) self.update_aliases_button = QPushButton('Update Aliases from URL') self.update_aliases_button.setFixedWidth(150) self.update_aliases_button.clicked.connect(self.update_aliases) self.update_buttons_layout.addWidget(self.update_aliases_button) self.v_layout.addLayout(self.update_buttons_layout) # add scrollable area for aliases self.aliases_label = QLabel('Aliases:') self.v_layout.addWidget(self.aliases_label) self.scroll_area = QScrollArea() self.v_layout.addWidget(self.scroll_area) # add status box self.status = QLabel('') self.v_layout.addWidget(self.status) # add previous, ok, cancel, and next buttons self.buttons_layout = QHBoxLayout(None) self.buttons_layout.setAlignment(Qt.AlignRight) if len(ids) > 1: self.previous_button = QPushButton("Previous") self.previous_button.setEnabled(False) self.previous_button.setFixedWidth(100) self.previous_button.clicked.connect(self.previous) self.buttons_layout.addWidget(self.previous_button) self.OK_button = QPushButton("OK") self.OK_button.setFixedWidth(100) self.OK_button.clicked.connect(self.ok) self.buttons_layout.addWidget(self.OK_button) self.cancel_button = QPushButton("Cancel") self.cancel_button.setFixedWidth(100) self.cancel_button.clicked.connect(self.cancel) self.buttons_layout.addWidget(self.cancel_button) if len(ids) > 1: self.next_button = QPushButton("Next") self.next_button.setFixedWidth(100) self.next_button.clicked.connect(self.next) self.buttons_layout.addWidget(self.next_button) self.v_layout.addLayout(self.buttons_layout) self.setLayout(self.v_layout) self.show_book_prefs() self.show()