commit abd4367d19133bba582b1cf5bb5269743bd0a14c
parent 3ab52ac90eb4a8c8c4d8df76467f34f09b769322
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date: Thu, 9 Jul 2020 12:17:33 +0200
Fix logic
Diffstat:
1 file changed, 35 insertions(+), 12 deletions(-)
diff --git a/.local/bin/comic b/.local/bin/comic
@@ -1,16 +1,25 @@
#!/bin/sh
set -e
-url="${1:-https://www.smbc-comics.com/}"
+help() {
+ printf 'usage %s TARGET ...\n' "${0##*/}"
+ printf 'where TARGET can be a comic URL or one of the strings: smbc, jsph, xkcd.\n'
+ printf 'Using the these strings gets the latest comic strip from the respective site.\n'
+}
die() {
printf '%s: error: %s\n' "${0##*/}" "$1" >&2
exit 1
}
-get_html() {
- html="$(mktemp)"
- $fetcher "$url" > "$html"
+fetch() {
+ if [ -n "$1" ]; then
+ out="$(mktemp)"
+ $fetcher "$1" > "$out"
+ printf '%s' "$out"
+ else
+ die 'empty url passed to fetch()'
+ fi
}
if command -v hurl >/dev/null 2>&1; then
@@ -21,29 +30,29 @@ else
die 'could not find hurl or curl'
fi
-img="$(mktemp)"
-alttext=""
-
process_url() {
+ img="$(mktemp)"
+ alttext=""
+
case "$1" in
*smbc-comics.com*)
- get_html
+ html="$(fetch "$1")"
$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
+ html="$(fetch "$1")"
$fetcher "$(sed -nE '/.*<img alt=".* src="\/\/(assets.jspowerhour.com\/system\/[^"]*)" width="[0-9]*" \/>/{
s,,https://\1,;p;}' "$html")" > "$img";;
*xkcd.com*)
- get_html
+ html="$(fetch "$1")"
$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
+ html="$(fetch "$1")"
$fetcher "$(sed -nE '/.*<meta property="og:image" content="([^"]*)">/{
s//\1/;p; }' "$html")" > "$img";;
*)
@@ -55,6 +64,20 @@ process_url() {
notify-send "${0##*/}" "$(printf '%s' "$alttext" | w3m -dump -T text/html)"
}
+if [ $# -lt 1 ]; then
+ help
+ exit 1
+fi
+
for u in "$@"; do
- process_url "$u"
+ case "$u" in
+ smbc)
+ process_url "https://www.smbc-comics.com";;
+ jsph)
+ process_url "https://www.jspowerhour.com";;
+ xkcd)
+ process_url "https://xkcd.com";;
+ *)
+ process_url "$u";;
+ esac
done