about summary refs log tree commit diff stats
path: root/bin/lh
blob: e6625892ddc7a652564a0d8b7b25eaf1edb7ed0e (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
#!/bin/sh
# lh: the Link Handler
# takes one path or URL as its argument, launches the appropriate program for this.

# if $1 isn't provided, spawn $BROWSER and exit. this allows lh to be used as a pseudo-browser.
[ -z "$1" ] && exec "$BROWSER"
#from pure-sh-bible
split() {
	set -f
	old_ifs=$IFS
	IFS=$2
	set -- $1
	printf '%s\n' "$@"
	IFS=$old_ifs
	set +f
}

# handle redirects. tr removes control characters, so the case statement below works as expected.
URL=$(curl -IL "$1"|grep '^location: '|tail -1|sed 's/location: //'|tr -d '[:cntrl:]')
# if there were no redirects, just set URL to the first argument
[ -z "$URL" ] && URL="$1"

echo $URL
case "$URL" in
	gemini://*|gopher://*)
		$TERMINAL -e bombadillo "$URL" ;;
	*mkv|*webm|*mp4)
		mpv "$URL" >/dev/null 2>&1 & ;;
	*youtube.com/watch*|*youtube.com/playlist*|*youtu.be*|*hooktube.com*|*bitchute.com*|*twitch.tv/videos/*|*twitch.tv/*/v/*)
		mpv --ytdl "$URL" >/dev/null 2>&1 & ;;
	*twitch.tv/*)
		STREAMQUAL="$(split "$(streamlink "$URL"|grep 'Available streams')" ", "|\
			grep -E "[0-9]"|\
			dmenu -p "choose stream quality for $URL")"
		streamlink -p mpv $URL $STREAMQUAL >/dev/null 2>&1 & ;;
	*png|*jpg|*jpe|*jpeg|*gif)
		IMGPATH="/tmp/$(echo "$URL"|sed "s/.*\\///")"
		curl -sL "$URL" >"$IMGPATH"&&sxiv -a "$IMGPATH">/dev/null 2>&1 & ;;
	*mp3|*m4a|*flac|*aiff|*opus|*ogg|*mp3?source*)
		mpv "$URL" >/dev/null 2>&1 & ;;
	*epub|*pdf|*djvu)
		BOOKPATH="/tmp/$(echo "$URL"|sed 's/.*\///')"
		if [ -n "$READER" ]; then
			curl -sL "$URL" >"$BOOKPATH"&&$READER "$BOOKPATH">/dev/null 2>&1 &
		else
			curl -sL "$URL" >"$BOOKPATH" &
		fi ;;
	*)
		if [ -f "$URL" ]; then
			$TERMINAL -e "${EDITOR:-vi} $URL"
		else
			$BROWSER "$URL" >/dev/null 2>&1 &
		fi ;;
esac