class Browser(QWidget): def __init__(self): super().__init__() self._web_view = QWebEngineView() self._search_bar = QWidget() self._back_button = QPushButton(text="back") self._back_button.clicked.connect(self._back) self._load_button = QPushButton(text="load") self._load_button.clicked.connect(self._load) self._search_text_field = QLineEdit('enter search or address') self._search_text_field.returnPressed.connect(self._load) self._setup_search_bar() self._setup_web_view() self._setup_self() def _setup_search_bar(self): layout = QHBoxLayout() layout.addWidget(self._back_button) layout.addWidget(self._load_button) layout.addWidget(self._search_text_field) layout.setSpacing(5) layout.setContentsMargins(10, 10, 10, 10) self._search_bar.setLayout(layout) def _setup_web_view(self): self._web_view.urlChanged.connect(self._update_search_text_field) def _setup_self(self): layout = QVBoxLayout() layout.addWidget(self._web_view) layout.addWidget(self._search_bar) layout.setSpacing(0) layout.setContentsMargins(0, 0, 0, 0) self.setLayout(layout) def _load(self): self._web_view.load(QUrl(self._search_text_field.text())) if not self._web_view.url().url( ): # check if web view recognized the url as valid self._web_view.load( QUrl('https://www.google.com/search?q=' + self._search_text_field.text())) def _back(self): self._web_view.back() def _update_search_text_field(self): self._search_text_field.setText(self._web_view.url().url())
class X0h(XrayServerWidget): name = "X0h" description = "X0h" icon = "icons/x0h.png" maintainer = "Luca Rebuffi" maintainer_email = "luca.rebuffi(@at@)elettra.eu" priority = 1 category = "X0h" keywords = ["data", "file", "load", "read"] want_main_area = 1 outputs = [ { "name": "xrayserver_data", "type": DataExchangeObject, "doc": "xrayserver_data", "id": "xrayserver_data" }, ] def __init__(self): super().__init__() left_box_1 = oasysgui.widgetBox(self.controlArea, "X0h Request Form", addSpace=True, orientation="vertical", width=610, height=640) html = self.clear_input_form( HttpManager.send_xray_server_direct_request( "/cgi/www_form.exe?template=X0h_form.htm")) self.x0h_input = QWebView(left_box_1) self.x0h_input.setHtml(html) left_box_1.layout().addWidget(self.x0h_input) self.x0h_input.setFixedHeight(540) self.x0h_input.setFixedWidth(590) button = gui.button(self.controlArea, self, "Get X0h!", callback=self.submit) button.setFixedHeight(30) gui.rubber(self.controlArea) self.tabs = [] self.tabs_widget = oasysgui.tabWidget(self.mainArea) self.initializeTabs() self.x0h_output = QWebView(self.tabs[0]) self.tabs[0].layout().addWidget(self.x0h_output) self.x0h_output.setFixedHeight(630) self.x0h_output.setFixedWidth(740) def clear_input_form(self, html): temp_1 = html.split("<body onload=\"setOnloads()\">")[0] temp_2 = html.split( "<table cellspacing=0 cellpadding=0 border=0 bgcolor=\"#c1c1c1\"><tr><td>" )[1] html = temp_1 + "\n<table cellspacing=0 cellpadding=0 border=0 bgcolor=\"#c1c1c1\"><tr><td>\n" + temp_2 html = html.split( "<input type=SUBMIT value=\"Get X0h!\"><input type=RESET> <br>")[0] html += "\n<input type=SUBMIT style=\"display: none;\" id=\"submit-btn\"><input type=RESET style=\"display: none;\" id=\"reset-btn\"> <br>" html += "\n</form>" html += "\n</td></tr></table>" html += "\n</td></tr></table>" html += "\n</body>" html += "\n</html>" return html def getLeftPartWidth(self): return 620 def initializeTabs(self): current_tab = self.tabs_widget.currentIndex() size = len(self.tabs) for index in range(0, size): self.tabs_widget.removeTab(size - 1 - index) self.tabs = [ gui.createTabPage(self.tabs_widget, "X-ray Server Ouput"), gui.createTabPage(self.tabs_widget, "Critical Angle for TER"), gui.createTabPage(self.tabs_widget, "Darwin Curve (" + u"\u03C3" + " Pol.)"), gui.createTabPage(self.tabs_widget, "Darwin Curve (" + u"\u03C0" + " Pol.)"), ] for tab in self.tabs: tab.setFixedHeight(650) tab.setFixedWidth(750) self.plot_canvas = [None, None, None] self.tabs_widget.setCurrentIndex(current_tab) def js_callback(self, result): pass def _callable_1(self, html): self.original_signal = self.x0h_input.loadFinished self.x0h_input.loadFinished.connect(self.loadFinished) self.x0h_input.setHidden(True) self.x0h_input.page().runJavaScript( "document.getElementById('submit-btn').click()", self.js_callback) def loadFinished(self): self.x0h_input.page().toHtml(self._callable_2) def _callable_2(self, html): try: self.x0h_input.loadFinished.disconnect() self.x0h_input.back() self.x0h_input.setHidden(False) response_1 = self.clear_response(html) response_2 = self.clear_response(html) self.tabs_widget.setCurrentIndex(0) self.x0h_output.setHtml(response_1) data0, data1, data2 = self.extract_plots(response_2) exchange_data = DataExchangeObject("XRAYSERVER", "X0H") exchange_data.add_content("reflectivity", data0) exchange_data.add_content("reflectivity_units_to_degrees", 1.0) exchange_data.add_content("x-ray_diffraction_profile_sigma", data1) exchange_data.add_content( "x-ray_diffraction_profile_sigma_units_to_degrees", 0.000277777805) exchange_data.add_content("x-ray_diffraction_profile_pi", data2) exchange_data.add_content( "x-ray_diffraction_profile_pi_units_to_degrees", 0.000277777805) self.send("xrayserver_data", exchange_data) except urllib.error.HTTPError as e: self.x0h_output.setHtml( 'The server couldn\'t fulfill the request.\nError Code: ' + str(e.code) + "\n\n" + server.BaseHTTPRequestHandler.responses[e.code][1]) except urllib.error.URLError as e: self.x0h_output.setHtml('We failed to reach a server.\nReason: ' + e.reason) except XrayServerException as e: ShowHtmlDialog.show_html("X-ray Server Error", e.response, width=750, height=500, parent=self) except Exception as e: self.x0h_output.setHtml('We failed to reach a server.\nReason: ' + str(e)) self.tabs_widget.setCurrentIndex(0) self.setStatusMessage("") self.progressBarFinished() def submit(self): self.progressBarInit() self.setStatusMessage("Submitting Request") if platform.system() == 'Darwin': self.x0h_input.page().toHtml(self._callable_1) elif platform.system() == 'Linux': doc = self.x0h_input.page().mainFrame().documentElement() submit_btn = doc.findFirst("input[id=submit-btn]") try: response = "" #response = HttpManager.send_xray_server_request_POST(APPLICATION, parameters) response = self.clear_response(response) self.tabs_widget.setCurrentIndex(0) self.x0h_output.setHtml(response) data0, data1, data2 = self.extract_plots(response) exchange_data = DataExchangeObject("XRAYSERVER", "X0H") exchange_data.add_content("reflectivity", data0) exchange_data.add_content("reflectivity_units_to_degrees", 1.0) exchange_data.add_content("x-ray_diffraction_profile_sigma", data1) exchange_data.add_content( "x-ray_diffraction_profile_sigma_units_to_degrees", 0.000277777805) exchange_data.add_content("x-ray_diffraction_profile_pi", data2) exchange_data.add_content( "x-ray_diffraction_profile_pi_units_to_degrees", 0.000277777805) self.send("xrayserver_data", exchange_data) pass except urllib.error.HTTPError as e: self.x0h_output.setHtml( 'The server couldn\'t fulfill the request.\nError Code: ' + str(e.code) + "\n\n" + server.BaseHTTPRequestHandler.responses[e.code][1]) raise e except urllib.error.URLError as e: self.x0h_output.setHtml( 'We failed to reach a server.\nReason: ' + e.reason) raise e except XrayServerException as e: ShowHtmlDialog.show_html("X-ray Server Error", e.response, width=750, height=500, parent=self) raise e except Exception as e: self.x0h_output.setHtml( 'We failed to reach a server.\nReason: ' + str(e)) raise e self.setStatusMessage("") self.progressBarFinished() def clear_response(self, response): # remove links output = response.split( "<img src=\"images/x.gif\" width=\"31\" height=\"32\" border=\"0\">" )[0] + "\n</body></html>" # remove "get the curve" images output = "".join( output.split( "<input type=image src=\"images/get_the_curve.gif\" border=0 width=102 height=12 alt=\"Get the reflectivity curve\">" )) output = "".join( output.split( "<input type=image src=\"images/get_the_curve.gif\" border=0 width=102 height=12 alt=\"Get the Bragg curve (sigma)\">" )) output = "".join( output.split( "<input type=image src=\"images/get_the_curve.gif\" border=0 width=102 height=12 alt=\"Get the Bragg curve (pi)\">" )) # remove question mark images and links output = "".join( output.split( "<a href=\"javascript:void(0)\" onClick=\"Wfloat(\'images/x0h_help_0.gif\',\'x0h_0\',740,357);\"><b>?</b></a> " )) output = "".join( output.split( "<a href=\"javascript:void(0)\" onClick=\"Wfloat(\'images/x0h_help_h.gif\',\'x0h_h\',705,853);\"><b>?</b></a> " )) return output def extract_plots(self, response): form_1_begin = False form_2_begin = False form_3_begin = False form_1 = None form_2 = None form_3 = None rows = response.split("\n") for row in rows: if form_1_begin: if "<td>" in row: form_1_begin = False elif form_2_begin: if "<td>" in row: form_2_begin = False elif form_3_begin: if "<td>" in row: form_3_begin = False if form_1_begin: form_1.append(row) elif form_2_begin: form_2.append(row) elif form_3_begin: form_3.append(row) if "/cgi/ter_form.pl" in row: if form_1 is None: form_1 = [] form_1_begin = True if "/cgi/gid_form.pl" in row: if form_2 is None: form_2 = [] form_2_begin = True elif form_3 is None: form_3 = [] form_3_begin = True self.setStatusMessage("Plotting Results") if not form_1 is None: x_1, y_1 = self.get_plots_from_form("/cgi/ter_form.pl", form_1) self.plot_histo(x_1, y_1, 40, 1, 0, "Critical Angle for TER", "Incidence angle [degrees]", "Reflectivity") self.tabs_widget.setCurrentIndex(1) else: x_1 = None y_1 = None if not form_2 is None: x_2, y_2 = self.get_plots_from_form("/cgi/gid_form.pl", form_2) self.plot_histo(x_2, y_2, 60, 2, 1, "Darwin Curve ($\sigma$ Pol.)", "Scan Angle [arcsec]", "Diffracted Intensity") self.tabs_widget.setCurrentIndex(2) else: x_2 = None y_2 = None if not form_3 is None: x_3, y_3 = self.get_plots_from_form("/cgi/gid_form.pl", form_3) self.plot_histo(x_3, y_3, 80, 3, 2, "Darwin Curve ($\pi$ Pol.)", "Scan Angle [arcsec]", "Diffracted Intensity") self.tabs_widget.setCurrentIndex(3) else: x_3 = None y_3 = None return [x_1, y_1], [x_2, y_2], [x_3, y_3]
class Hrobot(QObject): asyncFinished = pyqtSignal() def __init__(self, base_url=None, private=True, profile_name=None, gui=False): super().__init__() self.base_url = QUrl(base_url) self.__interceptor = Interceptor(self) if private: self.__profile = QWebEngineProfile() elif profile_name: self.__profile = QWebEngineProfile(profile_name) else: raise Exception( 'please pass `profile_name` in case of `private` flag set to False' ) self.__profile.setRequestInterceptor(self.__interceptor) self.__page = Page(self.__profile, None) self.__cookie_store = self.__profile.cookieStore() self.__cookie_store.cookieAdded.connect(self.__on_cookie_added) self.__cookies = [] self.__async_result = None self.__loading = False self.__view = QWebEngineView() self.__page.setView(self.__view) self._qt_invocation = QtInvocation(self.__page) self.__channel = QWebChannel(self) self.__channel.registerObject("QtInvocation", self._qt_invocation) self.__page.setWebChannel(self.__channel) self.__page.loadStarted.connect(self.__load_started) self.__page.loadFinished.connect(self.__load_finished) self.__view.loadProgress.connect(self.__prg) self.__page.navigationRequested.connect(self.__navigation_requested) # self._qt_invocation.js_navigation_requested.connect(self.__page._js_gap_eventloop) self.__page.settings().setAttribute( self.__page.settings().LocalContentCanAccessRemoteUrls, True) self.__page.setUrl(QUrl('about:blank')) self.__view.setWindowState(Qt.WindowMaximized) if gui: self.__view.show() def __prg(self, p): print('progress: ', p) def __load_started(self): print('load started') def __load_finished(self, ok): print('load finished', ok) def __node(self, node_id): return Node(self, self.__page, node_id) def __navigation_requested(self): print('navvvvvvvvvvvvv', self.__loading) if not self.__loading: self.__loading = True print('navigation event loop started') loop = QEventLoop() self.__page.loadFinished.connect(loop.quit) loop.exec() print('navigation event loop ended') loop_js = QEventLoop() self._qt_invocation.js_ready.connect(loop_js.quit) self.__register_js() loop_js.exec() self.__loading = False print('js register event loop ended') def __register_js(self): channel_file = QFile(":/qtwebchannel/qwebchannel.js") if channel_file.open(QIODevice.ReadOnly): content = channel_file.readAll() channel_file.close() print('runjssssssssssssssssssssssssssssssssssssssss 1') self.__run_js(content.data().decode()) print('runjssssssssssssssssssssssssssssssssssssssss 2', BASE_APP_PATH) qt_file = QFile( '{0}/extensions/hrobot/v_2_0/qt.js'.format(BASE_APP_PATH)) if qt_file.open(QIODevice.ReadOnly): content = qt_file.readAll() qt_file.close() print('runjssssssssssssssssssssssssssssssssssssssss 3') self.__run_js(content.data().decode()) print('runjssssssssssssssssssssssssssssssssssssssss 4') def __async_callback(self, result): self.__async_result = result self.asyncFinished.emit() print('async js emit') def __run_js(self, script): loop = QEventLoop() self.asyncFinished.connect(loop.quit) self.__page.runJavaScript(script, self.__async_callback) loop.exec() print('run js finished') return self.__async_result 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 set_cookie(self, cookie): self.__cookie_store.setCookie(cookie) def __set_browser_attribute(self, attr, value): self.__page.settings().setAttribute(attr, value) def go_to(self, url: Union[QUrl, str]) -> bool: """ Goes to a given URL. """ url = QUrl(url) if self.base_url: url = self.base_url.resolved(url) loop = QEventLoop() self.__page.loadFinished.connect(loop.exit) self.__page.load(url) return True if loop.exec() else False def go_back(self): self.__view.back() def get_body(self): """ Return the current DOM as HTML. """ loop = QEventLoop() self.asyncFinished.connect(loop.quit) self.__page.toHtml(self.__async_callback) loop.exec() return self.__async_result def get_cookies(self, as_json=False): if as_json: return self.__cookie_to_json() return self.__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 json_to_cookie(self, json_cookie_list): cookies_list_info = [] for json_cookie in json_cookie_list: c = QNetworkCookie() print(json_cookie, '** json_cookie') for k, v in json_cookie.items(): if k == 'path': c.setPath(v) elif k == 'domain': c.setPath(v) elif k == 'expirationDate': print(v, type(v)) qdate = QDateTime(2019, 12, 20, 11, 59, 59) print(qdate) c.setExpirationDate(qdate) elif k == 'httponly': c.setHttpOnly(v) elif k == 'secure': c.setSecure(v) elif k == 'value': c.setValue(v.encode()) elif k == 'name': c.setName(v.encode()) print('c', c.expirationDate()) cookies_list_info.append(c) return cookies_list_info def eval_script(self, script): """ Evaluates a piece of Javascript in the context of the current page and returns its value. """ return self.__run_js(script) def exec_script(self, script): """ Executes a piece of Javascript in the context of the current page. """ self.__run_js(script) def block_url(self, url): raise NotImplementedError def allow_url(self, url): raise NotImplementedError def get_url(self): """ Returns the current location. """ return self.__page.url() def reload(self): self.go_to(self.current_url()) def is_private(self): return self.__page.profile().isOffTheRecord() def set_skip_image_loading(self, value): """ Specifies whether images are automatically loaded in web pages. This is disabled by default.""" # self.browser.set_attribute('auto_load_images', value) self.__set_browser_attribute(self.__page.settings().AutoLoadImages, not value) def save_as_pdf(self, name): loop = QEventLoop() self.__page.pdfPrintingFinished.connect(loop.quit) self.__page.printToPdf(name) loop.exec() def scroll_to_end(self, lazy_load=True): loop = QEventLoop() self._qt_invocation.async_js_finished.connect(loop.quit) self.__run_js( 'Qt.scrollToEnd({0})'.format('true' if lazy_load else 'false')) loop.exec() def find_by_css(self, selector): """ Return the first node matching the given CSSv3 expression or None. """ nodes = self.find_all_by_css(selector) return None if not nodes else nodes[0] def find_by_xpath(self, xpath): """ Return the first node matching the given XPath 2.0 expression or None. """ nodes = self.find_all_by_xpath(xpath) return None if not nodes else nodes[0] def find_all_by_css(self, selector): """ Return all nodes matching the given CSSv3 expression. """ try: return [ self.__node(node_id) for node_id in self.__run_js( Utils.qt_js_prepare('Qt.findCss("{0}")'.format( Utils.normalize_quotes(selector)))).split(",") if node_id ] except AttributeError: return None def find_all_by_xpath(self, xpath): """ Return all nodes matching the given XPath 2.0 expression. """ try: return [ self.__node(node_id) for node_id in self.__run_js( Utils.qt_js_prepare('Qt.findXpath("{0}")'.format( Utils.normalize_quotes(xpath)))).split(",") if node_id ] except AttributeError: return None def wait_until_css(self, selector, timeout=30): node = self.find_by_css(selector) elapsed = 0 while not node: if elapsed > timeout: return None Utils.wait(1000) elapsed += 1 node = self.find_by_css(selector) return node def wait_until_xpath(self, xpath, timeout=30): node = self.find_by_xpath(xpath) elapsed = 0 while not node: if elapsed > timeout: return None Utils.wait(1000) elapsed += 1 node = self.find_by_xpath(xpath) return node def find_by_following_sibling(self, tag, sibling): """ Return first sibling node after the current node """ return self.find_by_xpath("//%s/following-sibling::%s" % (tag, sibling)) def find_all_by_following_sibling(self, tag, sibling): """ Return all sibling nodes after the current node """ return self.find_all_by_xpath("//%s/following-sibling::%s" % (tag, sibling)) def find_by_preceding_sibling(self, tag, sibling): """ Return first sibling node before the current node """ return self.find_by_xpath("//%s/preceding-sibling::%s" % (tag, sibling)) def find_all_by_preceding_sibling(self, tag, sibling): """ Return all sibling nodes before the current node """ return self.find_all_by_xpath("//%s/preceding-sibling::%s" % (tag, sibling)) def find_element_has_attribute(self, tag, attr): """ Return the first node has attr in its attribute or none """ return self.find_by_xpath("//%s[@%s]" % (tag, attr)) def find_elements_has_attribute(self, tag, attr): """ Return all nodes has attr in their attribute or none """ return self.find_all_by_xpath("//%s[@%s]" % (tag, attr)) def find_elements_has_any_attribute(self, tag): """ Return all nodes that has any attribute or none""" return self.find_by_xpath("//%s[@*]" % tag) def find_elements_has_not_attribute(self, tag): """ Return all nodes that has not any attribute or none""" return self.find_by_xpath("//%s[not(@*)]" % tag) def find_by_attribute_value(self, tag, attr, value, normalize_space=True): """ Return first node has attr in its attribute and the value of attr is equal to value.""" if normalize_space: return self.find_by_xpath("//%s[normalize-space(@%s = %s)]" % (tag, attr, value)) else: return self.find_by_xpath("//%s[@%s = %s]" % (tag, attr, value)) def find_all_by_attribute_value(self, tag, attr, value, normalize_space=True): """ Return all nodes has attr in their attribute and the value of attr is equal to value.""" if normalize_space: return self.find_all_by_xpath("//%s[normalize-space(@%s = %s)]" % (tag, attr, value)) else: return self.find_all_by_xpath("//%s[@%s = %s]" % (tag, attr, value)) def find_by_children_number(self, tag, children_number): """ Return first node that number of its children is equal to children_number """ return self.find_all_by_xpath("//*[count(%s)= %s]" % (tag, children_number)) def find_all_by_children_number(self, children_number): """ Return all nodes that number of their children is equal to children_number """ return self.find_all_by_xpath("//*[count(*)= %s]" % children_number) def find_by_name(self, tag_name): """ Return node that name of it is equal to tag_name """ return self.find_all_by_xpath("//*[name()=%s]" % tag_name) def find_by_name_starts_with(self, letter): """ Return node that name of it starts with to letter """ return self.find_all_by_xpath("//*[starts-with(name(), %s)]" % letter) def find_by_name_contains(self, letter): """ Return node that name of it contains letter """ return self.find_all_by_xpath("//*[contains(name(),%s)]" % letter) # this finds excludes any descendants and attribute nodes and namespace nodes. def find_by_following(self, node): """ Return nodes that exist in following of node """ return self.find_all_by_xpath("//%s/following::*" % node) # this finds excludes any descendants and attribute nodes and namespace nodes. def find_by_preceding(self, node): """ Return nodes that exist in preceding of node """ return self.find_all_by_xpath("//%s/preceding::*" % node) def find_element_descendant(self, tag, child_tag='*'): return self.find_by_xpath("%s/descendant::%s" % (tag, child_tag)) def find_elements_descendant(self, tag, child_tag='*'): return self.find_all_by_xpath("%s/descendant::%s" % (tag, child_tag)) def find_all_elements_descendant(self, tag): return self.find_all_by_xpath("//%s/descendant::*" % tag) def find_by_contain_text(self, tag, text): """ Return node that contains text in their text value""" return self.find_by_xpath("//%s[contains(text(), '%s')]" % (tag, text)) def find_all_by_contain_text(self, tag, text): """ Return all nodes that contains text in their text value""" return self.find_all_by_xpath("//%s[contains(text(), '%s')]" % (tag, text))
class Browser(QWidget): def __init__(self): super().__init__() # Create a search url widget self.search_url = QLineEdit() self.search_url.setStyleSheet( "background-color:black; color:white; border-radius:14px; margin:0px,10px,0px,10px; border:2px solid grey; padding: 2px 10px 2px 10px; font-weight:bold;" ) self.search_url.setFont(QtGui.QFont("sans-serif", 11)) self.search_url.setPlaceholderText("Search in Google or Type a URL") self.search_url.setMinimumWidth(500) self.search_url.returnPressed.connect(self.webb) self.search_url.setEnabled(True) self.search_url.setAlignment(Qt.AlignCenter) # Create a vbox vbox = QVBoxLayout() # Back Button self.back_button = QPushButton() self.back_button.setIcon(self.style().standardIcon( QStyle.SP_ArrowBack)) self.back_button.setEnabled(False) self.back_button.clicked.connect(self.go_back) # forword Button self.forword_button = QPushButton() self.forword_button.setIcon(self.style().standardIcon( QStyle.SP_ArrowForward)) self.forword_button.clicked.connect(self.go_fast) # Reload Button self.reload_button = QPushButton() self.reload_button.setIcon(self.style().standardIcon( QStyle.SP_BrowserReload)) self.reload_button.clicked.connect(self.go_reload_buton) # Reload Button self.stop_button = QPushButton() self.stop_button.setIcon(self.style().standardIcon( QStyle.SP_BrowserStop)) self.stop_button.setEnabled(False) self.stop_button.clicked.connect(self.go_stop_button) # Create a Hbox hbox = QHBoxLayout() hbox.addWidget(self.back_button) hbox.addWidget(self.forword_button) hbox.addWidget(self.reload_button) hbox.addWidget(self.stop_button) hbox.addWidget(self.search_url) self.web = QWebEngineView() self.web.load(QUrl(f"https://google.com/")) self.web.show() vbox.addLayout(hbox) vbox.addWidget(self.web) self.setLayout(vbox) self.show() def webb(self): self.stop_button.setEnabled(True) self.back_button.setEnabled(True) if 'http://' or 'https://' not in self.search_url: self.web.load(QUrl(f"https://{self.search_url.text()}/")) self.web.show() else: self.web.load(QUrl(self.search_url.text())) self.web.show() def go_back(self): self.web.back() def go_fast(self): self.web.forward() def go_reload_buton(self): self.web.reload() def go_stop_button(self): self.web.stop()
class App(QMainWindow, Ui_MainWindow): def __init__(self): super().__init__() self.setupUi(self) self.setUpStuff() def setUpStuff(self): self.webView = QWebEngineView() self.webView.load(QUrl('http://youtube.com')) self.webView.setBaseSize(500, 500) self.webView.setVisible(True) self.gridLayout.addWidget(self.webView, 1, 0, 1, 2) self.webView.installEventFilter(self) self.getActualURL() self.downloadButton.clicked.connect(self.youtubeDownload) self.setURL.clicked.connect(self.setURLm) self.getActualURL() def getActualURL(self): print("getactualurl") i = self.webView.url() print(i) i = str(i) i = i[19:] ilen = i.__len__() url = i[:ilen - 2] print(url) self.urlInput.setText(url) @pyqtSlot() def youtubeDownload(self): self.getActualURL() print("ytdownload") try: with youtube_dl.YoutubeDL({}) as ydl: dirname = os.path.split(os.path.abspath(__file__)) path = dirname[0] + "\\Downloads" print("script: __file__ is", path) if not os.path.exists(path): os.makedirs(path) os.chdir(path) ydl.download([str(self.urlInput.displayText())]) except: msgBox = QMessageBox() msgBox.setText("Error:") msgBox.setInformativeText( "An unknown error occurred. Ensure video link is correct") msgBox.setStandardButtons(QMessageBox.Ok) msgBox.setDefaultButton(QMessageBox.Ok) msgBox.exec_() @pyqtSlot() def setURLm(self): print("seturlm") try: shortcut = False url = self.urlInput.displayText() i7 = url[:7] i8 = url[:8] if (i7 == "http://" or i8 == "https://"): pass else: url = "http://" + url print(url) self.webView.load(QUrl(url)) except: pass def eventFilter(self, obj, event): #if(obj == self.webView): self.getActualURL() if event.type() == QtCore.QEvent.MouseButtonPress: print("t") #in (QtCore.QEvent.MouseButtonPress,QtCore.QEvent.MouseButtonDblClick): if event.button() == QtCore.Qt.LeftButton: print("left") if event.button() == QtCore.Qt.ExtraButton1: self.webView.back() if event.button() == QtCore.Qt.ExtraButton2: self.webView.forward() return QtGui.QWidget.eventFilter(self, obj, event)
class Main(QtWidgets.QMainWindow): def __init__(self): QtWidgets.QMainWindow.__init__(self) self.CreateUI() def CreateUI(self): # Create Necessary Widgets self.centralwidget = QtWidgets.QWidget(self) self.line = QtWidgets.QLineEdit(self) self.line.setMinimumSize(500, 20) self.line.setStyleSheet('font-size:15px;') # Enter URL self.enter = QtWidgets.QPushButton(self) self.enter.resize(0,0) self.enter.clicked.connect(self.set_url) self.enter.setShortcut('Return') # Reload Button self.reload = QtWidgets.QPushButton('↻',self) self.reload.setMinimumSize(20, 20) self.reload.setShortcut('F5') self.reload.setStyleSheet('font-size:23px;') self.reload.clicked.connect(self.reload_page) # Back Button self.back = QtWidgets.QPushButton('←',self) self.back.setMinimumSize(20, 20) self.back.setStyleSheet('font-size:23px;') self.back.clicked.connect(self.go_back) # Forward Button self.forward = QtWidgets.QPushButton('→',self) self.forward.setMinimumSize(20, 20) self.forward.setStyleSheet('font-size:23px;') self.forward.clicked.connect(self.go_forwardard) # Create Progress Bar self.pbar = QtWidgets.QProgressBar() self.pbar.setMaximumWidth(120) # Update Progress Bar, Window Title etc. self.web = QWebEngineView(loadProgress = self.pbar.setValue, loadFinished = self.pbar.hide, loadStarted = self.pbar.show, titleChanged = self.setWindowTitle) self.web.setMinimumSize(1200, 600) # Check for url changes self.web.urlChanged.connect(self.if_url_changed) # Check for user liink hovering self.web.page().linkHovered.connect(self.if_link_hover) # Set Grid grid = QtWidgets.QGridLayout() # Set Widget Locations grid.addWidget(self.back,0,0, 1, 1) grid.addWidget(self.line,0,3, 1, 1) grid.addWidget(self.forward,0,1, 1, 1) grid.addWidget(self.reload,0,2, 1, 1) grid.addWidget(self.web, 2, 0, 1, 6) self.centralwidget.setLayout(grid) # Window Settings # Default Window Size and Location self.setGeometry(25, 100, 1200, 600) # Window Title self.setWindowTitle('Browsey') # Window Icon self.setWindowIcon(QtGui.QIcon('')) # Window Colour self.setStyleSheet('background-color:') # Window Status Bar self.status = self.statusBar() self.status.addPermanentWidget(self.pbar) self.status.hide() self.setCentralWidget(self.centralwidget) # Function to Handle URLs def set_url(self): # Get default URL global url url = self.line.text() # Set URL Prefixes http = 'http://' www = 'www.' # Check and Set if www in url and http not in url: url = http + url elif http in url and www not in url: url = url[:7] + www + url[7:] # If no suffix used make it a google search elif '.' not in url: url = 'http://www.google.com/search?q='+url elif http and www not in url: url = http + www + url self.line.setText(url) # Load site at URL self.web.load(QtCore.QUrl(url)) # Show URL self.status.show() # Navigation functions def go_back(self): self.web.back() def go_forwardard(self): self.web.forwardard() def reload_page(self): self.web.reload() #Check if user has entered new URL def if_url_changed(self): self.line.setText(self.web.url().toString()) # Show link in statusbar if user hovers def if_link_hover(self, l): self.status.showMessage(l)
class MainWidget(QWidget): def __init__(self, url): super().__init__() fontDB = QFontDatabase() fontDB.addApplicationFont(':/mono-font') # fontDB.addApplicationFont(':/Taipei-font') screen = QDesktopWidget().screenGeometry() self.width, self.height = screen.width(), screen.height() self.html = '' self.url = url self.GI = None self.DL = None self.web_view = QWebEngineView(self) self.web_view.load(QUrl(self.url)) self.web_view.page().titleChanged.connect(self.get_html) btnExit = QPushButton('', self) btnExit.setIcon(QIcon(':/exit-icon')) btnExit.setIconSize(QSize(32, 32)) btnExit.setToolTip('Exit') btnExit.clicked.connect(QApplication.quit) self.btnHome = QPushButton('', self) self.btnHome.setIcon(QIcon(':/home-icon')) self.btnHome.setIconSize(QSize(32, 32)) self.btnHome.setToolTip('Home') self.btnHome.clicked.connect(self.goHome) self.btnBack = QPushButton('', self) self.btnBack.setIcon(QIcon(':/go_back-icon')) self.btnBack.setIconSize(QSize(32, 32)) self.btnBack.setToolTip('Backward') self.btnBack.clicked.connect(self.goBack) self.btnBack.setDisabled(True) self.btnNext = QPushButton('', self) self.btnNext.setIcon(QIcon(':/go_forward-icon')) self.btnNext.setIconSize(QSize(32, 32)) self.btnNext.setToolTip('Forward') self.btnNext.clicked.connect(self.goForward) self.btnNext.setDisabled(True) self.cmbDownList = QComboBox(self) self.cmbDownList.setMinimumHeight(40) # font = self.cmbDownList.font() font = QFont('Liberation Mono') font.setPointSize(14) self.cmbDownList.setFont(font) self.cmbDownList.setIconSize(QSize(24, 24)) self.cmbDownList.currentIndexChanged.connect(self.onIndexChanged) self.cmbDownList.setToolTip('Select a stream to download') self.cmbCapsList = QComboBox(self) self.cmbCapsList.setMinimumHeight(40) # font = QFont('Taipei Sans TC Beta') font = self.cmbCapsList.font() font.setPointSize(14) self.cmbCapsList.setFont(font) self.cmbCapsList.setToolTip('Select a caption/subtitle to download') btnSettings = QPushButton('', self) btnSettings.setIcon(QIcon(':/settings-icon')) btnSettings.setIconSize(QSize(32, 32)) btnSettings.setToolTip('Settings') btnSettings.clicked.connect(self.onSettings) self.btnDLoad = QPushButton('', self) self.btnDLoad.setIcon(QIcon(':/download-icon')) self.btnDLoad.setIconSize(QSize(32, 32)) self.btnDLoad.setToolTip('Download') self.btnDLoad.clicked.connect(self.goDownload) self.btnDLoad.setDisabled(True) self.progressBar = QProgressBar(self) self.progressBar.setMinimum(0) self.progressBar.setMaximum(100) hBox1 = QHBoxLayout() hBox1.addWidget(btnExit, 0) hBox1.addWidget(self.btnHome, 0) hBox1.addWidget(self.btnBack, 0) hBox1.addWidget(self.btnNext, 0) hBox1.addWidget(self.cmbDownList, 1) hBox1.addWidget(self.cmbCapsList, 0) hBox1.addWidget(btnSettings, 0) hBox1.addWidget(self.btnDLoad, 0) vBox = QVBoxLayout() vBox.addLayout(hBox1) vBox.addWidget(self.web_view) vBox.addWidget(self.progressBar) self.setLayout(vBox) self.setWindowTitle('Youtube Download Helper') self.setGeometry(QRect(round((self.width - 760) / 2), round((self.height - 550) / 2), 760, 550)) self.setMinimumSize(QSize(760, 550)) def store_html(self, html): # print('store_html()') self.html = html if self.web_view.page().action(QWebEnginePage.Back).isEnabled(): self.btnBack.setEnabled(True) else: self.btnBack.setDisabled(True) if self.web_view.page().action(QWebEnginePage.Forward).isEnabled(): self.btnNext.setEnabled(True) else: self.btnNext.setDisabled(True) if self.web_view.title() != 'YouTube' and self.web_view.title() != 'https://www.youtube.com': # print(self.web_view.title()) self.btnHome.setEnabled(True) else: self.btnHome.setDisabled(True) self.cmbDownList.clear() self.cmbCapsList.clear() url = self.web_view.page().url().url() if 'video-id' in self.html and '?v=' in url: # fp = open(self.web_view.title() + '.html', 'w') # fp.write(self.html) # fp.close() self.GI = GetItem(url) self.GI.addItem.connect(self.onAddItem) self.GI.addCaption.connect(self.onAddCaption) self.GI.finished.connect(self.onAddItemFinished) self.GI.start() else: self.btnDLoad.setDisabled(True) def get_html(self): # print('get_html') self.web_view.page().toHtml(self.store_html) def goHome(self): self.web_view.setUrl(QUrl(self.url)) def goBack(self): self.web_view.back() def goForward(self): self.web_view.forward() def goDownload(self): global download_ongoing download_ongoing = True self.btnDLoad.setDisabled(True) self.progressBar.setValue(0) self.DL = DownLoad(self.web_view.page().url().url(), self.cmbDownList.currentIndex(), self.cmbCapsList.currentIndex()) self.DL.valueChanged.connect(self.onValueChanged) self.DL.dlCompleted.connect(self.onDlCompleted) self.DL.start() def onAddItem(self, icon, item): self.cmbDownList.addItem(QIcon(icon), item) def onAddCaption(self, cap): self.cmbCapsList.addItem(cap) def onAddItemFinished(self): if not download_ongoing: self.btnDLoad.setEnabled(True) def onValueChanged(self, per): self.progressBar.setValue(round(per)) # print('%.2f%% completed' % per) def onDlCompleted(self): global download_ongoing download_ongoing = False self.btnDLoad.setDisabled(True) self.progressBar.setValue(0) def onIndexChanged(self): if not download_ongoing: self.btnDLoad.setEnabled(True) def onSettings(self): sWnd = SettingsDlg(self.width, self.height) sWnd.exec() if sWnd.SettingsChanged: # print('Settings saved!') if self.web_view.title() != 'YouTube' and self.web_view.title() != 'https://www.youtube.com': self.web_view.reload()
class X0h(XrayServerWidget): name = "X0h" description = "X0h" icon = "icons/x0h.png" maintainer = "Luca Rebuffi" maintainer_email = "luca.rebuffi(@at@)elettra.eu" priority = 1 category = "X0h" keywords = ["data", "file", "load", "read"] want_main_area = 1 outputs = [{"name": "xrayserver_data", "type": DataExchangeObject, "doc": "xrayserver_data", "id": "xrayserver_data"}, ] def __init__(self): super().__init__() left_box_1 = oasysgui.widgetBox(self.controlArea, "X0h Request Form", addSpace=True, orientation="vertical", width=610, height=640) html = self.clear_input_form(HttpManager.send_xray_server_direct_request("/cgi/www_form.exe?template=x0h_form.htm")) self.x0h_input = QWebView(left_box_1) self.x0h_input.setHtml(html) left_box_1.layout().addWidget(self.x0h_input) self.x0h_input.setFixedHeight(540) self.x0h_input.setFixedWidth(590) button = gui.button(self.controlArea, self, "Get X0h!", callback=self.submit) button.setFixedHeight(30) gui.rubber(self.controlArea) self.tabs = [] self.tabs_widget = oasysgui.tabWidget(self.mainArea) self.initializeTabs() self.x0h_output = QWebView(self.tabs[0]) self.tabs[0].layout().addWidget(self.x0h_output) self.x0h_output.setFixedHeight(630) self.x0h_output.setFixedWidth(740) def clear_input_form(self, html): temp_1 = html.split("<body onload=\"setOnloads()\">")[0] temp_2 = html.split("<table cellspacing=0 cellpadding=0 border=0 bgcolor=\"#c1c1c1\"><tr><td>")[1] html = temp_1 + "<body>\n<table cellspacing=0 cellpadding=0 border=0 bgcolor=\"#c1c1c1\"><tr><td>\n" + temp_2 html = html.split("<input type=SUBMIT value=\"Get X0h!\"><input type=RESET> <br>")[0] html += "\n<input type=SUBMIT style=\"display: none;\" id=\"submit-btn\"><input type=RESET style=\"display: none;\" id=\"reset-btn\"> <br>" html += "\n</form>" html += "\n</td></tr></table>" html += "\n</td></tr></table>" html += "\n</body>" html += "\n</html>" temp_1, temp_2 = html.split("x0h.js") html = temp_1 + XRAY_SERVER_URL + "/x0h.js" + temp_2 temp_1, temp_2 = html.split("/cgi/x0h_form.exe") html = temp_1 + XRAY_SERVER_URL + "/cgi/x0h_form.exe" + temp_2 return html def getLeftPartWidth(self): return 620 def initializeTabs(self): current_tab = self.tabs_widget.currentIndex() size = len(self.tabs) for index in range(0, size): self.tabs_widget.removeTab(size-1-index) self.tabs = [gui.createTabPage(self.tabs_widget, "X-ray Server Ouput"), gui.createTabPage(self.tabs_widget, "Critical Angle for TER"), gui.createTabPage(self.tabs_widget, "Darwin Curve (" + u"\u03C3" + " Pol.)"), gui.createTabPage(self.tabs_widget, "Darwin Curve (" + u"\u03C0" + " Pol.)"), ] for tab in self.tabs: tab.setFixedHeight(650) tab.setFixedWidth(750) self.plot_canvas = [None, None, None] self.tabs_widget.setCurrentIndex(current_tab) def js_callback(self, result): pass def _callable_1(self, html): self.original_signal = self.x0h_input.loadFinished self.x0h_input.loadFinished.connect(self.loadFinished) self.x0h_input.setHidden(True) self.x0h_input.page().runJavaScript("document.getElementById('submit-btn').click()", self.js_callback) def loadFinished(self): self.x0h_input.page().toHtml(self._callable_2) def _callable_2(self, html): try: self.x0h_input.loadFinished.disconnect() self.x0h_input.back() self.x0h_input.setHidden(False) response_1 = self.clear_response(html) response_2 = self.clear_response(html) self.tabs_widget.setCurrentIndex(0) self.x0h_output.setHtml(response_1) data0, data1, data2 = self.extract_plots(response_2) exchange_data = DataExchangeObject("XRAYSERVER", "X0H") exchange_data.add_content("reflectivity", data0) exchange_data.add_content("reflectivity_units_to_degrees", 1.0) exchange_data.add_content("x-ray_diffraction_profile_sigma", data1) exchange_data.add_content("x-ray_diffraction_profile_sigma_units_to_degrees", 0.000277777805) exchange_data.add_content("x-ray_diffraction_profile_pi", data2) exchange_data.add_content("x-ray_diffraction_profile_pi_units_to_degrees", 0.000277777805) self.send("xrayserver_data", exchange_data) except urllib.error.HTTPError as e: self.x0h_output.setHtml('The server couldn\'t fulfill the request.\nError Code: ' + str(e.code) + "\n\n" + server.BaseHTTPRequestHandler.responses[e.code][1]) except urllib.error.URLError as e: self.x0h_output.setHtml('We failed to reach a server.\nReason: ' + e.reason) except XrayServerException as e: ShowHtmlDialog.show_html("X-ray Server Error", e.response, width=750, height=500, parent=self) except Exception as e: self.x0h_output.setHtml('We failed to reach a server.\nReason: ' + str(e)) self.tabs_widget.setCurrentIndex(0) self.setStatusMessage("") self.progressBarFinished() def submit(self): self.progressBarInit() self.setStatusMessage("Submitting Request") if platform.system() == 'Darwin': self.x0h_input.page().toHtml(self._callable_1) elif platform.system() == 'Linux': doc = self.x0h_input.page().mainFrame().documentElement() submit_btn = doc.findFirst("input[id=submit-btn]") try: response = "" #response = HttpManager.send_xray_server_request_POST(APPLICATION, parameters) response = self.clear_response(response) self.tabs_widget.setCurrentIndex(0) self.x0h_output.setHtml(response) data0, data1, data2 = self.extract_plots(response) exchange_data = DataExchangeObject("XRAYSERVER", "X0H") exchange_data.add_content("reflectivity", data0) exchange_data.add_content("reflectivity_units_to_degrees", 1.0) exchange_data.add_content("x-ray_diffraction_profile_sigma", data1) exchange_data.add_content("x-ray_diffraction_profile_sigma_units_to_degrees", 0.000277777805) exchange_data.add_content("x-ray_diffraction_profile_pi", data2) exchange_data.add_content("x-ray_diffraction_profile_pi_units_to_degrees", 0.000277777805) self.send("xrayserver_data", exchange_data) pass except urllib.error.HTTPError as e: self.x0h_output.setHtml('The server couldn\'t fulfill the request.\nError Code: ' + str(e.code) + "\n\n" + server.BaseHTTPRequestHandler.responses[e.code][1]) raise e except urllib.error.URLError as e: self.x0h_output.setHtml('We failed to reach a server.\nReason: ' + e.reason) raise e except XrayServerException as e: ShowHtmlDialog.show_html("X-ray Server Error", e.response, width=750, height=500, parent=self) raise e except Exception as e: self.x0h_output.setHtml('We failed to reach a server.\nReason: ' + str(e)) raise e self.setStatusMessage("") self.progressBarFinished() def clear_response(self, response): print(response) # remove links output = response.split("<img src=\"images/x.gif\" width=\"31\" height=\"32\" border=\"0\">")[0] + "\n</body></html>" # remove "get the curve" images output = "".join(output.split("<input type=image src=\"images/get_the_curve.gif\" border=0 width=102 height=12 alt=\"Get the reflectivity curve\">")) output = "".join(output.split("<input type=image src=\"images/get_the_curve.gif\" border=0 width=102 height=12 alt=\"Get the Bragg curve (sigma)\">")) output = "".join(output.split("<input type=image src=\"images/get_the_curve.gif\" border=0 width=102 height=12 alt=\"Get the Bragg curve (pi)\">")) # remove question mark images and links output = "".join(output.split("<a href=\"javascript:void(0)\" onClick=\"Wfloat(\'images/x0h_help_0.gif\',\'x0h_0\',740,357);\"><b>?</b></a> ")) output = "".join(output.split("<a href=\"javascript:void(0)\" onClick=\"Wfloat(\'images/x0h_help_h.gif\',\'x0h_h\',705,853);\"><b>?</b></a> ")) temp_1, temp_2 = output.split("style.css") output = temp_1 + XRAY_SERVER_URL + "/style.css" + temp_2 return output def extract_plots(self, response): form_1_begin = False form_2_begin = False form_3_begin = False form_1 = None form_2 = None form_3 = None rows = response.split("\n") for row in rows: if form_1_begin: if "<td>" in row: form_1_begin = False elif form_2_begin: if "<td>" in row: form_2_begin = False elif form_3_begin: if "<td>" in row: form_3_begin = False if form_1_begin: form_1.append(row) elif form_2_begin: form_2.append(row) elif form_3_begin: form_3.append(row) if "/cgi/ter_form.pl" in row: if form_1 is None: form_1 = [] form_1_begin = True if "/cgi/gid_form.pl" in row: if form_2 is None: form_2 = [] form_2_begin = True elif form_3 is None: form_3 = [] form_3_begin = True self.setStatusMessage("Plotting Results") if not form_1 is None: x_1, y_1 = self.get_plots_from_form("/cgi/ter_form.pl", form_1) self.plot_histo(x_1, y_1, 40, 1, 0, "Critical Angle for TER", "Incidence angle [degrees]", "Reflectivity") self.tabs_widget.setCurrentIndex(1) else: x_1 = None y_1 = None if not form_2 is None: x_2, y_2 = self.get_plots_from_form("/cgi/gid_form.pl", form_2) self.plot_histo(x_2, y_2, 60, 2, 1, "Darwin Curve ($\sigma$ Pol.)", "Scan Angle [arcsec]", "Diffracted Intensity") self.tabs_widget.setCurrentIndex(2) else: x_2 = None y_2 = None if not form_3 is None: x_3, y_3 = self.get_plots_from_form("/cgi/gid_form.pl", form_3) self.plot_histo(x_3, y_3, 80, 3, 2, "Darwin Curve ($\pi$ Pol.)", "Scan Angle [arcsec]", "Diffracted Intensity") self.tabs_widget.setCurrentIndex(3) else: x_3 = None y_3 = None return [x_1, y_1], [x_2, y_2], [x_3, y_3]