def capture_url(port_tuple, project='project', timeout=10): """ This function is responsible to create a screen capture from ip and port. The procedure of this function creates a URL which consists from ip:port, If the url is valid, it opens headless browser and capture the page. Finally, it returns tuple with the current details (host, port, details, url). :param port_tuple: Tuple of ports. :param project: Project name. Default is 'project' :param timeout:How long to wait on page load. Default is 10 secs. """ # Extract The Port Tuple host, port, details = port_tuple # Crate New Port Details Dictionary port_details = { 'state': details['state'].upper(), 'service': details['name'].upper(), 'product': "{product_name} {product_version}".format( product_name=details['product'], product_version=details['version'], ).strip() } # Create Ghost Object And Set Size ghost = Ghost( wait_timeout=timeout, download_images=True, cache_dir=None, ) ghost.webview.resize(QSize(1280, 720)) ghost.page.setViewportSize(QSize(1280, 720)) # Try To Open URL page = None for http_type in ('http', 'https'): request_url = '%s://%s:%d/' % (http_type, host, port) try: page = ghost.open(request_url)[0] except TimeoutError: pass if page: # Make a Screen Capture capture_name = '%s-%s-%d.png' % (http_type, host, port) ghost.capture_to(path.join(project, capture_name)) return host, port, port_details, request_url return host, port, port_details, 'Not HTTP/S Service'
def fetch_grades(year, semester, base_meyda_net_url, id_number, meyda_net_password, timeout=None): logging.info("Starting fetching process") reload(sys) sys.setdefaultencoding("utf-8") ghost = Ghost().start() page, extra_resources = ghost.open(base_meyda_net_url + "/fireflyweb.aspx", timeout=timeout) if not page: return None result, resources = ghost.set_field_value("input[name=R1C1]", id_number) result, resources = ghost.set_field_value("input[name=R1C2]", meyda_net_password) page, resources = ghost.call("form", "submit", expect_loading=True) page, extra_resources = ghost.open( base_meyda_net_url + "/fireflyweb.aspx?PRGname=MenuCall&ARGUMENTS=-N,-N,-N0013,", timeout=timeout) uniq = str( ghost.evaluate('document.getElementsByName("UNIQ")[0].value')[0]) # R1C2=0 means "שנתי" page, extra_resources = ghost.open( (base_meyda_net_url + "/fireflyweb.aspx?PRGname=Bitsua_maarechet_shaot" + "&ARGUMENTS=TZ,UNIQ,MisparSheilta,R1C1,R1C2&TZ=" + id_number + "&UNIQ=%s&MisparSheilta=13&R1C1=" + year + "&R1C2=" + semester) % uniq, timeout=timeout) logging.info("Starting parsing process") parser = BeautifulSoup(str(page.content), "lxml") table = parser.find("table", class_="SortMe") table_thead = table.find("thead") HEADERS = ["סמסטר", "שם קורס", "מועד", "ציון", "סוג מקצוע"] table_col_dict = {} index = 0 for th in table_thead.find("tr").find_all("th"): current_header = th.text.strip().encode("utf-8") if current_header in HEADERS: table_col_dict[current_header] = index index += 1 curr_grades = {} def extract_text_by_key(key): return line_cells[table_col_dict[key]].text.strip().encode("utf-8") for tr in table.find("tbody").find_all("tr"): line_cells = tr.find_all("td") if "שעור" in extract_text_by_key("סוג מקצוע"): ukey = extract_text_by_key("שם קורס") + ";" + extract_text_by_key( "סמסטר") + ";" + extract_text_by_key("מועד") curr_grades[ukey] = extract_text_by_key("ציון") return curr_grades