-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlink_handler.sh
executable file
·175 lines (161 loc) · 4.01 KB
/
link_handler.sh
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/bin/sh
# path: /home/klassiker/.local/share/repos/link-handler/link_handler.sh
# author: klassiker [mrdotx]
# github: https://github.com/mrdotx/link-handler
# date: 2024-08-29T12:16:09+0200
# config
web="w3m.sh"
edit="$TERMINAL -e $EDITOR"
podcast="tsp $TERMINAL -e mpv"
video="tsp mpv --terminal=no"
iptv="mpv --terminal=no --script-opts=menu_playlist=1 --force-window"
picture="nsxiv --quiet --animate --scale-mode w"
document="zathura"
download="$TERMINAL -e terminal_wrapper.sh aria2c.sh"
tmp_download="curl -fsS "
tmp_readable="python -W ignore -m readability.readability -u"
# help
script=$(basename "$0")
help="$script [-h/--help] -- script to open links on basis of extensions
Usage:
$script [--clipboard/--readable] [uri]
Settings:
[--clipboard] = open uri from clipboard
[--readable] = make the html content readable with python readability-lxml
(Mozilla's Readability library)
[uri] = uniform resource identifier
Examples:
$script suckless.org
$script https://raw.githubusercontent.com/mrdotx/dotfiles/master/screenshot.png
$script --clipboard
$script --readable suckless.org
Config:
web = $web
edit = $edit
podcast = $podcast
video = $video
iptv = $iptv
picture = $picture
document = $document
download = $download
tmp_download = $tmp_download
tmp_readable = $tmp_readable"
case "$1" in
-h | --help | '')
printf "%s\n" "$help"
[ -z "$1" ] \
&& exit 1
exit 0
;;
--clipboard)
[ "$(command -v "xsel")" ] \
&& uri="$(xsel -n -o -b)"
;;
*)
uri="$1"
;;
esac
uri_lower="$(printf "%s" "$uri" | tr '[:upper:]' '[:lower:]')"
# notifications
notify() {
notify-send \
-u low \
"link handler - $1" \
"$2"
}
# open with/-out tmp file or readable
open() {
open_tmp() {
tmp_file=$(mktemp -t link_handler.XXXXXX --suffix=".$1")
$2 "$3" > "$tmp_file" \
&& $4 "$tmp_file" \
&& rm -rf "$tmp_file"
}
case "$1" in
"--readable")
open_tmp "html" "$tmp_readable" "$2" "$web"
;;
"--tmp")
open_tmp "${uri_lower##*.}" "$tmp_download" "$uri" "$2"
;;
*)
$1 "$uri"
;;
esac
}
# main
case "$uri_lower" in
--readable)
[ -n "$2" ] \
&& notify "open link readable" "$2"
open "$uri_lower" "$2"
;;
*.mp3 \
| *.ogg \
| *.flac \
| *.opus)
notify "add audio to taskspooler" "$uri"
open "$podcast"
;;
*.m3u \
| *.m3u8)
notify "open stream" "$uri"
open "$iptv"
;;
*.mkv \
| *.mp4 \
| *.webm \
| rtsp://* \
| *youtube.com/watch* \
| *youtube.com/playlist* \
| *youtu.be*)
notify "add video to taskspooler" "$uri"
open "$video"
;;
*.jpg \
| *.jpe \
| *.jpeg \
| *.png \
| *.gif \
| *.webp)
notify "open picture" "$uri"
open --tmp "$picture" &
;;
*.pdf \
| *.ps \
| *.djvu \
| *.epub \
| *.cbr \
| *.cbz)
notify "open document" "$uri"
open --tmp "$document" &
;;
*.torrent \
| 'magnet\:'* \
| *.metalink \
| *.iso \
| *.img \
| *.bin \
| *.tar \
| *.tar.bz2 \
| *.tbz2 \
| *.tar.gz \
| *.tgz \
| *.tar.xz \
| *.txz \
| *.zip \
| *.7z \
| *.rar)
notify "download file" "$uri"
open "$download"
;;
*)
if [ -f "$uri" ]; then
notify "edit file" "$uri"
open "$edit"
else
notify "open link" "$uri"
open "$web"
fi
;;
esac