comic (2259B)
1 #!/bin/sh 2 set -e 3 4 help() { 5 printf 'usage %s TARGET ...\n' "${0##*/}" 6 printf 'where TARGET can be a comic URL or one of the strings: smbc, jsph, xkcd.\n' 7 printf 'Using the these strings gets the latest comic strip from the respective site.\n' 8 } 9 10 die() { 11 printf '%s: error: %s\n' "${0##*/}" "$1" >&2 12 exit 1 13 } 14 15 fetch() { 16 if [ -n "$1" ]; then 17 out="$(mktemp)" 18 $fetcher "$1" > "$out" 19 printf '%s' "$out" 20 else 21 die 'empty url passed to fetch()' 22 fi 23 } 24 25 if command -v hurl >/dev/null 2>&1; then 26 fetcher="hurl" 27 elif command -v curl >/dev/null 2>&1; then 28 fetcher="curl -L" 29 else 30 die 'could not find hurl or curl' 31 fi 32 33 process_url() { 34 img="$(mktemp)" 35 alttext="" 36 37 case "$1" in 38 *smbc-comics.com*) 39 html="$(fetch "$1")" 40 $fetcher "$(sed -nE '/.*<div id="cc-comicbody"><img title="([^"]*)" src="([^"]*)" id="cc-comic" \/>.*/{ 41 s//\2/;p; }' "$html")" > "$img" 42 alttext="$(sed -nE '/.*<div id="cc-comicbody"><img title="([^"]*)" src="([^"]*)" id="cc-comic" \/>.*/{ 43 s//\1/;p; }' "$html")";; 44 *jspowerhour.com*) 45 html="$(fetch "$1")" 46 $fetcher "$(sed -nE '/.*<img alt=".* src="\/\/(assets.jspowerhour.com\/system\/[^"]*)" width="[0-9]*" \/>/{ 47 s,,https://\1,;p;}' "$html")" > "$img";; 48 *xkcd.com*) 49 html="$(fetch "$1")" 50 $fetcher "$(sed -nE '/.*<meta property="og:image" content="([^"]*)">/{ 51 s//\1/;p; }' "$html")" > "$img" 52 alttext="$(sed -nE '/<img src=".*\.png" title="([^"]*)" alt=.*>/{ 53 s//\1/;p;}' "$html")";; 54 *explosm.net/comics*) 55 html="$(fetch "$1")" 56 $fetcher "$(sed -nE '/.*<meta property="og:image" content="([^"]*)">/{ 57 s//\1/;p; }' "$html")" > "$img";; 58 *patchfriday*) 59 html="$(fetch "$1")" 60 $fetcher "https://patchfriday.com$(sed -nE '/.*<img src="([^"]*)" .*/{ 61 s//\1/;p; }' "$html")" > "$img";; 62 *) 63 die "unknown site: $1";; 64 esac 65 66 xdg-open "$img" 67 [ -n "$alttext" ] && \ 68 xmessage "$(printf '%s' "$alttext" | w3m -dump -T text/html)" 69 } 70 71 if [ $# -lt 1 ]; then 72 help 73 exit 1 74 fi 75 76 for u in "$@"; do 77 case "$u" in 78 smbc) 79 u="https://www.smbc-comics.com";; 80 jsph) 81 u="https://www.jspowerhour.com";; 82 xkcd) 83 u="https://xkcd.com";; 84 patchfriday) 85 u="https://patchfriday.com";; 86 esac 87 process_url "$u" 88 done