arbit/arbit

95 lines
2.7 KiB
Bash
Executable file

#!/bin/sh
# Copyright 2023 Marty Sluijtman <marty.wanderer@disroot.org>
# Distributed under the terms of the GPLv3 license
videoDir="${XDG_VIDEOS_DIR:-$HOME/Videos}"
downloadDir="${ARBITDIR:-$videoDir/YouTube}"
[ -d "$downloadDir" ] || mkdir -p "$downloadDir"
die(){
if [ -n "$*" ]
then echo "${*}, exiting..."; notify-send "Arbit" "${*}, exiting..."
else echo "Failed, exiting..."; notify-send "Arbit" "Failed, exiting..."
fi
exit 1
}
getClip(){
clipboardContent="$(xclip -o)"
}
mpvClip(){
getClip
if echo "${clipboardContent}" | grep -qE 'youtube|youtu.be'; then
notify-send "Arbit" "Opening clipboard content with 'pipe-viewer'..."
pipe-viewer --player=mpv "${clipboardContent}" || die "Failed to parse URL"
else
notify-send "Arbit" "Opening clipboard content with 'mpv'..."
mpv --force-window "${clipboardContent}" || die "Failed to parse URL"
fi
}
clipboardQuery(){
getClip
notify-send "Arbit" "Searching YouTube with:\n\n${clipboardContent}"
pipe-viewer --player=mpv "${clipboardContent}"
}
downloadClipUrl(){
getClip
notify-send "Arbit" "Downloading video to ${downloadDir}"
if yt-dlp -o "${downloadDir}/%(title)s - %(uploader)s.%(ext)s" "${clipboardContent}"
then notify-send "Arbit" "Finished downloading video."
else die "Failed to download file"
fi
}
search(){
pipe-viewer --player=mpv "$(echo "$*" | sed 's/search //')"
}
viewUrl(){
url="$(echo "$*" | sed 's/url //')"
if echo "${url}" | grep -qE 'youtube|youtu.be'; then
notify-send "Arbit" "Opening URL with 'pipe-viewer'..."
pipe-viewer --player=mpv "${url}" || die "Failed to parse URL"
else
notify-send "Arbit" "Opening URL with 'mpv'..."
mpv --force-window "${url}" || die "Failed to parse URL"
fi
}
download(){
url="$(echo "$*" | sed 's/download //')"
notify-send "Arbit" "Downloading video to ${downloadDir}"
if yt-dlp -o "${downloadDir}/%(title)s - %(uploader)s.%(ext)s" "${url}"
then notify-send "Arbit" "Finished downloading video."
else die "Failed to download file"
fi
}
usage(){
cat >&2 <<EOF
Usage: $(basename "$0") <clipboard-url|clipboard-content|download-url|url|download>
clipboard-url: open the contents of clipboard in either 'pipe-viewer' or 'mpv'
clipboard-content: open pipe-viewer with clipboard content as search query
download-url: open 'yt-dlp' with clipboard content and save the result to
${downloadDir}
search: use any other given arguments as search query
url: open a given URL in pipe-viewer or mpv
download: open a given URL using 'yt-dlp' and save the result to
${downloadDir}
EOF
}
case $1 in
clipboard-url) mpvClip;;
clipboard-content) clipboardQuery;;
download-url) downloadClipUrl;;
search) search "$*";;
url)viewUrl "$*";;
download)download "$*";;
*) usage;;
esac