-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb-xdg-open.sh
executable file
·174 lines (150 loc) · 4.26 KB
/
web-xdg-open.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
#!/bin/bash
set -o pipefail
[[ ! "$1" ]] &&
echo "Usage: $0 link" &&
exit 1
readonly CONFIG="${XDG_CONFIG_HOME:-$HOME/.config}/wxorc"
#shellcheck disable=2155
readonly APP_NAME="$(basename "$0")"
## ============== PUBLIC OVERRIDABLE METHODS ================
notify() {
notify-send -a "$APP_NAME" "$@"
true
}
image() {
dl "$1" >"$TEMPFILE" && xdg-open "$TEMPFILE"
}
video() {
if command -V mpv &>/dev/null; then
notify 'Playing in mpv' "$1" -u low
mpv "$1" || {
notify 'Retrying in mpv' "$1" -u low
mpv "$1"
}
else
notify 'Downloading' "$1"
if ! error=$(youtube-dl --no-color "$1" --output "$TEMPFILE" 2>&1); then
notify 'Download failed' "$error" -u critical
return 1
else
xdg-open "$TEMPFILE"
fi
fi
}
text() {
dl "$1" >"$TEMPFILE" && xdg-open "$TEMPFILE"
}
matematica() {
text "$@"
}
git() {
dl "$1" >"$TEMPFILE" && xdg-open "$TEMPFILE"
}
gif() {
if command -V mpv &>/dev/null; then
mpv --loop-file "$1"
else
dl "$1" && xdg-open "$TEMPFILE"
fi
}
pdf() {
dl "$1" >"$TEMPFILE" && xdg-open "$TEMPFILE"
}
clipboard() {
if command -V xclip &>/dev/null; then
dl "$1" | xclip && notify 'Key coppied to clipboard' -u low
else
notify 'xclip not installed' \
'Install it or override the behaviour of clipboard' \
-u low
fi
}
archive() {
false
#dl "$1" >"$TEMPFILE" && xdg-open "$TEMPFILE"
}
fallback() {
notify 'Using fallback' -u low
if [[ "$BROWSER" ]]; then
"$BROWSER" "$1" &
disown
else
notify 'Browser not set' \
"Please set the \$BROWSER enviroment variable" \
-u critical
false
fi
exit
}
dl() {
__bash_not_smart_enough() {
local error
if ! error=$(curl -# --fail "$1" 3>&2 2>&1 1>&3); then
notify 'Download failed' \
"$(tail -1 <<<"$error" | sed -E 's/curl:\s*\([0-9]*\)//g')" \
-u critical
return 1
fi
}
__bash_not_smart_enough "$@" 2>&1
}
other() {
false
}
__temp_file() {
TEMPFILE=$(mktemp) || return 1
if [[ "$1" ]]; then
name="$(basename "$1")"
[[ "$name" = */* ]] && return 0
mv "$TEMPFILE" "$TEMPFILE-$name" || return 1
TEMPFILE="$TEMPFILE-$name"
fi
}
## ============== PRIVATE METHODS ================
__cleanup() {
sleep 30
[[ "$TEMPFILE" ]] && rm -f "$TEMPFILE"
:
}
trap __cleanup EXIT
readonly target="$1"
__try() {
clean_link=${target,,}
clean_link=${clean_link%\?*}
case "$clean_link" in
*github*blob*)
raw_link="${target//github/raw.githubusercontent}"
raw_link="${raw_link//blob\//}"
git "$raw_link"
;;
*.txt | *.sh | *.c | *.cpp | *.h | *.hpp | *.rs | *.spell | *.tool | *.hs | *.conf | *.tex | *.log | *.lhs | *.java | *.py | *.awk | *.pl | *.cs | *.css | *.js | *.scss | *.lua | *.bash | *makefile | *.md | *.yml | *.yaml)
text "$target"
;;
*.nb)
matematica "$target"
;;
*.png | *.jpg | *.jpeg | *.bmp | *.tiff | *.svg | *.ico | *.img | *.webp)
image "$target"
;;
*.gif)
gif "$target"
;;
*.pdf | *.djvu)
pdf "$target"
;;
*.tar | *.bz2 | *.rar | *.Z | *.7z | *.gz | *.xz | *.zip)
archive "$target"
;;
*bilibili.com* | *tiktok.com* | *youtube.com* | *youtu.be* | *vimeo* | *clips.twitch.tv* | *soundcloud.com* | https://v.redd.it* | https://t.co* | *medal.tv* | *streamable.com* | *.avi | *.webm | *.mp4 | *.mp3 | *.wav | *.flac | *.midi | *.dvdrip | *.cam | *.mkv | *.mov | *.mpeg | *.flv | *.mpg | *.mp2 | *.mpv | *.m4p | *.m4v | *.wmv | *.qt | *.swf | *.avchd | *.m4a | *.ogg | *.wma | *.amv | *.mpa | *.ra | *.rax | *.raw | *.smf | *.snd | *.sng | *.swa | *.hma | *.aac | *.ac3 | *.eac3 | *.Vorbis | *.pcm)
video "$target"
;;
*.pub)
clipboard "$target"
;;
*) other "$target" "$clean_link" ;;
esac
}
#shellcheck disable=1090
. "$CONFIG"
__temp_file "$1" || fallback "$target"
__try || fallback "$target"