summaryrefslogblamecommitdiff
path: root/.local/bin/linkhandler
blob: 5dd42e745f68f938ebb2d49a5fa0ad3dc8b4f3cf (plain) (tree)
1
2
3
4
                   
 
        
                                             






































                                                             

 


        
 




                                                               
 




                                                                        
          

                          

          
                             

          
#!/usr/bin/env bash

note() {
	command -v notify-send > /dev/null &&
		notify-send "    linkhandler" "$1"
}

die() {
	printf "\033[31;1merr: %b\033[0m\n" "$1"
	note "$1"
	exit "${2:-1}"
}

curl_cmd() {
	curl "$@" \
		--silent \
		--location \
		--compressed
}

stream() {
	note "streaming ${2:-media}"
	exec mpv --force-window -quiet "$1" > /dev/null 2>&1
}

download() {
	note "downloading ${2:-$1}"

	out="/tmp/$(echo "$1" | sed "s/.*\///;s/%20/ /g")"
	curl_cmd  "$1" -o "$out" || die "download failed: $1"
	xdg-open "$out"
	rm -rf "$out"
}

domain_switch() {
	case "${1%/}" in
	*youtube.com/*|*youtu.be/*)
		stream "$1"
		;;
	*)
		exec firefox "$1"
		;;
	esac
}

########
# MAIN #
########

if [ -z "$1" ]; then
	exec firefox
elif echo "$1" | grep -Eqx '^https?://[^/]*\.onion(/.*)?'; then
	exec tor-browser "$1"
fi

type="$(curl_cmd "$1" --write-out '%header{content-type}' -o /dev/null)"
case "$type" in
video/*) ;&
audio/*)
	stream "$1" "$type"
	;;
text/html*)
	domain_switch "$1"
	;;
*)
	download "$1" "$type"
	;;
esac