def youtube(): if len(sys.argv) < 2: helper.print_stderr("The site URL was not passed.") else: url = sys.argv[1] if check_url(url): helper.print_stdout(f"Opening the YouTube video at {url}.") try: webbrowser.open(url) helper.print_stdout(f"YouTube video opened.") except Exception as err: helper.print_stderr(err) else: helper.print_stderr("The URL is invalid.")
def main(): if len(sys.argv) < 4: raise BaseException("Insufficient number of client arguments. Need 5.") name = sys.argv[1] addr = sys.argv[2] port = int(sys.argv[3]) client_socket = socket.socket() try: client_socket.connect((addr, port)) except socket.error as err: error_msg = utils.CLIENT_CANNOT_CONNECT.format(addr, port) helper.print_stdout(error_msg) sys.exit() client = Client(client_socket) # new special message that creates the name for the client client_socket.send(helper.pad_msg(name)) # make it a blocking call interfaces = [client_socket, sys.stdin] helper.print_stdout(utils.CLIENT_MESSAGE_PREFIX) while True: # read data into buffer from available sockets input_ready, output_ready, error = \ select.select(interfaces, [], []) for input_sock in input_ready: output_sock = None for sock in client.sockets: if sock is not input_sock: output_sock = sock assert output_sock try: data = client.parse_input(input_sock) except ValueError as err: assert input_sock is not sys.stdin error_msg = utils.CLIENT_SERVER_DISCONNECTED.format(addr, port) helper.print_stdout(error_msg) sys.exit() if data: if input_sock is sys.stdin: output_sock.sendall(data) else: data = data.rstrip() server_msg = utils.CLIENT_WIPE_ME + '\r' + data helper.print_stdout(server_msg) if data: helper.print_stdout('\n') helper.print_stdout(utils.CLIENT_MESSAGE_PREFIX)
def html_save(url, f_path): reg = r"^(https?|ftp|file)://.+$" res = re.search(reg, url) if res is not None: try: r = requests.get(url) print_stdout(f"Sending the request to the '{url}'.") print_stdout(f"Request to the '{url}' has been sent.") print_stdout(f"<Response [{r.status_code}]>.") print_stdout(f"Parsing page data.") print_stdout(f"Page data has been parsed.") try: print_stdout(f"Saving page data to '{f_path}'.") file = open(f_path, 'w') file.write(str(r.content)) file.close print_stdout(f"Page data has been saved.") except Exception as e: print_stderr(e) except Exception as e: print_stderr(e) else: print_stderr("The site URL format is invalid.")
def html_save(url, path): if check_url(url): helper.print_stdout(f"Sending the request to the '{url}'.") try: data_r = req.get(url) helper.print_stdout(f"Request to the '{url}' has been sent.") if data_r.status_code == 200: helper.print_stdout(str(data_r) + '.') helper.print_stdout("Parsing page data.") helper.print_stdout("Page data has been parsed.") helper.print_stdout(f"Saving page data to '{path}'.") with open(path, "w") as file: file.write(data_r.text) helper.print_stdout("Page data has been saved.") else: helper.print_stderr(str(data_r)) except Exception as err: helper.print_stderr(err) else: helper.print_stderr("The site URL format is invalid.")
import re, sys, webbrowser from helper import print_stderr, print_stdout try: url = sys.argv[1] reg = r"^(https?|ftp|file)://.+$" res = re.search(reg, url) if res is not None: if 'youtube.com' in url: try: print_stdout(f"Opening the YouTube video at {url}.") webbrowser.open(url) print_stdout(f"YouTube video opened.") except Exception as e: print_stderr(e) else: print_stderr("The URL is invalid.") else: print_stderr("The URL is invalid.") except Exception: print_stderr("The site URL was not passed.")