dotfiles

configuration files for shell, text editor, graphical environment, etc.
git clone git://src.adamsgaard.dk/dotfiles
Log | Files | Refs | README | LICENSE Back to index

linkhandler (3112B)


      1 #!/bin/sh
      2 mkdir -p ~/tmp
      3 cd ~/tmp || exit 42
      4 
      5 die() {
      6 	printf '%s\n' "$1" >&2
      7 	exit 1
      8 }
      9 
     10 handle_url() {
     11 	if [ -f "$1" ]; then
     12 		setsid "$TERMINAL" -e "$EDITOR $1"
     13 		exit 0;
     14 	fi
     15 
     16 	url="$1"
     17 
     18 	# strip redirects
     19 	case "$url" in
     20 		*scholar.google.*)
     21 			url="$(echo "$url" | sed 's/.*scholar_url?url\=//;s/&hl\=.*//')";;
     22 	esac
     23 			
     24 
     25 	action="undefined"
     26 	case "$url" in
     27 		*.png|*.PNG|*.jpg|*.JPG|*.jpeg|*.JPEG|*.tif|*.TIF|*.bmp|*.BMP|*.pdf|*.PDF|*.mp4|*.mkv|*.webm)
     28 			notify-send "${0##*/}" "download + xdg-open"
     29 			action="xdg-open" ;;
     30 		*youtube.com*|*youtu.be*)
     31 			notify-send "${0##*/}" "youtube-dl"
     32 			action="youtube-dl" ;;
     33 		*smbc-comics.com*|*xkcd.com*|*jspowerhour.com*)
     34 			notify-send "${0##*/}" "comic"
     35 			action="comic" ;;
     36 		10.[0-9]*\/*)
     37 			url="https://doi.org/$url"
     38 			notify-send "${0##*/}" "open as doi"
     39 			action="open" ;;
     40 		gopher://*)
     41 			nohup $TERMINAL sacc "$url" >/dev/null 2>&1 &
     42 			;;
     43 		*)
     44 			action="$(printf "open\nopen (w3m)\nopen (tbb)\nxclip\nducksearch\nmap\nbookmark\ndownload\nhtml to pdf\nsci-hub\nscholarref\nmpv\nxdg-open\nyoutube-dl\nyoutube-dl-music\ndefine" | \
     45 				dmenu -i -p "$(echo "$url" | sed 's/.*:\/\///' | cut -c-80)")";;
     46 	esac
     47 
     48 	case "$action" in
     49 		"open")
     50 			nohup xdg-open "$url" >/dev/null 2>&1 & ;;
     51 		"open (w3m)")
     52 			nohup $TERMINAL w3m "$url" >/dev/null 2>&1 & ;;
     53 		"open (tbb)")
     54 			nohup tor-browser "$url" >/dev/null 2>&1 & ;;
     55 		"xclip")
     56 			printf "%s" "$url" | xclip
     57 			printf "%s" "$url" | xclip -selection clipboard ;;
     58 		"bookmark")
     59 			description="$(echo "$(date), $hostname" | \
     60 				dmenu -p "description:")"
     61 			nohup $TERMINAL bookmark "$url" "$description" >/dev/null 2>&1 & ;;
     62 		"comic")
     63 			nohup comic "$url" >/dev/null 2>&1 & ;;
     64 		"download")
     65 			if type -v curl >/dev/null 2>&1; then
     66 				echo "curl -sLO '$url' >/dev/null" | at now
     67 			elif type -v wget >/dev/null 2>&1; then
     68 				echo "wget '$url' >/dev/null" | at now
     69 			else
     70 				die 'Error: no download program found'
     71 			fi;;
     72 		"html to pdf")
     73 			out="$HOME/tmp/html_to_pdf_$(date +'%F-%T')".pdf && \
     74 				wkhtmltopdf "$url" "$out" && \
     75 				notify-send "$(basename $out) complete" ;;
     76 		"ducksearch")
     77 			ducksearch "$url";;
     78 		"map")
     79 			ducksearch "!map $url";;
     80 		"sci-hub")
     81 			echo "shdl --tor-socks --notify --open --reference '$url' >/dev/null" \
     82 				| at now;;
     83 		"scholarref")
     84 			echo "getdoi '$url' | getref --notify >> '$BIB'" | at now;;
     85 		"mpv")
     86 			nohup mpv -quiet "$url" >/dev/null 2>&1 & ;;
     87 		"xdg-open")
     88 			mkdir -p open && cd open
     89 			f="$(curl -s --write-out "%{filename_effective}\n" -OL "$url")"
     90 			nohup xdg-open "$f" >/dev/null 2>&1 & ;;
     91 		"youtube-dl")
     92 			echo "youtube-dl '$url' >/dev/null" | at now;;
     93 		"youtube-dl-music")
     94 			nohup $TERMINAL -e youtube-dl-music "$url" >/dev/null 2>&1 & ;;
     95 		"define")
     96 			define -g "$url\n";;
     97 		*)
     98 			notify-send "${0##/*}" "Error: Action not understood"
     99 			exit 1 ;;
    100 	esac
    101 }
    102 
    103 if [ $# -lt 1 ]; then
    104 	handle_url "$(cat)"
    105 else
    106 	if [ "$1" = "-c" ]; then
    107 		if [ $# -eq 2 ] && [ "$2" = "clipboard" ]; then
    108 			handle_url "$(xclip -o -selection clipboard)"
    109 		else
    110 			handle_url "$(xclip -o)"
    111 		fi
    112 	else
    113 		for url in "$@"; do
    114 			handle_url "$url"
    115 		done
    116 	fi
    117 fi