Last active
November 15, 2017 16:03
-
-
Save tastytea/cbe1ce2e989a13ee8a125534c66656e3 to your computer and use it in GitHub Desktop.
Open URLs based on regular expressions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
# Open URLs based on regular expressions | |
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;gnome-mpv;kodi;${default_cmd}" | |
"\.(ogg|flac|opus|m4a|wav|mp3)$" "vlc;gnome-mpv;kodi;${default_cmd}" | |
"^https?://(www\.)?youtu(\.be|be\.com)/([^/]*v=)?[a-zA-Z0-9\-\_]*([\?&].*)?" "gnome-mpv;vlc;kodi;${default_cmd}" | |
"^https://media.ccc.de/v/[^/]*$" "gnome-mpv;kodi;${default_cmd}" | |
"^https?://(vivo.sx|streamcloud.eu|shared.sx|openload.co|powerwatch.pw|cloudtime.to|auroravid.to|vidto.me)/" "gnome-mpv;kodi;${default_cmd}" | |
"^https?://www.ndr.de/fernsehen/sendungen/[^/]*/[^/]*[0-9]*.html" "gnome-mpv;kodi;${default_cmd}" | |
#"https?://daserste.ndr.de/[^/]*/[^/]*-vom-[0-9]{8},[^/]*[0-9]*.html" "gnome-mpv;kodi;${default_cmd}" | |
"^https?://www.ardmediathek.de/tv/.*/Video\?" "gnome-mpv;kodi;${default_cmd}" | |
"^https?://www.zdf.de/[^/]*/[^/]*/[^/]*-10[0-9].html" "gnome-mpv;kodi;${default_cmd}" | |
) | |
match=0 | |
for url in ${@}; do | |
for regex cmd in ${(kv)assignments}; do | |
if [[ "${url}" -regex-match "${regex}" ]]; then | |
if [ "$(basename ${0})" = "urlhandler-gui" ]; 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment