add my ‘save xkcd’ script
- ID
4b53a22- date
2023-07-28 18:27:03+00:00- author
Alex Chan <alex@alexwlchan.net>- parent
6cdfcbe- message
add my 'save xkcd' script- changed files
2 files, 51 additions, 2 deletions
Changed files
images/README.md (4243) → images/README.md (4491)
diff --git a/images/README.md b/images/README.md
index 29e273f..7ced139 100644
--- a/images/README.md
+++ b/images/README.md
@@ -45,7 +45,7 @@ These scripts are for working with images and other visual material.
I use this to work around an odd behaviour of the “Scan Document” feature in Notes.app, where it adds a large white border around scanned images that I don’t want.
<p><strong>Note:</strong> this script overwrites the original file.</p>
</dd>
-
+
<dt>
<a href="https://github.com/alexwlchan/scripts/blob/main/images/kn_cover_image">
<code>kn_cover_image [PATH]</code>
@@ -104,7 +104,16 @@ These scripts are for working with images and other visual material.
<dd>
an alias for running the Retrobatch image processor <a href="https://flyingmeat.com/retrobatch/docs-1.0/commandline/">from the command-line</a>
</dd>
-
+
+ <dt>
+ <a href="https://github.com/alexwlchan/scripts/blob/main/images/save_xkcd">
+ <code>save_xkcd [COMIC_NUMBER]</code>
+ </a>
+ </dt>
+ <dd>
+ saves a single comic from <a href="https://xkcd.com/">xkcd</a>, plus some metadata.
+ </dd>
+
<dt>
<a href="https://github.com/alexwlchan/scripts/blob/main/images/tint_image">
<code>tint_image [PATH] [HEX_COLOUR]</code>
images/save_xkcd (0) → images/save_xkcd (1117)
diff --git a/images/save_xkcd b/images/save_xkcd
new file mode 100755
index 0000000..014114e
--- /dev/null
+++ b/images/save_xkcd
@@ -0,0 +1,40 @@
+#!/usr/bin/env python3
+"""
+Downloads and saves a single xkcd comic, plus a bit of metadata.
+
+I'm not using this to create a complete archive of xkcd (of which I'm sure
+many already exist) but to create a mini-library of my personal favourites.
+"""
+
+import json
+import os
+import pathlib
+import sys
+from urllib.request import urlretrieve
+
+import httpx
+
+BACKUP_ROOT = pathlib.Path("/Volumes/Media (Sapphire)/backups/xkcd")
+
+
+if __name__ == "__main__":
+ try:
+ xkcd_number = int(sys.argv[1])
+ except (IndexError, ValueError):
+ sys.exit(f"Usage: {__file__} <XKCD_NUMBER>")
+
+ resp = httpx.get(f"https://xkcd.com/{xkcd_number}/info.0.json")
+
+ img_url = resp.json()["img"]
+ filename = os.path.basename(img_url)
+ name = os.path.splitext(filename)[0]
+
+ try:
+ with open(BACKUP_ROOT / f"{xkcd_number}-{name}.json", "x") as outfile:
+ outfile.write(json.dumps(resp.json(), indent=2, sort_keys=True))
+ except FileExistsError:
+ pass
+
+ out_path = BACKUP_ROOT / f"{xkcd_number}-{filename}"
+ if not out_path.exists():
+ urlretrieve(resp.json()["img"], out_path)