forked from simonw/til
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_screenshots.py
61 lines (51 loc) · 1.8 KB
/
generate_screenshots.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
import hashlib
import pathlib
import subprocess
import sqlite_utils
import tempfile
root = pathlib.Path(__file__).parent.resolve()
TMP_PATH = pathlib.Path(tempfile.gettempdir())
SHOT_HASH_PATHS = [
(root / "templates" / "row.html"),
(root / "templates" / "til_base.html"),
]
def png_for_path(path):
page_html = str(TMP_PATH / "generate-screenshots-page.html")
# Use datasette to generate HTML
proc = subprocess.run(["datasette", ".", "--get", path], capture_output=True)
open(page_html, "wb").write(proc.stdout)
# Now use puppeteer screenshot to generate a PNG
proc2 = subprocess.run(
[
"puppeteer",
"screenshot",
page_html,
"--viewport",
"800x400",
"--full-page=false",
],
capture_output=True,
)
png_bytes = proc2.stdout
return png_bytes
def generate_screenshots(root):
db = sqlite_utils.Database(root / "tils.db")
# The shot_hash incorporates a hash of all of row.html
shot_html_hash = hashlib.md5()
for filepath in SHOT_HASH_PATHS:
shot_html_hash.update(filepath.read_text().encode("utf-8"))
shot_html_hash = shot_html_hash.hexdigest()
for row in db["til"].rows:
path = row["path"]
html = row["html"]
shot_hash = hashlib.md5((shot_html_hash + html).encode("utf-8")).hexdigest()
if shot_hash != row.get("shot_hash"):
png = png_for_path("/tils/til/{}".format(path))
db["til"].update(path, {"shot": png, "shot_hash": shot_hash}, alter=True)
print(
"Got {} byte PNG for {} shot hash {}".format(len(png), path, shot_hash)
)
else:
print("Skipped {} with shot hash {}".format(path, shot_hash))
if __name__ == "__main__":
generate_screenshots(root)