scripts/urlhandler

55 lines
2.1 KiB
Bash
Executable File

#!/bin/zsh
# Open URLs based on regular expressions.
# Format of assignments: "Regular expression", "program1;program2;…"
# If this script is named urlhandler-gui, it shows a GUI using zenity. If not,
# the first program in the list is used.
if [ -z "${1}" ]; then
print "usage: $(basename ${0}) URL [URL] ..." >&2
exit 1
fi
zmodload zsh/regex
default_cmd="firefox"
assignments=(
"\.(mp4|m4v|mkv|avi|webm|flv|xvid|ogv|theora|mov)$" "vlc;mpv;kodi;${default_cmd}"
"\.(ogg|flac|opus|m4a|wav|mp3)$" "vlc;mpv;kodi;${default_cmd}"
"^https://media.ccc.de/v/[^/]*$" "mpv;kodi;${default_cmd}"
"^https?://www.ardmediathek.de/tv/.*/Video\?" "mpv;kodi;${default_cmd}"
"^https?://www.zdf.de/[^/]*/[^/]*/[^/]*-10[0-9].html" "mpv;kodi;${default_cmd}"
"^https?://www.ndr.de/fernsehen/sendungen/[^/]*/[^/]*[0-9]*.html" "mpv;kodi;${default_cmd}"
"^https?://(www\.)?youtu(\.be|be\.com)/([^/]*v=)?[a-zA-Z0-9\-\_]*([\?&].*)?" "mpv;kodi;${default_cmd}"
"^https?://(www\.)?invidio.us/([^/]*v=)?[a-zA-Z0-9\-\_]*([\?&].*)?" "mpv;kodi;${default_cmd}"
"^(gemini|gopher)://" "kristall;castor;"
)
match=0
for url in ${@}; do
for regex cmd in ${(kv)assignments}; do
if [[ "${url}" -regex-match "${regex}" ]]; then
if [ "$(basename ${0})" = "urlhandler-gui" ] &&
[[ $(tr -d -c ';' <<<${cmd} | wc -c) -gt 1 ]]; then
# Show a dialogue with commands to choose from
choice=$(zenity --list --column="commands" --hide-header ${(s:;:)cmd})
if [ -n "${choice}" ]; then
${choice} ${url}
fi
else
# Use first entry in list of commands
${${(s:;:)cmd}[1]} "${url}"
fi
match=1
break
fi
done
if [ ${match} -eq 0 ]; then
${default_cmd} "${url}"
else
match=0
fi
done