-
Notifications
You must be signed in to change notification settings - Fork 0
/
v2_slide_ogp.py
76 lines (61 loc) · 2.42 KB
/
v2_slide_ogp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import argparse
from pathlib import Path
from urllib.parse import urlparse
from helium import get_driver, go_to, kill_browser, start_firefox
from selenium.webdriver import FirefoxOptions
def format_as_uri(string_for_slide: str) -> str:
"""
>>> format_as_uri("http://example.com/slide.html")
'http://example.com/slide.html'
Case: slide path
>>> expected = f"file://{Path.cwd() / 'build/revealjs/slide.html'}"
>>> expected == format_as_uri("build/revealjs/slide.html")
True
Case: slide directory name only (doctestはルートで実行)
>>> expected = f"file://{Path.cwd() / 'build/revealjs/ainouta/recommend_as_best.html'}"
>>> expected == format_as_uri("ainouta")
True
"""
parsed = urlparse(string_for_slide)
if parsed.scheme in ("http", "https"):
return string_for_slide
if string_for_slide.endswith(".html"):
slide_path = Path(string_for_slide).resolve()
return slide_path.as_uri()
script_path = Path(__file__).resolve()
project_root = script_path.parent.parent
slide_directory = project_root / "build" / "revealjs" / string_for_slide
slides = list(slide_directory.glob("*.html"))
assert len(slides) == 1, str(slides)
return slides[0].as_uri()
def build_output_path(slide_path: str) -> Path:
"""
>>> expected = Path.cwd() / "docs/_images/ogps/test.png"
>>> expected == build_output_path("file:///.../test/awesome_slide.html")
True
"""
parsed = urlparse(slide_path)
stem = Path(parsed.path).parent.stem
script_path = Path(__file__).resolve()
project_root = script_path.parent.parent
return project_root / "docs" / "_images" / "ogps" / f"{stem}.png"
def take_screenshot(url, save_path):
options = FirefoxOptions()
options.add_argument("--width=800")
options.add_argument("--height=485") # ブラウザウィンドウ上部が85px分ある
start_firefox(options=options, headless=True)
go_to(url)
get_driver().save_screenshot(save_path)
kill_browser()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"slide", help="URL, local file path or local directory name"
)
parser.add_argument("output_path", nargs="?", type=Path)
args = parser.parse_args()
uri = format_as_uri(args.slide)
output_path = (
args.output_path if args.output_path else build_output_path(uri)
)
take_screenshot(uri, str(output_path))