diff --git a/.gitignore b/.gitignore index 8321b98..143439f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ chromedriver.zip LICENSE.chromedriver -chromedriver +chromedriver-*.zip +chrome-*.zip __pycache__ build dist diff --git a/Makefile b/Makefile deleted file mode 100644 index 665979a..0000000 --- a/Makefile +++ /dev/null @@ -1,4 +0,0 @@ -test: - python3 -m unittest tests/test_*.py - -.PHONY: test diff --git a/README.md b/README.md index 77a38c7..c46a69c 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,8 @@ # Chromedriver Downloader -これは、標準入力に与えられたGoogle Chromeのバージョンから、利用可能なchromedriverのバージョンを推定して、chromedriver.zipをダウンロードするツールです。 +これは、引数に与えられたバージョンのchrome.zipとchromedriver.zipをダウンロードするツールです。 -現時点では、google chrome専用です。 たぶん、chromiumでは動きません。 - -This is a tool that estimates the available chromedriver version from the Google Chrome version given in the standard input and downloads the chromedriver.zip. - -At this time, it is only for google chrome. -It may not work on chromium. +現時点では、google chrome専用です。 chromiumでは動きません。 ## インストール / Install @@ -18,23 +13,7 @@ pip install git+https://github.com/hk220/chromedriver-downloader.git ## 使い方 / Usage ```bash -# from google-chrome --version -google-chrome --version | chromedriver-downloader -# from deb file -dpkg-deb -f ./google-chrome-stable_current_amd64.deb version | chromedriver-downloader -# from version -echo "111.0.5563.64" | chromedriver-downloader +chromedriver-downloader "115.0.5763.0" linux64 ``` -カレントディレクトリに`chromedriver.zip`がダウンロードされます。 - -The `chromedriver.zip` will be downloaded to the current directory. - -## テスト / Test - -``` -# unitest -make test -# docker test -docker build . -``` +カレントディレクトリに`chrome-.zip`と`chromedriver-.zip`がダウンロードされます。 diff --git a/chromedriver_downloader/__init__.py b/chromedriver_downloader/__init__.py index 694eaca..80196df 100644 --- a/chromedriver_downloader/__init__.py +++ b/chromedriver_downloader/__init__.py @@ -1,93 +1,52 @@ import logging -import re import sys import urllib.request import urllib.error +import json -logging.basicConfig(level=logging.DEBUG) - -LATEST_RELEASE_URL = "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_" -CHROME_DRIVER_URL = "https://chromedriver.storage.googleapis.com/{version}/chromedriver_{arch}.zip" - - -class ChromeVersionFormatExepction(Exception): - pass - - -class Chrome(object): - def __init__(self, chrome_version: str) -> None: - self._chrome_version = chrome_version - - @property - def version(self) -> str: - return self._chrome_version - - @property - def major_version(self) -> str: - splited_version = self._chrome_version.split('.') - if len(splited_version) != 4: - raise ChromeVersionFormatExepction("Must be 4 numbers.") - - return '.'.join(splited_version[:-1]) - - @classmethod - def parse(cls, version_text: str) -> 'Chrome': - # google-chrome --version - # Google Chrome 111.0.5563.64 - if version_text.startswith("Google Chrome"): - splited_text = version_text.strip().split() - if len(splited_text) != 3: - raise ChromeVersionFormatExepction(f"Must be able to divide into 3 words: {version_text}") - return cls(splited_text[2]) - # dpkg-deb -f ./google-chrome-stable_current_amd64.deb version - # 111.0.5563.64-1 - if re.match(r"^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\-[0-9]+$", version_text.strip()): - splited_text = version_text.strip().split("-") - if len(splited_text) != 2: - raise ChromeVersionFormatExepction(f"Must be able to divide into 2 words: {version_text}") - return cls(splited_text[0]) - # 111.0.5563.64 - if re.match(r"^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$", version_text.strip()): - return cls(version_text.strip()) - else: - raise ChromeVersionFormatExepction(f"Not found format: {version_text}") - - -class ChromeDriver(object): - @staticmethod - def download(version: str) -> None: - splited_version = version.split('.') - if len(splited_version) != 4: - raise ChromeVersionFormatExepction("Must be 4 numbers.") - major_version = '.'.join(splited_version[:-1]) - latest_release_url = LATEST_RELEASE_URL + major_version - body = "" - try: - with urllib.request.urlopen(latest_release_url) as res: - body = res.read() - except urllib.error.URLError as e: - logging.critical(f"Fetch chromedriver version error: {e}") - exit(255) - chromedriver_version = body.decode("utf-8").strip() - logging.info(f"chromedriver version: {chromedriver_version}") - chrome_driver_url = CHROME_DRIVER_URL.format(version=chromedriver_version, arch="linux64") - try: - with urllib.request.urlopen(chrome_driver_url) as res: - data = res.read() - with open("chromedriver.zip", "wb") as f: - f.write(data) - except urllib.error.URLError as e: - logging.critical(f"Fetch chromedriver error: {e}") - exit(255) +VERSION_API = "https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json" +logging.basicConfig(level=logging.DEBUG) -def main() -> None: - # read from stdin - version_text = "".join(sys.stdin.readlines()) - logging.info(f"version_text: {version_text.strip()}") - chrome = Chrome.parse(version_text) - logging.info(f"chrome version: {chrome.version}") - ChromeDriver.download(chrome.version) +def download(url, filename): + try: + with urllib.request.urlopen(url) as res: + data = res.read() + with open(filename, "wb") as f: + f.write(data) + except urllib.error.URLError as e: + logging.critical(f"Fetch {filename} error: {e}") + exit(255) + +def get_url(chrome_version, platform): + chrome_url = "" + chromedriver_url = "" + body = "" + try: + with urllib.request.urlopen(VERSION_API) as f: + body = f.read().decode("utf-8") + except urllib.error.URLError as e: + logging.critical(f"Fetch chromedriver version error: {e}") + exit(255) + obj = json.loads(body) + chrome_url = "" + chromedriver_url = "" + for v in obj["versions"]: + if v["version"] == chrome_version: + for d in v["downloads"]["chrome"]: + if d["platform"] == platform: + chrome_url = d["url"] + for d in v["downloads"]["chromedriver"]: + if d["platform"] == platform: + chromedriver_url = d["url"] + return (chrome_url, chromedriver_url) + +def main(): + chrome_version = sys.argv[1] + platform = sys.argv[2] + chrome_url, chromedriver_url = get_url(chrome_version, platform) + download(chrome_url, f"chrome-{platform}.zip") + download(chromedriver_url, f"chromedriver-{platform}.zip") if __name__ == "__main__": main() diff --git a/tests/test_chrome.py b/tests/test_chrome.py deleted file mode 100644 index cd697a0..0000000 --- a/tests/test_chrome.py +++ /dev/null @@ -1,29 +0,0 @@ -import unittest - -from chromedriver_downloader import Chrome - -class TestChrome(unittest.TestCase): - - def test_parse_with_google_chrome_version(self): - cv = Chrome.parse("Google Chrome 111.0.5563.64\n\n") - expect = "111.0.5563.64" - actual = cv.version - self.assertEqual(actual, expect) - - def test_parse_with_deb_version(self): - cv = Chrome.parse("111.0.5563.64-1\n") - expect = "111.0.5563.64" - actual = cv.version - self.assertEqual(actual, expect) - - def test_parse(self): - cv = Chrome.parse("111.0.5563.64\n") - expect = "111.0.5563.64" - actual = cv.version - self.assertEqual(actual, expect) - - def test_major_version(self): - cv = Chrome.parse("111.0.5563.64\n") - expect = "111.0.5563" - actual = cv.major_version - self.assertEqual(actual, expect)