Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2 first commit. #7

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
chromedriver.zip
LICENSE.chromedriver
chromedriver
chromedriver-*.zip
chrome-*.zip
__pycache__
build
dist
Expand Down
4 changes: 0 additions & 4 deletions Makefile

This file was deleted.

29 changes: 4 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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-<platform>.zip`と`chromedriver-<platform>.zip`がダウンロードされます。
125 changes: 42 additions & 83 deletions chromedriver_downloader/__init__.py
Original file line number Diff line number Diff line change
@@ -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()
29 changes: 0 additions & 29 deletions tests/test_chrome.py

This file was deleted.