dotfiles

configuration files for shell, text editor, graphical environment, etc.
git clone git://src.adamsgaard.dk/dotfiles
Log | Files | Refs | README | LICENSE Back to index

commit 2432521659ce9e28ca3328daff25aa4a23c7c1ee
parent a7a63892907557d1c969ed88f3e0a8c2f14ee43b
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date:   Wed, 13 Nov 2019 10:28:32 +0100

Strip google scholar redirects from URLs

Diffstat:
M.local/bin/linkhandler | 60+++++++++++++++++++++++++++++++++++++++++-------------------
1 file changed, 41 insertions(+), 19 deletions(-)

diff --git a/.local/bin/linkhandler b/.local/bin/linkhandler @@ -2,16 +2,30 @@ mkdir -p ~/tmp cd ~/tmp || exit 42 +die() { + printf '%s\n' "$1" >&2 + exit 1 +} + handle_url() { if [ -f "$1" ]; then setsid "$TERMINAL" -e "$EDITOR $1" exit 0; fi + url="$1" + + # strip redirects + case "$url" in + *scholar.google.*) + url="$(echo "$url" | sed 's/.*scholar_url?url\=//;s/&hl\=.*//')";; + esac + + action="undefined" - case "$1" in + case "$url" in *.png|*.PNG|*.jpg|*.JPG|*.jpeg|*.JPEG|*.tif|*.TIF|*.bmp|*.BMP|*.pdf|*.PDF) - notify-send "linkhandler" "curl + xdg-open" + notify-send "linkhandler" "download + xdg-open" action="xdg-open" ;; *youtube.com*|*youtu.be*) notify-send "linkhandler" "youtube-dl" @@ -20,45 +34,53 @@ handle_url() { notify-send "linkhandler" "comic" action="comic" ;; *) - action="$(printf "open\nopen (tbb)\nxclip\nbookmark\ncurl\nhtml to pdf\nsci-hub\nadd bibref\nmpv\nxdg-open\nyoutube-dl\nyoutube-dl-music" | \ - dmenu -i -p "$1")";; + action="$(printf "open\nopen (tbb)\nxclip\nbookmark\ndownload\nhtml to pdf\nsci-hub\nadd bibref\nmpv\nxdg-open\nyoutube-dl\nyoutube-dl-music" | \ + dmenu -i -p "$url")";; esac case "$action" in "open") - nohup xdg-open "$1" >/dev/null 2>&1 & ;; + nohup xdg-open "$url" >/dev/null 2>&1 & ;; "open (tbb)") - nohup tor-browser "$1" >/dev/null 2>&1 & ;; + nohup tor-browser "$url" >/dev/null 2>&1 & ;; "xclip") - printf "%s" "$1" | xclip - printf "%s" "$1" | xclip -selection clipboard ;; + printf "%s" "$url" | xclip + printf "%s" "$url" | xclip -selection clipboard ;; "bookmark") description="$(echo "$(date), $hostname" | \ dmenu -p "description:")" - nohup $TERMINAL bookmark "$1" "$description" >/dev/null 2>&1 & ;; + nohup $TERMINAL bookmark "$url" "$description" >/dev/null 2>&1 & ;; "comic") - nohup comic "$1" >/dev/null 2>&1 & ;; - "curl") - echo "curl -LO '$1' >/dev/null" | at now;; + nohup comic "$url" >/dev/null 2>&1 & ;; + "download") + if type -v hurl >/dev/null 2>&1; then + echo "hurl '$url' >/dev/null" | at now;; + elif type -v hurl >/dev/null 2>&1; then + echo "curl -LO '$url' >/dev/null" | at now;; + elif type -v wget >/dev/null 2>&1; then + echo "wget '$url' >/dev/null" | at now;; + else + die 'Error: no download program found' + fi "html to pdf") out="$HOME/tmp/html_to_pdf_$(date +'%F-%T')".pdf && \ - wkhtmltopdf "$1" "$out" && \ + wkhtmltopdf "$url" "$out" && \ notify-send "$(basename $out) complete" ;; "sci-hub") - echo "shdl --tor-socks --notify --open --reference '$1' >/dev/null" \ + echo "shdl --tor-socks --notify --open --reference '$url' >/dev/null" \ | at now;; "add bibref") - echo "getdoi '$1' | getref --notify >> '$BIB'" | at now;; + echo "getdoi '$url' | getref --notify >> '$BIB'" | at now;; "mpv") - nohup mpv -quiet "$1" >/dev/null 2>&1 & ;; + nohup mpv -quiet "$url" >/dev/null 2>&1 & ;; "xdg-open") - f="$(curl -s --write-out "%{filename_effective}\n" -OL "$1")" + f="$(curl -s --write-out "%{filename_effective}\n" -OL "$url")" nohup xdg-open "$f" >/dev/null 2>&1 & sleep 1; rm "$f" ;; "youtube-dl") - echo "youtube-dl '$1' >/dev/null" | at now;; + echo "youtube-dl '$url' >/dev/null" | at now;; "youtube-dl-music") - nohup $TERMINAL -e youtube-dl-music "$1" >/dev/null 2>&1 & ;; + nohup $TERMINAL -e youtube-dl-music "$url" >/dev/null 2>&1 & ;; *) notify-send "${0##/*}" "Error: Action not understood" exit 1 ;;