Last active
June 4, 2021 16:20
-
-
Save dikiaap/96b9725f8c55d52b603d773de90fd3dd to your computer and use it in GitHub Desktop.
Bash script to download xkcd comics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Print usage message. | |
usage() { | |
local program_name | |
program_name=${0##*/} | |
cat <<EOF | |
Usage: $program_name [-option] | |
Options: | |
-h Print this message | |
-a Download all images | |
-s [value] Download from specific comic number | |
EOF | |
} | |
# Download image and JSON file. | |
download_xkcd_images() { | |
TOTAL=$(curl https://xkcd.com/info.0.json | grep -Po '"num": [0-9]+' | grep -Po '[0-9]+') | |
download_dir="$HOME/Downloads/xkcd" | |
mkdir -p "$download_dir/JSON" | |
for i in $(seq "${FROM:-1}" "$TOTAL"); do | |
URL=https://xkcd.com/$i/info.0.json | |
JSON=$(curl "$URL") | |
IMG=$(echo "$JSON" | grep -Po '"img":\s"https:\/\/imgs.xkcd.com\/comics\/[\w\(\)-]+.(png|jpg|gif)"' | grep -Po 'https://imgs.xkcd.com\/comics\/[\w\(\)-]+.(png|jpg|gif)' | sed 's/\\//g') | |
TYPE=$(echo "$IMG" | grep -o "...$") | |
NUM=$(echo "$JSON" | grep -Po '"num": [0-9]+' | grep -Po '[0-9]+') | |
TITLE=$(echo "$JSON" | grep -Po '"title": ".*?"' | grep -Po '"[A-Z0-9].*?"' | sed 's/"//g') | |
echo "$JSON" > "$download_dir/xkcd $NUM: $TITLE.json" | |
wget "$IMG" -O "$download_dir/xkcd $NUM: $TITLE.$TYPE" | |
done | |
# Verification, there should be 2 files for each comic (image, JSON). | |
# Some don't have an image (404, 1446, 1608, 1663), so a small amount of errors being reported is ok. | |
for i in $(seq "${FROM:-1}" "$TOTAL"); do | |
if [[ $(ls "$download_dir" | grep "xkcd $i" | wc -l) -lt 2 ]]; then | |
echo "Error at xkcd $i" | |
fi | |
done | |
mv -f "$download_dir"/*.json "$download_dir/JSON" | |
} | |
main() { | |
if [ -z "$1" ]; then | |
usage | |
exit | |
fi | |
while getopts has: option; do | |
case "${option}" in | |
h) | |
usage | |
exit 0 | |
;; | |
a) | |
download_xkcd_images | |
;; | |
s) | |
FROM=${OPTARG} | |
download_xkcd_images | |
;; | |
esac | |
done | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Shortened URL for raw text of this script: https://git.io/xkcd-dl