summaryrefslogtreecommitdiff
path: root/.local/bin/linkhandler
blob: 5dd42e745f68f938ebb2d49a5fa0ad3dc8b4f3cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/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