Last active
October 18, 2021 19:02
-
-
Save pystardust/46a0b136ddd1a108d11b4fddb5c622eb to your computer and use it in GitHub Desktop.
xkcd
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
#!/bin/sh | |
# USAGE: ./xkcd_fetch.sh [-r] | |
# Get random xkcd_commics | |
# Idea by https://github.com/Error916/xkcd-retriver | |
viewer=sxiv | |
dest_img=/tmp/xkcd_image.png | |
# when random is not mention it defaults to latest | |
json_url='https://xkcd.com/info.0.json' | |
while getopts 'r' opt; do | |
case $opt in | |
r) | |
printf 'Getting comic count...\n' >&2 | |
num=$(curl -sL 'https://xkcd.com/info.0.json' | jq -r '.num') | |
printf 'Fetching random comic...\n' >&2 | |
json_url="https://xkcd.com/$(shuf -i 1-$num -n 1)/info.0.json" | |
;; | |
esac | |
done | |
printf 'Downloading comic...\n' >&2 | |
image=$(curl -sL "$json_url" | jq -r '.img') | |
curl -sL "$image" -o "$dest_img" | |
$viewer "$dest_img" | |
rm -v "$dest_img" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment