commit 9d0115220acaaaa9c09380082218b4a072840d95 parent 72d10482d72dad05bea1abbe534f5cd7f8840fa9 Author: Anders Damsgaard <anders@adamsgaard.dk> Date: Thu, 9 Jul 2020 11:55:05 +0200 Clean up comic script Diffstat:
M | .local/bin/comic | | | 92 | ++++++++++++++++++++++++++++++++++++++++++++++++------------------------------- |
1 file changed, 56 insertions(+), 36 deletions(-)
diff --git a/.local/bin/comic b/.local/bin/comic @@ -1,41 +1,61 @@ #!/bin/sh -set -eu +set -e url="${1:-https://www.smbc-comics.com/}" +die() { + printf '%s: error: %s\n' "${0##*/}" "$1" >&2 + exit 1 +} + +get_html() { + html="$(mktemp)" + $fetcher "$url" > "$html" +} + +if command -v hurl >/dev/null 2>&1; then + fetcher="hurl" +elif command -v curl >/dev/null 2>&1; then + fetcher="curl -L" +else + die 'could not find hurl or curl' +fi + +img="$(mktemp)" alttext="" -case "$url" in - *smbc*) - html="$(curl -s -L "$url")" - f="$(curl -s --write-out "%{filename_effective}\n" -OL \ - $(printf '%s' "$html" | \ - grep '.png' | head -1 | \ - sed 's/^.*https/https/;s/".*$//'))" - alttext="$(printf '%s' "$html" | grep title | tail -1 | head -1 | \ - sed 's/.*title="//;s/".*//')";; - *jspowerhour*) - html="$(curl -s -L "$url")" - f="$(curl -s --write-out "%{filename_effective}\n" -OL \ - $(printf '%s' "$html" | \ - grep '.png' | head -1 | \ - sed 's/^.*src="//;s/" .*//;s|^|https:|'))";; - *xkcd*) - html="$(curl -s -L "$url")" - f="$(curl -s --write-out "%{filename_effective}\n" -OL \ - $(printf '%s' "$html" | grep 'Image URL' | head -1 | \ - sed 's/^.*https/https/;s/".*$//'))" - alttext="$(printf '%s' "$html" | grep 2x | tail -1 | \ - sed 's/.*title="//;s/".*//')";; - *explosm*) - f="$(mktemp)" - hurl "$(hurl "$url" | sed -nE '/.*<meta property="og:image" content="([^"]*)">/{ s//\1/;p; }')" > "$f";; - *) - (>&2 echo "unknown site") - exit 1;; -esac - -xdg-open "$f" -[ -n "$alttext" ] && \ - notify-send "${0##*/}" "$(printf '%s' "$alttext" | w3m -dump -T text/html)" -sleep 1 -rm "$f" + +process_url() { + case "$1" in + *smbc-comics.com*) + get_html + $fetcher "$(sed -nE '/.*<div id="cc-comicbody"><img title="([^"]*)" src="([^"]*)" id="cc-comic" \/>.*/{ + s//\2/;p; }' "$html")" > "$img" + alttext="$(sed -nE '/.*<div id="cc-comicbody"><img title="([^"]*)" src="([^"]*)" id="cc-comic" \/>.*/{ + s//\1/;p; }' "$html")";; + *jspowerhour.com*) + get_html + $fetcher "$(sed -nE '/.*<img alt=".* src="\/\/(assets.jspowerhour.com\/system\/[^"]*)" width="[0-9]*" \/>/{ + s,,https://\1,;p;}' "$html")" > "$img";; + *xkcd.com*) + get_html + $fetcher "$(sed -nE '/.*<meta property="og:image" content="([^"]*)">/{ + s//\1/;p; }' "$html")" > "$img" + alttext="$(sed -nE '/<img src=".*\.png" title="([^"]*)" alt=.*>/{ + s//\1/;p;}' "$html")";; + *explosm.net/comics*) + get_html + $fetcher "$(sed -nE '/.*<meta property="og:image" content="([^"]*)">/{ + s//\1/;p; }' "$html")" > "$img";; + *) + (>&2 echo "unknown site") + exit 1;; + esac + + xdg-open "$img" + [ -n "$alttext" ] && \ + notify-send "${0##*/}" "$(printf '%s' "$alttext" | w3m -dump -T text/html)" +} + +for u in "$@"; do + process_url "$u" +done