commit 39403448f2d3ba7a1f423bcf3ae03d69271a6c45 parent 5d3563d266134f2a41c3314af550d43f4d861723 Author: hhvn <dev@hhvn.uk> Date: Thu, 29 Jul 2021 17:28:33 +0100 .s/b/netdownload: bulk download Diffstat:
M | .scripts/bin/netdownload | | | 75 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------- |
1 file changed, 56 insertions(+), 19 deletions(-)
diff --git a/.scripts/bin/netdownload b/.scripts/bin/netdownload @@ -1,8 +1,5 @@ #!/bin/sh -# silently ignore non-authorative (or non-) uris. -echo "$1" | grep "://" >/dev/null || exit 99 - uri2file(){ awk ' BEGIN { @@ -16,22 +13,62 @@ uri2file(){ }' } -input=$(printf "plumb\ndownload\n" | dmenu -i -p "$1") -case "$input" in -d*) - file="$HOME/general/downloads/$(echo "$1" | uri2file)" - while [ -f $file ] - do - file="$HOME/general/downloads/$(base64 </dev/urandom | tr '/' '+' | head -c10)" - done - curl "$(echo "$1" | sed -E 's/]|\[|}|\{/\\&/g')" > "$file" - herbe "Saved to: $file" - echo "$file" | xclip - ;; -p*) - plumb "$1" +case "$1" in +*"://"*) # URI + input=$(printf "plumb\ndownload\n" | dmenu -i -p "$1") + case "$input" in + d*) + file="$HOME/general/downloads/$(echo "$1" | uri2file)" + while [ -f $file ] + do + file="$HOME/general/downloads/$(base64 </dev/urandom | tr '/' '+' | head -c10)" + done + curl "$(echo "$1" | sed -E 's/]|\[|}|\{/\\&/g')" > "$file" + herbe "Saved to: $file" + echo "$file" | xclip + ;; + p*) + plumb "$1" + ;; + *) + exit 99 + ;; + esac ;; -*) - exit 99 +*) # download queue + linec=0 + while read -r line + do + linec=$(echo "$linec + 1" | bc) + + dir=$(printf '%s\n' "$line" | awk -F"\t" '{print $1}') + uri=$(printf '%s\n' "$line" | awk -F"\t" '{print $2}') + # let $3 be comment, therefore no need to handle + + [ -z "$dir" ] || [ -z "$uri" ] && + printf "ERROR: Erroneous syntax on line %d\n" "$linec" && + continue + + [ -f "$dir" ] && + printf "ERROR: File exists inplace of dir '%s'\n" "$dir" && + continue + + mkdir -p "$dir" + + file="$dir/$(echo "$uri" | uri2file)" + [ -f "$file" ] && + printf "ERROR: File '%s' already exits: skipping '%s'\n" "$file" "$uri" && + continue + + curlout=$(curl -o"$file" "$uri" 2>&1) + curlexit=$? + curlout=$(printf '%s\n' "$curlout" | tail -n1 | tr -d '\r' | sed 's/.*curl: ([0-9]*) //') + + [ $curlexit -ne 0 ] && + printf "ERROR: Failed to download '%s': %s\n" "$uri" "$curlout" && + continue + + printf "Downloaded '%s' as %s\n" "$uri" "$file" + done < "$1" ;; esac