Skip to content

Instantly share code, notes, and snippets.

@pystardust
Last active October 18, 2021 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pystardust/46a0b136ddd1a108d11b4fddb5c622eb to your computer and use it in GitHub Desktop.
Save pystardust/46a0b136ddd1a108d11b4fddb5c622eb to your computer and use it in GitHub Desktop.
xkcd
#!/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