someguy (3366B)
1 #!/bin/sh 2 # https://efe.kim/files/scripts/linkhandler 3 # Author: Efe Vural <efe@efe.kim> 4 # give -d flag for manual selection 5 # In my setup $TERMINAL=st, $PLAYER=mpv, $BROWSERCLI=w3m 6 7 geturl(){ 8 primary=$(xclip -o 2>/dev/null | sed '1!d') 9 clipboard=$(xclip -sel c -o 2>/dev/null | sed '1!d') 10 url=$(printf "%s\n%s\n%s" "$1" "$primary" "$clipboard"\ 11 | grep -E "^ *magnet:|^ *git://|^ *https?://" | sed '1!d;s/ *//g') 12 [ -z "$url" ] && notify-send "Not a link" && exit 13 } 14 15 tmpdl(){ tmpfile=$(mktemp /tmp/linkhandler-"$1".XXXXXX) 16 setsid "$TERMINAL" -n download -e curl -o "$tmpfile" "$url" ;} 17 torrentdl(){ setsid "$TORRENT" "$url" >/dev/null 2>&1 ;} 18 gitdl(){ setsid "$TERMINAL" -n download\ -e git clone "$url" ;} 19 imgview(){ tmpdl pic && setsid sxiv -qfabsf "$tmpfile" >/dev/null 2>&1 20 rm "$tmpfile" ;} 21 pdfview(){ tmpdl pdf && setsid zathura "$tmpfile" >/dev/null 2>&1 22 rm "$tmpfile" ;} 23 htmlview(){ setsid "$TERMINAL" -e "$BROWSERCLI" "$url" >/dev/null 2>&1 ;} 24 torview(){ setsid torsocks "$TERMINAL" -e "$BROWSERCLI" "$url" 25 >/dev/null 2>&1 ;} 26 videoplay(){ tmpdl vid && 27 setsid "$PLAYER" --no-terminal "$tmpfile" >/dev/null 2>&1 28 rm "$tmpfile" ;} 29 audioplay(){ tmpdl aud && 30 setsid "$TERMINAL" -e "$PLAYER" --no-video "$tmpfile" >/dev/null 2>&1 31 rm "$tmpfile" ;} 32 streamplay(){ notify-send "Starting stream" &\ 33 setsid "$PLAYER" --no-terminal "$url" >/dev/null 2>&1 || 34 notify-send "Stream failed" ;} 35 36 autohandler() { 37 case "$url" in 38 (*.pdf) pdfview & ;; 39 (*.mkv|*.webm|*.mp4|*.gifv) videoplay & ;; 40 (*.png|*.jpg|*.jpeg|*.jpe|*.gif) imgview & ;; 41 (*.mp3|*.flac|*.opus|*.ogg|*.mp3?source) audioplay & ;; 42 (*youtube.com*|*m.youtube.com*|*youtu.be*|*twitch.tv*|*hooktube.com*\ 43 |*bitchute.com*|*liveleak.com*|*crunchyroll.com*) streamplay & ;; 44 (git://*) gitdl & ;; 45 (magnet:*) torrentdl & ;; 46 (http://*.onion|http://*.onion/*) torview & ;; 47 (http://*|https://*) htmlview & ;; 48 esac 49 } 50 51 dmenuhandler(){ 52 visual=$(echo "$url" | sed -E '/^.{30}/s/^(.{20}).*(.{7})$/\1...\2/') 53 options="video\naudio\ndownload\nimage\npdf\nbrowser\nbrowsergui\ 54 \nbrowsercli\ntorrent\nbookmark\nfeed\ncomments\ntor\nytinfo\ 55 \nsubscribe\ncopy\nstream\ngitclone" 56 selection=$(echo "$options" | dmenu -i -p "Open '$visual':") 57 case "$selection" in 58 (video) videoplay & ;; 59 (audio) audioplay & ;; 60 (download) cd ~/Downloads/ 61 setsid "$TERMINAL" -n download -e curl -OL "$url" || 62 setsid "$TERMINAL" -n download -e curl -o\ 63 "$(echo "$url" | sed 's_.*/\(.*\)/*$_\1.html_')" "$url" & ;; 64 (image) imgview & ;; 65 (document) pdfview & ;; 66 (browsergui) setsid "$BROWSERGUI" "$url" & ;; 67 (browsercli) htmlview & ;; 68 (torrent) torrentdl & ;; 69 (bookmark) echo "$url" >> "$HOME/.bookmarks" & ;; 70 (feed) echo "$url" >> "$HOME/.newsbeuter/urls" & ;; 71 (tor) torview & ;; 72 (comments) setsid "$TERMINAL" youtube-viewer -c "$url" & ;; 73 (ytinfo) setsid "$TERMINAL" youtube-viewer -i "$url" & ;; 74 (subscribe) chanid=$(youtube-viewer --no-interactive --extract\ 75 '*CHANNELID* #*AUTHOR*' "$url") 76 [ "$(echo "$chanid" | sed -n $=)" = "1" ] || exit 77 echo "https://www.youtube.com/feeds/videos.xml?channel_id=$chanid"\ 78 >> "$HOME/.newsbeuter/urls" & ;; 79 (copy) echo "$url" | xclip -i -f | xclip -sel c -i ;; 80 (stream) streamplay & ;; 81 (gitclone) gitdl & ;; 82 (*) $selection "$url" ;; 83 esac 84 } 85 86 if [ "$1" = "-d" ] 87 then shift 1 ; geturl "$1" ; dmenuhandler >/dev/null 2>&1 & 88 else geturl "$1" ; autohandler >/dev/null 2>&1 & 89 fi 90 91 92