From 191c1359c5a23606410d5a513569aeb964afcad9 Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Fri, 26 Jul 2024 22:17:54 +0530 Subject: linkhandler: refactor --- .local/bin/linkhandler | 84 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 30 deletions(-) (limited to '.local/bin/linkhandler') 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 -- cgit v1.2.3