|
| 1 | +import re |
| 2 | +import zipfile |
| 3 | +import os, sys, stat, platform |
| 4 | +from urllib.request import urlretrieve |
| 5 | +from collections import namedtuple |
| 6 | + |
| 7 | +from clint.textui import puts, colored |
| 8 | +import progressbar |
| 9 | + |
| 10 | +from selenium import webdriver |
| 11 | + |
| 12 | +from .common import NO_CHROME_DRIVER |
| 13 | +from ..exceptions import UnknownOSException |
| 14 | + |
| 15 | + |
| 16 | +_ = namedtuple('WebDrivers', 'mac linux windows') |
| 17 | +drivers = ['https://chromedriver.storage.googleapis.com/78.0.3904.70/chromedriver_mac64.zip', |
| 18 | + 'https://chromedriver.storage.googleapis.com/78.0.3904.70/chromedriver_linux64.zip', |
| 19 | + 'https://chromedriver.storage.googleapis.com/78.0.3904.70/chromedriver_win32.zip' |
| 20 | + ] |
| 21 | +WebDriver = _(drivers[0], drivers[1], drivers[2]) |
| 22 | + |
| 23 | + |
| 24 | +def extract_zip(filename): |
| 25 | + """ |
| 26 | + Uses zipfile package to extract a single zipfile |
| 27 | + :param filename: |
| 28 | + :return: new filename |
| 29 | + """ |
| 30 | + try: |
| 31 | + _file = zipfile.ZipFile(filename, 'r') |
| 32 | + except FileNotFoundError: |
| 33 | + puts(colored.red(f"{filename} Does not exist")) |
| 34 | + sys.exit(1) |
| 35 | + |
| 36 | + # Save the name of the new file |
| 37 | + new_file_name = _file.namelist()[0] |
| 38 | + |
| 39 | + # Extract the file and make it executable |
| 40 | + _file.extractall() |
| 41 | + |
| 42 | + driver_stat = os.stat(new_file_name) |
| 43 | + os.chmod(new_file_name, driver_stat.st_mode | stat.S_IEXEC) |
| 44 | + |
| 45 | + _file.close() |
| 46 | + os.remove(filename) |
| 47 | + return new_file_name |
| 48 | + |
| 49 | + |
| 50 | +def setup_selenium(driver_path, options): |
| 51 | + # Configures selenium to use a custom path |
| 52 | + return webdriver.Chrome(executable_path=driver_path, options=options) |
| 53 | + |
| 54 | + |
| 55 | +def get_webdriver(): |
| 56 | + """ |
| 57 | + Ensure a webdriver is available |
| 58 | + If Not, Download it. |
| 59 | + """ |
| 60 | + cwd = os.listdir(os.getcwd()) |
| 61 | + webdriver_regex = re.compile('chromedriver') |
| 62 | + web_driver = list(filter(webdriver_regex.match, cwd)) |
| 63 | + |
| 64 | + if web_driver: |
| 65 | + # check if a extracted copy already exists |
| 66 | + if not os.path.isfile('chromedriver'): |
| 67 | + # Extract file |
| 68 | + extract_zip(web_driver[0]) |
| 69 | + |
| 70 | + return "{0}/chromedriver".format(os.getcwd()) |
| 71 | + |
| 72 | + else: |
| 73 | + # Download it according to the current machine |
| 74 | + |
| 75 | + os_platform = platform.system() |
| 76 | + if os_platform == 'Darwin': |
| 77 | + chrome_webdriver = WebDriver.mac |
| 78 | + elif os_platform == 'Linux': |
| 79 | + chrome_webdriver = WebDriver.linux |
| 80 | + elif os_platform == 'Windows': |
| 81 | + chrome_webdriver = WebDriver.windows |
| 82 | + else: |
| 83 | + raise UnknownOSException("Unknown Operating system platform") |
| 84 | + |
| 85 | + global total_size |
| 86 | + |
| 87 | + def show_progress(*res): |
| 88 | + global total_size |
| 89 | + pbar = None |
| 90 | + downloaded = 0 |
| 91 | + block_num, block_size, total_size = res |
| 92 | + |
| 93 | + if not pbar: |
| 94 | + pbar = progressbar.ProgressBar(maxval=total_size) |
| 95 | + pbar.start() |
| 96 | + downloaded += block_num * block_size |
| 97 | + |
| 98 | + if downloaded < total_size: |
| 99 | + pbar.update(downloaded) |
| 100 | + else: |
| 101 | + pbar.finish() |
| 102 | + |
| 103 | + puts(colored.yellow("Downloading Chrome Webdriver")) |
| 104 | + file_name = chrome_webdriver.split('/')[-1] |
| 105 | + response = urlretrieve(chrome_webdriver, file_name, show_progress) |
| 106 | + |
| 107 | + if int(response[1].get('Content-Length')) == total_size: |
| 108 | + puts(colored.green(f"DONE!")) |
| 109 | + |
| 110 | + return "{0}/{1}".format(os.getcwd(), extract_zip(file_name)) |
| 111 | + |
| 112 | + else: |
| 113 | + puts(colored.red("An error Occurred While trying to download the driver.")) |
| 114 | + # remove the downloaded file and exit |
| 115 | + os.remove(file_name) |
| 116 | + sys.stderr.write(NO_CHROME_DRIVER) |
| 117 | + sys.exit(1) |
0 commit comments