python-chromedriver-autoinstaller
python-chromedriver-autoinstaller copied to clipboard
add support for custom URL
Hi, sometime we do not have access to the google pages from inside corporate network, so we keep zip's on our custom host. I would like to provide my own link to download the dedicated .zip file.
we just need add new argument custom_url_to_zip, add change variable for folder_name and chromedriver_version
`def download_chromedriver(path: Optional[AnyStr] = None, no_ssl: bool = False, custom_url_to_zip: str = ""):
if not custom_url_to_zip:
chrome_version = get_chrome_version()
if not chrome_version:
logging.debug("Chrome is not installed.")
return
chromedriver_version, download_options = get_matched_chromedriver_version(chrome_version, no_ssl)
major_version = get_major_version(chromedriver_version)
folder_name = major_version
if not chromedriver_version or (major_version >= "115" and not download_options):
logging.warning(
"Can not find chromedriver for currently installed chrome version."
)
return
else:
folder_name = 'chromedriver'
chromedriver_version = 'custom'
if path:
if not os.path.isdir(path):
raise ValueError(f"Invalid path: {path}")
chromedriver_dir = os.path.join(os.path.abspath(path), folder_name)
else:
chromedriver_dir = os.path.join(
os.path.abspath(os.path.dirname(__file__)), folder_name
)
chromedriver_filename = get_chromedriver_filename()
chromedriver_filepath = os.path.join(chromedriver_dir, chromedriver_filename)
if not os.path.isfile(chromedriver_filepath) or not check_version(
chromedriver_filepath, chromedriver_version
):
logging.info(f"Downloading chromedriver ({chromedriver_version})...")
if not os.path.isdir(chromedriver_dir):
os.makedirs(chromedriver_dir)
if not custom_url_to_zip:
url = get_chromedriver_url(chromedriver_version=chromedriver_version, download_options=download_options, no_ssl=no_ssl)
else:
url = custom_url_to_zip
try:
response = urllib.request.urlopen(url)
if response.getcode() != 200:
raise urllib.error.URLError("Not Found")
except urllib.error.URLError:
raise RuntimeError(f"Failed to download chromedriver archive: {url}")
archive = BytesIO(response.read())
with zipfile.ZipFile(archive) as zip_file:
# v115+ have files in subdirectories- need to adjust filename before extracting
for zip_info in zip_file.infolist():
if os.path.basename(zip_info.filename) == chromedriver_filename:
zip_info.filename = chromedriver_filename
zip_file.extract(zip_info, chromedriver_dir)
break
else:
logging.info("Chromedriver is already installed.")
if not os.access(chromedriver_filepath, os.X_OK):
os.chmod(chromedriver_filepath, 0o744)
return chromedriver_filepath`