diff options
Diffstat (limited to 'ar/.local/bin/qndl')
| -rwxr-xr-x | ar/.local/bin/qndl | 110 |
1 files changed, 55 insertions, 55 deletions
diff --git a/ar/.local/bin/qndl b/ar/.local/bin/qndl index 8e30dba..f762568 100755 --- a/ar/.local/bin/qndl +++ b/ar/.local/bin/qndl @@ -1,12 +1,12 @@ #!/bin/sh -YTDL_CMD_BASE="yt-dlp --continue --embed-metadata --ignore-errors --no-force-overwrites --no-playlist --verbose" +ytdl_cmd_base="yt-dlp --continue --embed-metadata --ignore-errors --no-force-overwrites --no-playlist --verbose" case "$BROWSER" in *firefox*) cookies="firefox" ;; esac -[ -n "$cookies" ] && YTDL_CMD_BASE="$YTDL_CMD_BASE --cookies-from-browser $cookies" +[ -n "$cookies" ] && ytdl_cmd_base="$ytdl_cmd_base --cookies-from-browser $cookies" shift $((OPTIND - 1)) @@ -14,62 +14,62 @@ shift $((OPTIND - 1)) # [url] [type] [cmd] if [ $# -eq 1 ]; then type="$1" - URL="$(xclip -selection clipboard -o)" + url="$(xclip -selection clipboard -o)" elif [ $# -eq 2 ]; then if echo "$1" | grep -qE "https?://"; then - URL="$1" + url="$1" elif echo "$2" | grep -qE "https?://"; then type="$1" - URL="$2" + url="$2" fi fi # Process command-line options for download type case $type in -m | --music | m | music) - DOWNLOAD_TYPE="music" - OUTPUT_DIR="${XDG_MUSIC_DIR:-${HOME}/Music}" - ARCHIVE_FILE="${XDG_DOTFILES_DIR:-${HOME}/.dotfiles}/global/Music/.music.txt" - YTDL_OUTPUT_FORMAT="${OUTPUT_DIR}/%(artist|)s%(artist& - |)s%(title)s.%(ext)s" - YTDL_CMD_BASE="$YTDL_CMD_BASE --audio-format best --audio-quality 0 --download-archive \"$ARCHIVE_FILE\" --extract-audio --recode-video mp3" + download_type="music" + output_dir="${XDG_MUSIC_DIR:-${HOME}/Music}" + archive_file="${XDG_DOTFILES_DIR:-${HOME}/.dotfiles}/global/Music/.music.txt" + ytdl_output_format="${output_dir}/%(artist|)s%(artist& - |)s%(title)s.%(ext)s" + ytdl_cmd_base="$ytdl_cmd_base --audio-format best --audio-quality 0 --download-archive \"$archive_file\" --extract-audio --recode-video mp3" ;; -r | --restore | r | restore) - OUTPUT_DIR="${XDG_MUSIC_DIR:-${HOME}/Music}" - ARCHIVE_FILE="${XDG_DOTFILES_DIR:-${HOME}/.dotfiles}/global/Music/.music.txt" - YTDL_OUTPUT_FORMAT="${OUTPUT_DIR}/%(artist|)s%(artist& - |)s%(title)s.%(ext)s" - YTDL_CMD_BASE="$YTDL_CMD_BASE --audio-format best --audio-quality 0 --extract-audio --recode-video mp3" - YTDL_CMD="$YTDL_CMD_BASE --output \"$YTDL_OUTPUT_FORMAT\"" - [ ! -f "$ARCHIVE_FILE" ] && exit 1 + output_dir="${XDG_MUSIC_DIR:-${HOME}/Music}" + archive_file="${XDG_DOTFILES_DIR:-${HOME}/.dotfiles}/global/Music/.music.txt" + ytdl_output_format="${output_dir}/%(artist|)s%(artist& - |)s%(title)s.%(ext)s" + ytdl_cmd_base="$ytdl_cmd_base --audio-format best --audio-quality 0 --extract-audio --recode-video mp3" + ytdl_cmd="$ytdl_cmd_base --output \"$ytdl_output_format\"" + [ ! -f "$archive_file" ] && exit 1 while read -r line; do video_id=$(echo "$line" | awk '{print $2}') - YTDL_CMD="$YTDL_CMD_BASE --output \"$YTDL_OUTPUT_FORMAT\" \"https://www.youtube.com/watch?v=$video_id\"" - idnum=$(tsp bash -c "$YTDL_CMD") + ytdl_cmd="$ytdl_cmd_base --output \"$ytdl_output_format\" \"https://www.youtube.com/watch?v=$video_id\"" + idnum=$(tsp bash -c "$ytdl_cmd") pkill -RTMIN+21 "${STATUSBAR:-dwmblocks}" - done <"$ARCHIVE_FILE" + done <"$archive_file" exit 0 ;; -v | --video | v | video) - DOWNLOAD_TYPE="video" - OUTPUT_DIR="${XDG_VIDEOS_DIR:-${HOME}/Videos}" - YTDL_OUTPUT_FORMAT="${OUTPUT_DIR}/%(title)s [%(id)s].%(ext)s" - VIDEO_EXT=$(printf "best\n60fps\n30fps\nmp4\nmkv" | dmenu -i -p "Choose an encoding (default: 1080p)") || exit - case $VIDEO_EXT in + download_type="video" + output_dir="${XDG_VIDEOS_DIR:-${HOME}/Videos}" + ytdl_output_format="${output_dir}/%(title)s [%(id)s].%(ext)s" + video_ext=$(printf "best\n60fps\n30fps\nmp4\nmkv" | dmenu -i -p "Choose an encoding (default: 1080p)") || exit + case $video_ext in best) - VIDEO_FORMATS="--format bestvideo+bestaudio/best" + video_formats="--format bestvideo+bestaudio/best" ;; 60fps) - VIDEO_FORMATS='--format "((bv*[fps=60]/bv*)[height<=1080]/(wv*[fps=60]/wv*)) + ba / (b[fps=60]/b)[height<=1080]/(w[fps=60]/w)"' + video_formats='--format "((bv*[fps=60]/bv*)[height<=1080]/(wv*[fps=60]/wv*)) + ba / (b[fps=60]/b)[height<=1080]/(w[fps=60]/w)"' ;; 30fps) - VIDEO_FORMATS='--format "((bv*[fps=30]/bv*)[height<=1080]/(wv*[fps=30]/wv*)) + ba / (b[fps=30]/b)[height<=1080]/(w[fps=30]/w)"' + video_formats='--format "((bv*[fps=30]/bv*)[height<=1080]/(wv*[fps=30]/wv*)) + ba / (b[fps=30]/b)[height<=1080]/(w[fps=30]/w)"' ;; *) - VIDEO_FORMATS="--format bestvideo+bestaudio/best" - VIDEO_OPTIONS="--recode-video $VIDEO_EXT" + video_formats="--format bestvideo+bestaudio/best" + video_options="--recode-video $video_ext" ;; esac - YTDL_CMD_BASE="$YTDL_CMD_BASE --buffer-size 1M --embed-thumbnail $VIDEO_FORMATS --no-sponsorblock $VIDEO_OPTIONS" - YTDL_CMD_BASE="${YTDL_CMD_BASE%* }" + ytdl_cmd_base="$ytdl_cmd_base --buffer-size 1M --embed-thumbnail $video_formats --no-sponsorblock $video_options" + ytdl_cmd_base="${ytdl_cmd_base%* }" ;; *) notify-send "⛔ Invalid option: -$OPTARG" @@ -77,49 +77,49 @@ case $type in ;; esac -[ -z "$URL" ] && notify-send "⛔ No URL provided and clipboard is empty or does not contain a valid URL." && exit 1 +[ -z "$url" ] && notify-send "⛔ No URL provided and clipboard is empty or does not contain a valid URL." && exit 1 # Validate the URL format -! echo "$URL" | grep -qE '^https?://[a-zA-Z0-9.-]+(/[a-zA-Z0-9./?&%=_-]*)?$' && notify-send "⛔ Invalid URL format: $URL" && exit 1 +! echo "$url" | grep -qE '^https?://[a-zA-Z0-9.-]+(/[a-zA-Z0-9./?&%=_-]*)?$' && notify-send "⛔ Invalid URL format: $url" && exit 1 # Validate URL accessibility -! curl --head --silent --fail "$URL" >/dev/null && notify-send "⛔ URL is not accessible: $URL" && exit 1 +! curl --head --silent --fail "$url" >/dev/null && notify-send "⛔ URL is not accessible: $url" && exit 1 -case $URL in +case $url in *playlist* | *list=*) - PL_DOWNLOAD_CHOICE=$(printf "playlist\na content" | dmenu -i -p "Download entire playlist or just this content?") - [ "$PL_DOWNLOAD_CHOICE" = "playlist" ] && - YTDL_CMD_BASE=$(echo "$YTDL_CMD_BASE" | sed 's/ --no-playlist//') && - YTDL_CMD_BASE="$YTDL_CMD_BASE --yes-playlist" && + pl_download_choice=$(printf "playlist\na content" | dmenu -i -p "Download entire playlist or just this content?") + [ "$pl_download_choice" = "playlist" ] && + ytdl_cmd_base=$(echo "$ytdl_cmd_base" | sed 's/ --no-playlist//') && + ytdl_cmd_base="$ytdl_cmd_base --yes-playlist" && echo >/tmp/qplaylist - [ "$DOWNLOAD_TYPE" = "video" ] && - CHANNEL=$(yt-dlp --print "%(channel)s" "$URL" | head -n 1 | sed 's/, /,/g;s/[\/:*?"<>| ]/-/g' | tr '[:upper:]' '[:lower:]') && - PLAYLIST=$(yt-dlp --print "%(playlist_title)s" "$URL" | head -n 1 | sed 's/, /,/g;s/[\/:*?"<>| ]/-/g' | tr '[:upper:]' '[:lower:]') && - SUBDIR="${CHANNEL}/${PLAYLIST}" && - mkdir -p "${OUTPUT_DIR}/${SUBDIR}" && - YTDL_OUTPUT_FORMAT="${OUTPUT_DIR}/${SUBDIR}/%(playlist_index)02d_%(title)s [%(id)s].%(ext)s" + [ "$download_type" = "video" ] && + channel=$(yt-dlp --print "%(channel)s" "$url" | head -n 1 | sed 's/, /,/g;s/[\/:*?"<>| ]/-/g' | tr '[:upper:]' '[:lower:]') && + playlist=$(yt-dlp --print "%(playlist_title)s" "$url" | head -n 1 | sed 's/, /,/g;s/[\/:*?"<>| ]/-/g' | tr '[:upper:]' '[:lower:]') && + subdir="${channel}/${playlist}" && + mkdir -p "${output_dir}/${subdir}" && + ytdl_output_format="${output_dir}/${subdir}/%(playlist_index)02d_%(title)s [%(id)s].%(ext)s" ;; esac -SIMULATION_CMD="yt-dlp --simulate --print %(filename)s $URL" -YTDL_CMD="$YTDL_CMD_BASE --output \"$YTDL_OUTPUT_FORMAT\" \"$URL\"" +simulation_cmd="yt-dlp --simulate --print %(filename)s $url" +ytdl_cmd="$ytdl_cmd_base --output \"$ytdl_output_format\" \"$url\"" # Notify and perform simulation to get filename (feedback to user) -echo "$SIMULATION_CMD" | while IFS= read -r line; do +echo "$simulation_cmd" | while IFS= read -r line; do filename=$(basename "$line") - notify-send "📥 Queuing $DOWNLOAD_TYPE to download:" "$filename" + notify-send "📥 Queuing $download_type to download:" "$filename" done # Enqueue the download task with tsp -FILENAME=$($SIMULATION_CMD 2>/dev/null) +filename=$($simulation_cmd 2>/dev/null) [ -f /tmp/qplaylist ] && rm -rf /tmp/qplaylist -notify-send "⏳ Downloading $DOWNLOAD_TYPE:" "$FILENAME" -idnum=$(tsp bash -c "$YTDL_CMD") +notify-send "⏳ Downloading $download_type:" "$filename" +idnum=$(tsp bash -c "$ytdl_cmd") pkill -RTMIN+21 "${STATUSBAR:-dwmblocks}" # Notify upon completion -tsp -D "$idnum" notify-send "✅ $DOWNLOAD_TYPE download complete:" "$URL" || - notify-send "❌ Faild to download:" "$URL" +tsp -D "$idnum" notify-send "✅ $download_type download complete:" "$url" || + notify-send "❌ Faild to download:" "$url" # Conditionally update the music database if the download type is music -[ "$DOWNLOAD_TYPE" = "music" ] && tsp -D "$idnum" bash -c "mpc update" +[ "$download_type" = "music" ] && tsp -D "$idnum" bash -c "mpc update" |
