dotfiles/.local/bin/urlhandler

61 lines
2.6 KiB
Bash
Executable File

#!/usr/bin/env zsh
# Open URLs based on perl compatible regular expressions. Several commands can
# be specified with ; as separator. If the URL doesn't match anything or the
# commands are not found, use ${default_cmd}.
setopt LOCAL_OPTIONS ERR_RETURN NO_UNSET PIPE_FAIL
zmodload zsh/zutil
local -a o_help=()
zparseopts -D -K -- h=o_help
if [[ ${#o_help} -ne 0 || ! -v 1 ]]; then
local ret=$(( ${#o_help} ^ 1 ))
print -u $(( 1 + ${ret} )) "usage: ${0} [-h] <URL> …"
return ${ret}
fi
local -a urls=(${@})
autoload -U readwwwlog openwwwimg
zmodload zsh/pcre
local default_cmd="firefox"
local mpv="mpv --force-window=yes"
local -A assignments=(
'\.(mp4|m4v|mkv|avi|webm|flv|xvid|ogv|theora|mov|wmv)(\?.+)?$' "${mpv};vlc"
'\.(ogg|flac|opus|m4a|wav|mp3|mid|aac|wma)(\?.+)?$' "${mpv};vlc"
'^https://media\.ccc\.de/v/[^/]*$' "${mpv}"
'^https?://(www\.)?youtu(\.be|be\.com)/' "${mpv}"
'^https?://(invidious|piped|tube)\.[^/]+/watch\?v=' "${mpv}"
'^https://yewtu\.be/watch\?v=' "${mpv}"
'^https://v.redd.it/' "${mpv}"
'^(gemini|gopher)://' "kristall"
'\.(log|txt)$' "readwwwlog"
'^https?://(www\.)?(dpaste|irccloud|termbin)\.com/' "readwwwlog"
'^https?://(www\.)?pastebin\.[^/]+/' "readwwwlog"
'^https?://bpa\.st/' "readwwwlog"
'^https?://sprunge\.us/' "readwwwlog"
'^https?://paste\.debian\.net/' "readwwwlog"
'^https?://paste\.centos\.org/' "readwwwlog"
'^https?://paste\.ee/' "readwwwlog"
'^https?://ix\.io/' "readwwwlog"
'^https?://0x0.st/.+\.txt$' "readwwwlog"
'\.(webm|png|jpe?g|gif|svg|bmp|xcf)(\?.+)?$' "openwwwimg"
'(^magnet:|\.torrent$)' "transmission-remote-gtk"
)
local selected_cmd=${default_cmd}
for regex cmds in ${(kv)assignments}; do
if [[ ${urls[1]} -pcre-match ${regex} ]]; then
for cmd in ${(s/;/)cmds}; do
if type ${cmd%% *} >& -; then
selected_cmd=${cmd}
break
fi
done
break
fi
done
${(@s/ /)selected_cmd} ${urls}