plumb.sh - randomcrap - random crap programs of varying quality
 (HTM) git clone git://git.codemadness.org/randomcrap
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       plumb.sh (5890B)
       ---
            1 #!/bin/sh
            2 
            3 # match(str, regex, [flags])
            4 match() {
            5         printf '%s' "$1" | grep "-qE$3" "$2"
            6 }
            7 
            8 # isallowedproto(url)
            9 isallowedproto() {
           10         match "$1" '^(http|https|gopher|gophers|rtmp|lbry|ytdl)://'
           11 }
           12 
           13 # isgraphical mode? X11 or not.
           14 isgraphical() {
           15         test -n "$DISPLAY"
           16 }
           17 
           18 # image(url)
           19 image() {
           20         # scale to fit (-s f).
           21         # private mode (no cache), (-p).
           22         test -n "$1" && sxiv -p -s f "$1"
           23 }
           24 
           25 # pdf(url)
           26 pdf() {
           27         test -n "$1" && mupdf "$1"
           28 }
           29 
           30 # www(url)
           31 www() {
           32         test -n "$1" || return
           33 #        isgraphical && "${BROWSER:-firefox}" "$1" || lynx "$1"
           34         "${BROWSER:-firefox}" "$1"
           35 }
           36 
           37 #audio(url)
           38 audio() {
           39         test -n "$1" && mpv "$1"
           40 }
           41 
           42 # movie(url)
           43 movie() {
           44         test -n "$1" && mpv "$1"
           45 }
           46 
           47 # handle pastebin-like url.
           48 # pastebin(url)
           49 pastebin() {
           50         st -e less "$1"
           51 }
           52 
           53 # fetch(url)
           54 fetch() {
           55         hurl -t 30 "$1"
           56         #curl -H 'User-Agent:' -m 30 "$1"
           57 }
           58 
           59 # download file, invoke a function on the text, delete temporary file.
           60 # tempview(fn, text)
           61 tempview() {
           62         tmp=$(mktemp)
           63         fetch "$2" > "$tmp"
           64         test -f "$tmp" && "$1" "$tmp"
           65         rm -f "$tmp"
           66 }
           67 
           68 # specify as argument or use clipboard.
           69 text=""
           70 if [ x"$1" != x"" ]; then
           71         text="$1"
           72 else
           73         if test "$#" != "0"; then
           74                 echo "if an argument is specified it must be non-empty" >&2
           75                 exit 1
           76         fi
           77         text=$(sselp)
           78 fi
           79 
           80 # check allowed protocols.
           81 if ! isallowedproto "$text"; then
           82         echo "disallowed proto for $text" >&2
           83         exit 1
           84 fi
           85 
           86 # specified action.
           87 # this is always a temporary download followed by an action and a delete.
           88 if [ x"$2" != x"" ]; then
           89         tempview "$2" "$1"
           90         exit $?
           91 fi
           92 
           93 # change gophers:// to gopher:// for now.
           94 #if match "$text" '^gophers://'; then
           95 #        text="${text#gophers://}"
           96 #        text="gopher://${text}"
           97 #fi
           98 
           99 # gopher protocol plumber
          100 if match "$text" '^(gopher|gophers)://'; then
          101         filename=$(basename "$text" | sed 's@^\.*@@g')
          102         mkdir -p "/tmp/gophertmp"
          103         chmod 700 "/tmp/gophertmp"
          104         out="/tmp/gophertmp/${filename}"
          105         gophertype=$(printf '%s' "$text" | \
          106                 sed -E -e 's@^(gopher|gophers)://@@g' -e 's@^[^/]*/?@@g'  | \
          107                 cut -b 1)
          108 
          109         if test "$gophertype" = "" || test "$gophertype" = "1"; then
          110                 st -e sacc "$text"
          111                 exit $?
          112         fi
          113 
          114         # Download to temp dir and use it's output.
          115         fetch "$text" > "${out}"
          116 
          117         if test "$gophertype" = "0"; then
          118                 st -e less "$out"
          119         elif test "$gophertype" = "I"; then
          120                 image "$out"
          121         elif test "$gophertype" = "g"; then
          122                 movie "$out"
          123         elif test "$gophertype" = "a" -o "$gophertype" = "s"; then
          124                 audio "$out" # unofficial types used for audio/sound sometimes.
          125         elif match "$text" '\.(mkv|mp4|m4v|avi|webm|ogg|ogv|gif|gifv|ogm)$' 'i'; then
          126                 movie "$out"
          127         elif match "$text" '\.(wav|mp3|flac)$' 'i'; then
          128                 audio "$out"
          129         elif match "$text" '\.(png|jpg|jpeg|webp|bmp)$' 'i'; then
          130                 image "$out"
          131         fi
          132 elif match "$text" '^(ytdl)://'; then
          133         text="https://www.youtube.com/embed/${text}"
          134         movie "$text"
          135 elif match "$text" '^(lbry)://'; then
          136         text=$("$HOME/.config/scripts/plumb/lbry.sh" "$text")
          137         movie "$text"
          138 elif match "$text" '^(http|https)://([a-zA-Z0-9]*\.)?wikipedia\.org/'; then
          139         # open wikipedia always in a browser due to shitty .jpg links with a wrong mime-type.
          140         www "$text"
          141 elif match "$text" '\.(ogg|mp3)$' 'i'; then
          142         audio "$text"
          143 elif match "$text" '\.(jpg|png|jpeg)$' 'i'; then
          144         # download, view in image viewer, then delete.
          145         tempview image "$text"
          146 elif match "$text" '\.(pdf)$' 'i'; then
          147         # download, view in pdf viewer, then delete.
          148         tempview pdf "$text"
          149 elif match "$text" '\.(gifv)$' 'i'; then
          150         # replace gifv with mp4
          151         text=$(printf '%s' "$text" | sed 's@gifv$@mp4@')
          152         movie "$text"
          153 elif match "$text" 'https://docs\.google\.com/document/d' 'i'; then
          154         # google docs: export document to PDF.
          155         id=$(printf '%s' "$text" | sed -nE 's@.*https://docs\.google\.com/document/d/([^/]*).*@\1@p')
          156         if test -n "$id"; then
          157                 # download, view in pdf viewer, then delete.
          158                 text="https://docs.google.com/document/d/$id/export?format=pdf"
          159                 tempview pdf "$text"
          160         fi
          161 elif match "$text" '^(http|https)://imgur\.com/a/'; then
          162         # album viewer
          163         albumid="${text##*/}"
          164         tmp=$(mktemp)
          165         "$HOME/.config/scripts/plumb/imgur_album.sh" "$albumid" > "$tmp"
          166         st -e less "$tmp"
          167         rm -f "$tmp"
          168 elif match "$text" '^(http|https)://imgur\.com/'; then
          169         imgid="$(printf '%s' "$text" | sed -E 's@^(http|https)://imgur.com/@@')"
          170         if test x"$imgid" != x""; then
          171                 text="https://i.imgur.com/$imgid.jpg"
          172                 tempview image "$text"
          173         else
          174                 www "$text"
          175         fi
          176 elif match "$text" '://i\.imgur\.com/'; then
          177         # download, view in image viewer, then delete.
          178         tempview image "$text"
          179 elif match "$text" '^(http|https)://explosm\.net/comics/[0-9]+/'; then
          180         # grab direct comic image url.
          181         # TODO: make this some more generic og:image / og:video function? maybe using "xml2tsv".
          182         # see: https://ogp.me/
          183         url=$(fetch "$text" | sed -nE '/<meta property="og:image" content="([^"]*)\?.*">/{ s//\1/;p;q; }')
          184         test -n "$url" && tempview image "$url"
          185 elif match "$text" '^(http|https)://pastebin\.com/'; then
          186         # pastebin, always use raw url.
          187         id="${text##*/}"
          188         text="https://pastebin.com/raw/${id}"
          189 
          190         # download, view in less, then delete.
          191         tempview pastebin "$text"
          192 elif match "$text" '^(http|https)://(sprunge\.us|ix\.io|0x0\.st|termbin\.com)/'; then
          193         # sprunge.us / ix.io: pastebin-like services.
          194         # download, view in less, then delete.
          195         tempview pastebin "$text"
          196 elif match "$text" '^(http|https)://.*(\.youtube\.|youtu\.be)'; then
          197         movie "$text"
          198 elif match "$text" '^(http|https)://.*(\.?twitter\.com)/'; then
          199         url="${text}"
          200         url="${url#http://}"
          201         url="${url#https://}"
          202         url="${url#www.}"
          203         url="${url#twitter.com}"
          204         url="${url#/}"
          205         # replace with nitter instance.
          206         url="https://nitter.privacydev.net/${url}"
          207         www "$url"
          208 
          209         # old twitter status script
          210 #        tmp=$(mktemp)
          211 #        "$HOME/.config/scripts/plumb/twitter_status.sh" "$text" > "$tmp"
          212 #        st -e less "$tmp"
          213 #        rm -f "$tmp"
          214 elif match "$text" '^(http|https)://([^a-z]*\.)?twitch\.tv'; then
          215         movie "$text"
          216 elif match "$text" '^(http|https)://'; then
          217         www "$text"
          218 #elif match "$text" '^(magnet):?xt'; then
          219 #        # magnet link.
          220 #        rtorrent "$text"
          221 fi