def fetch(self, url_or_qurl, timeout=60): fetching_url = QUrl(url_or_qurl) self.current_fetch = { 'timeout': timeout, 'end_time': time.monotonic() + timeout, 'fetching_url': canonicalize_qurl(fetching_url), 'working': True, 'load_started': False } self.load(fetching_url) try: app = QApplication.instance() while self.current_fetch['working'] and time.monotonic( ) < self.current_fetch['end_time']: app.processEvents( QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents) ans = self.current_fetch.get('html') if ans is None: eurl = fetching_url.toString() if self.current_fetch['working']: raise TimeoutError(f'Timed out loading HTML from: {eurl}') raise ValueError(f'Failed to load HTML from: {eurl}') return ans finally: del self.current_fetch
def process_rule(self, rule, ancestor_specificity, maximum_specificities): selector = rule['selector'] sheet_index = rule['sheet_index'] rule_address = rule['rule_address'] or () if selector is not None: try: specificity = [0] + list(parse(selector)[0].specificity()) except (AttributeError, TypeError, SelectorError): specificity = [0, 0, 0, 0] else: # style attribute specificity = [1, 0, 0, 0] specificity.extend((sheet_index, tuple(rule_address))) properties = [] for prop in rule['properties']: important = 1 if prop[-1] == 'important' else 0 p = Property(prop, [ancestor_specificity] + [important] + specificity) properties.append(p) if p.specificity > maximum_specificities.get( p.name, lowest_specificity): maximum_specificities[p.name] = p.specificity rule['properties'] = properties href = rule['href'] if hasattr(href, 'startswith') and href.startswith( f'{FAKE_PROTOCOL}://{FAKE_HOST}'): qurl = QUrl(href) name = qurl.path()[1:] if name: rule['href'] = name
def urls_from_md(md): ans = list(md.urls()) if md.hasText(): # Chromium returns the url as text/plain on drag and drop of image text = md.text() if text and text.lstrip().partition(':')[0] in remote_protocols: u = QUrl(text.strip()) if u.isValid(): ans.append(u) return ans
def _copy_links(self, lines): urls = QUrl.fromStringList(lines) cb = QApplication.instance().clipboard() md = QMimeData() md.setText('\n'.join(lines)) md.setUrls(urls) cb.setMimeData(md)
def load_html(path, view, codec='utf-8', mime_type=None, pre_load_callback=lambda x:None, path_is_html=False, force_as_html=False, loading_url=None): from qt.core import QUrl, QByteArray if mime_type is None: mime_type = guess_type(path)[0] if not mime_type: mime_type = 'text/html' if path_is_html: html = path else: with open(path, 'rb') as f: html = f.read().decode(codec, 'replace') html = cleanup_html(html) loading_url = loading_url or QUrl.fromLocalFile(path) pre_load_callback(loading_url) if force_as_html or load_as_html(html): view.setHtml(html, loading_url) else: view.setContent(QByteArray(html.encode(codec)), mime_type, loading_url) mf = view.page().mainFrame() elem = mf.findFirstElement('parsererror') if not elem.isNull(): return False return True
def main(): must_use_qt() load_builtin_fonts() renderer = Renderer() renderer.setUrl(QUrl.fromLocalFile(sys.argv[-1])) renderer.loadFinished.connect(renderer.do_print) QApplication.instance().exec_() print('Output written to:', OUTPUT)
def convert_html_file(self, path, page_layout, settle_time=0, wait_for_title=None): self.working = True self.load_complete = False self.wait_for_title = wait_for_title self.settle_time = settle_time self.page_layout = page_layout self.setUrl(QUrl.fromLocalFile(path))
def open(self, parent=None, detail_item=None, external=False): url = detail_item or shop_url if external or self.config.get('open_external', False): open_url(QUrl(url_slash_cleaner(url))) else: d = WebStoreDialog(self.gui, shop_url, parent, url) d.setWindowTitle(self.name) d.set_tags(self.config.get('tags', '')) d.exec_()
def test_server(self): prefix = self.advanced_tab.get('url_prefix') or '' protocol = 'https' if self.advanced_tab.has_ssl else 'http' lo = self.advanced_tab.get('listen_on') or '0.0.0.0' lo = {'0.0.0.0': '127.0.0.1', '::':'::1'}.get(lo) url = '{protocol}://{interface}:{port}{prefix}'.format( protocol=protocol, interface=lo, port=self.main_tab.opt_port.value(), prefix=prefix) open_url(QUrl(url))
def open(self, parent=None, detail_item=None, external=False): url = 'https://books.google.com/books' if True or external or self.config.get('open_external', False): open_url(QUrl(url_slash_cleaner(detail_item if detail_item else url))) else: d = WebStoreDialog(self.gui, url, parent, detail_item) d.setWindowTitle(self.name) d.set_tags(self.config.get('tags', '')) d.exec_()
def open(self, parent=None, detail_item=None, external=False): url = 'http://www.baenebooks.com/' if external or self.config.get('open_external', False): open_url(QUrl(detail_item or url)) else: d = WebStoreDialog(self.gui, url, parent, detail_item or url) d.setWindowTitle(self.name) d.set_tags(self.config.get('tags', '')) d.exec()
def main(url): # This function is run in a separate process and can do anything it likes, # including use QWebEngine. Here it simply opens the passed in URL # in a QWebEngineView app = Application([]) w = QWebEngineView() w.setUrl(QUrl(url)) w.show() w.raise_() app.exec_()
def fetch_url(self, url_or_qurl, source='', timeout=60): w = self.worker_for_source(source) if isinstance(url_or_qurl, str): url_or_qurl = QUrl(url_or_qurl) w.stdin.write(b'FETCH:') w.stdin.write( json.dumps({ 'url': bytes(url_or_qurl.toEncoded()).decode('utf-8'), 'timeout': timeout }).encode('utf-8')) w.stdin.write(b'\n') w.stdin.flush() output = json.loads(w.stdout.readline()) if not output['ok']: raise ValueError(output['err']) with open(output['html_file'], 'rb') as f: html = f.read().decode('utf-8') retry_on_fail(os.remove, output['html_file']) return html
def open(self, parent=None, detail_item=None, external=False): url = 'https://www.bubok.es/tienda' if external or self.config.get('open_external', False): open_url( QUrl(url_slash_cleaner(detail_item if detail_item else url))) else: d = WebStoreDialog(self.gui, url, parent, detail_item) d.setWindowTitle(self.name) d.set_tags(self.config.get('tags', '')) d.exec()
def __init__(self): profile = QWebEngineProfile(QApplication.instance()) profile.setHttpUserAgent('calibre-tester') insert_scripts(profile, create_script('test-rapydscript.js', js, on_subframes=False)) url_handler = UrlSchemeHandler(profile) profile.installUrlSchemeHandler(QByteArray(FAKE_PROTOCOL.encode('ascii')), url_handler) QWebEnginePage.__init__(self, profile, None) self.titleChanged.connect(self.title_changed) secure_webengine(self) self.setHtml('<p>initialize', QUrl(f'{FAKE_PROTOCOL}://{FAKE_HOST}/index.html')) self.working = True
def update_query(self): self.debounce_timer.stop() query = self.selected_text or self.current_query if self.query_is_up_to_date: return if not self.is_visible or not query: return self.current_source = self.url_template url = self.current_source.format(word=query) self.view.load(QUrl(url)) self.current_query = query self.update_refresh_button_status()
def fetch_url(self): Dialog.current_url = self.url_field.text() self.reply = self.network_access.get( QNetworkRequest(QUrl(Dialog.current_url))) def load_in_progress(current, maximum): self.fetch_in_progress.setMaximum(maximum) self.fetch_in_progress.setValue(current) load_in_progress(0, 1) self.reply.downloadProgress.connect( lambda received, total: load_in_progress(received, total)) self.reply.finished.connect(self.fetch_ok) self.reply.error.connect(self.fetch_error)
def view_image(self, name): path = get_path_for_name(name) if path: pmap = QPixmap() if pmap.load(path): self.image_popup.current_img = pmap self.image_popup.current_url = QUrl.fromLocalFile(path) self.image_popup() else: error_dialog(self, _('Invalid image'), _( "Failed to load the image {}").format(name), show=True) else: error_dialog(self, _('Image not found'), _( "Failed to find the image {}").format(name), show=True)
def open(self, parent=None, detail_item=None, external=False): if not hasattr(self, 'web_url'): return if external or self.config.get('open_external', False): open_url(QUrl(detail_item if detail_item else self.web_url)) else: d = WebStoreDialog(self.gui, self.web_url, parent, detail_item, create_browser=self.create_browser) d.setWindowTitle(self.name) d.set_tags(self.config.get('tags', '')) d.exec()
def copy_image(self, name): path = get_path_for_name(name) if not path: return error_dialog(self, _('Image not found'), _( "Failed to find the image {}").format(name), show=True) try: img = image_from_path(path) except Exception: return error_dialog(self, _('Invalid image'), _( "Failed to load the image {}").format(name), show=True) url = QUrl.fromLocalFile(path) md = QMimeData() md.setImageData(img) md.setUrls([url]) QApplication.instance().clipboard().setMimeData(md)
def open(self, parent=None, detail_item=None, external=False): url = 'https://www.beam-shop.de/' if external or self.config.get('open_external', False): if detail_item: url = detail_item open_url(QUrl(url)) else: detail_url = None if detail_item: detail_url = detail_item d = WebStoreDialog(self.gui, url, parent, detail_url) d.setWindowTitle(self.name) d.set_tags(self.config.get('tags', '')) d.exec()
def open(self, parent=None, detail_item=None, external=False): url = 'http://chitanka.info' if external or self.config.get('open_external', False): if detail_item: url = url + detail_item open_url(QUrl(url_slash_cleaner(url))) else: detail_url = None if detail_item: detail_url = url + detail_item d = WebStoreDialog(self.gui, url, parent, detail_url) d.setWindowTitle(self.name) d.set_tags(self.config.get('tags', '')) d.exec()
def open(self, parent=None, detail_item=None, external=False): url = 'https://www.millsandboon.co.uk' if external or self.config.get('open_external', False): if detail_item: url = detail_item open_url(QUrl(url_slash_cleaner(url))) else: if detail_item: detail_url = detail_item else: detail_url = None d = WebStoreDialog(self.gui, url, parent, detail_url) d.setWindowTitle(self.name) d.set_tags(self.config.get('tags', '')) d.exec_()
def open(self, parent=None, detail_item=None, external=False): url_details = 'http://www.awin1.com/cread.php?awinmid=1414&awinaffid=120917&clickref=&p={0}' url = 'http://www.awin1.com/awclick.php?mid=2666&id=120917' if external or self.config.get('open_external', False): if detail_item: url = url_details.format(detail_item) open_url(QUrl(url)) else: detail_url = None if detail_item: detail_url = url_details.format(detail_item) d = WebStoreDialog(self.gui, url, parent, detail_url) d.setWindowTitle(self.name) d.set_tags(self.config.get('tags', '')) d.exec()
def open(self, parent=None, detail_item=None, external=False): url = 'http://www.ebook.nl/' url_details = ('http://www.ebook.nl/store/{0}') if external or self.config.get('open_external', False): if detail_item: url = url_details.format(detail_item) open_url(QUrl(url)) else: detail_url = None if detail_item: detail_url = url_details.format(detail_item) d = WebStoreDialog(self.gui, url, parent, detail_url) d.setWindowTitle(self.name) d.set_tags(self.config.get('tags', '')) d.exec()
def open(self, parent=None, detail_item=None, external=False): url = 'https://wolnelektury.pl' detail_url = None if detail_item: detail_url = detail_item if external or self.config.get('open_external', False): open_url(QUrl( url_slash_cleaner(detail_url if detail_url else url))) else: d = WebStoreDialog(self.gui, url, parent, detail_url) d.setWindowTitle(self.name) d.set_tags(self.config.get('tags', '')) d.exec()
def open(self, parent=None, detail_item=None, external=False): url = 'https://www.mobileread.com/' if external or self.config.get('open_external', False): open_url(QUrl(detail_item if detail_item else url)) else: if detail_item: d = WebStoreDialog(self.gui, url, parent, detail_item) d.setWindowTitle(self.name) d.set_tags(self.config.get('tags', '')) d.exec_() else: self.update_cache(parent, 30) d = MobileReadStoreDialog(self, parent) d.setWindowTitle(self.name) d.exec_()
def open(self, parent=None, detail_item=None, external=False): url = 'https://www.whsmith.co.uk/' url_details = '' if external or self.config.get('open_external', False): if detail_item: url = url_details + detail_item open_url(QUrl(url)) else: detail_url = None if detail_item: detail_url = url_details + detail_item d = WebStoreDialog(self.gui, url, parent, detail_url) d.setWindowTitle(self.name) d.set_tags(self.config.get('tags', '')) d.exec_()
def parse_link(self, link): link = link.strip() if link and os.path.exists(link): return QUrl.fromLocalFile(link) has_schema = re.match(r'^[a-zA-Z]+:', link) if has_schema is not None: url = QUrl(link, QUrl.ParsingMode.TolerantMode) if url.isValid(): return url if os.path.exists(link): return QUrl.fromLocalFile(link) if has_schema is None: first, _, rest = link.partition('.') prefix = 'http' if first == 'ftp': prefix = 'ftp' url = QUrl(prefix +'://'+link, QUrl.ParsingMode.TolerantMode) if url.isValid(): return url return QUrl(link, QUrl.ParsingMode.TolerantMode)
def open(self, parent=None, detail_item=None, external=False): aff_root = 'https://www.a4b-tracking.com/pl/stat-click-text-link/29/58/' url = 'http://www.publio.pl/' aff_url = aff_root + as_base64(url) detail_url = None if detail_item: detail_url = aff_root + as_base64(detail_item) if external or self.config.get('open_external', False): open_url(QUrl(url_slash_cleaner(detail_url if detail_url else aff_url))) else: d = WebStoreDialog(self.gui, url, parent, detail_url if detail_url else aff_url) d.setWindowTitle(self.name) d.set_tags(self.config.get('tags', '')) d.exec()