summaryrefslogtreecommitdiff
path: root/.local/bin/linkhandler
diff options
context:
space:
mode:
authorsinanmohd <sinan@sinanmohd.com>2024-07-26 22:17:54 +0530
committersinanmohd <sinan@sinanmohd.com>2024-07-26 22:17:54 +0530
commit191c1359c5a23606410d5a513569aeb964afcad9 (patch)
tree2984682314410ad23df0e5c0d5892ca8517652a9 /.local/bin/linkhandler
parent8d4c41d685fa56c06145a70abadf571e002b8a23 (diff)
linkhandler: refactorHEADmaster
Diffstat (limited to '.local/bin/linkhandler')
-rwxr-xr-x.local/bin/linkhandler84
1 files changed, 54 insertions, 30 deletions
diff --git a/.local/bin/linkhandler b/.local/bin/linkhandler
index c703eba..5dd42e7 100755
--- a/.local/bin/linkhandler
+++ b/.local/bin/linkhandler
@@ -1,44 +1,68 @@
-#!/bin/sh
+#!/usr/bin/env bash
-note()
-{
+note() {
command -v notify-send > /dev/null &&
- notify-send " linkhandler" "$1"
+ notify-send " linkhandler" "$1"
+}
+
+die() {
+ printf "\033[31;1merr: %b\033[0m\n" "$1"
+ note "$1"
+ exit "${2:-1}"
+}
+
+curl_cmd() {
+ curl "$@" \
+ --silent \
+ --location \
+ --compressed
+}
+
+stream() {
+ note "streaming ${2:-media}"
+ exec mpv --force-window -quiet "$1" > /dev/null 2>&1
+}
+
+download() {
+ note "downloading ${2:-$1}"
+
+ out="/tmp/$(echo "$1" | sed "s/.*\///;s/%20/ /g")"
+ curl_cmd "$1" -o "$out" || die "download failed: $1"
+ xdg-open "$out"
+ rm -rf "$out"
+}
+
+domain_switch() {
+ case "${1%/}" in
+ *youtube.com/*|*youtu.be/*)
+ stream "$1"
+ ;;
+ *)
+ exec firefox "$1"
+ ;;
+ esac
}
########
# MAIN #
########
-[ -z "$1" ] &&
- exec "${BROWSER:-firefox}"
+if [ -z "$1" ]; then
+ exec firefox
+elif echo "$1" | grep -Eqx '^https?://[^/]*\.onion(/.*)?'; then
+ exec tor-browser "$1"
+fi
-case "${1%/}" in
-*mp3|*flac|*opus|*mkv|*webm|*mp4|*youtube.com/watch*|\
-*youtube.com/playlist*|*youtu.be*|*bitchute.com*|*videos.lukesmith.xyz*|\
-*piped.kavin.rocks*|*inv.vern.cc*|*yewtu.be*)
- note "streaming media"
- exec mpv -quiet "$1" > /dev/null 2>&1
- ;;
-*.png|*.jpg|*.jpeg|*.gif|*.webp|*.pdf|*.cbz|*.cbr)
- note "processing file"
- curl -sL "$1" > "/tmp/$(echo "$1" | sed "s/.*\///;s/%20/ /g")" &&
- exec xdg-open "/tmp/$(echo "$1" | sed "s/.*\///;s/%20/ /g")" > /dev/null 2>&1 &
+type="$(curl_cmd "$1" --write-out '%header{content-type}' -o /dev/null)"
+case "$type" in
+video/*) ;&
+audio/*)
+ stream "$1" "$type"
;;
-*.c|*.cpp|*.sh|*.txt|*.java|*.nix|*.lua|*.py|*.toml)
- note "processing text"
- curl -sL "$1" > "/tmp/$(echo "$1" | sed "s/.*\///;s/%20/ /g")" &&
- exec xdg-open "/tmp/$(echo "$1" | sed "s/.*\///;s/%20/ /g")" > /dev/null 2>&1 &
+text/html*)
+ domain_switch "$1"
;;
*)
- if echo "$1" | grep -Eqx '^https?://[^/]*\.onion(/.*)?'; then
- if ! command -v tor-browser > /dev/null; then
- note "tor browser not installed"
- exit 1
- fi
- exec tor-browser "$1" >/dev/null 2>&1
- else
- exec "$BROWSER" "$1" >/dev/null 2>&1
- fi
+ download "$1" "$type"
;;
esac