commit 0a1d47d8553d6a79e4173f57b63a50d887896fa4 parent 5731a939eb0664fc8c50d81f9dbf936a76c5e27e Author: hhvn <dev@hhvn.uk> Date: Sun, 2 Jan 2022 02:09:07 +0000 .s/b/netdownload: rewrite in rc Diffstat:
M | .scripts/bin/netdownload | | | 214 | ++++++++++++++++++++++++++++++------------------------------------------------- |
1 file changed, 80 insertions(+), 134 deletions(-)
diff --git a/.scripts/bin/netdownload b/.scripts/bin/netdownload @@ -1,141 +1,87 @@ -#!/bin/sh - -uri2file(){ - awk ' - BEGIN { - FS="/" +#!/bin/rc + +fn uri2file { + ret = () + + for (uri in $*) { + filetype = `{echo $uri | grep -o '\.[^/.]*$' | sed 's/^.//'} + hier = ``('/'){echo $uri | sed 's~^.*://~~'} + for (ent in $hier) { + if (~ $reverse ()) { + reverse = $ent + } else { + reverse = $ent ^ '+' ^ $reverse + } + } + filename = `{echo $reverse | tr -d '\n'} + if (! ~ $filetype ()) + filename = $^filename ^ '.' ^ $filetype + reverse = () + ret = ($ret $filename) } - { - for (i=NF; i != 1; i--) - printf "%s+", $i - printf "%s\n", $1 - }' + echo $^ret | tr -d ' ' } -trimlast(){ - echo "$1" | sed -E 's~/[^/]*/?$~/~' +fn mktemp { + urifile = ``($nl){uri2file $2} + ret = $1 ^ '/' ^ $^urifile + while (test -f $ret) { + ret = $1 ^ '/' ^ `{base64 </dev/urandom | tr -d '/' | head -c10} + } + echo $^ret | tr -s '/' } -append(){ - echo "$1" | sed 's~/$~~' | tr -d '\n' - printf '/%s\n' "$2" +rcname = `{basename $0} + +if (~ $* ()) + * = `{cat /dev/stdin} + +for (arg in $*) { + switch ($arg) { + case -h +cat >[1=2] <<eof +usage: $rcname [-hpdt] <file|URI> ... + +-h help +-p plumb URIs +-d download URIs to default location +. +eof + exit 2 + case -p + default = 'plumb' + case -d + default = 'download' + case * + if (test -f $arg) { + lines = ``($nl){cat $arg} + for (line in $lines) { + column = ``($tab $nl){echo $line} + if (!~ $column(1) () && !~ $column(2) ()) { + mkdir -p $column(1) + filename = $column(1) ^ '/' ^ `{uri2file $column(2)} + if (!test -f $^filename) { + curl '-o'^$filename $column(2) + } + } + } + } else { + if (! ~ $default ()) { + input = $default + } else { + input = `{printf 'plumb\ndownload\n' | dmenu -i -p $arg} + } + + switch ($input) { + case plumb + plumb $arg + case download + filename = ``($nl){mktemp $HOME/general/downloads $arg} + uri = `{echo $arg | sed -E 's/]|\[|}|\{/\\&/g'} + curl $^uri '>' $^filename + herbe 'Saved to: ' ^ $^filename + } + } + } } - -[ $# -eq 0 ] && set -- "/dev/stdin" - -while [ $# -ne 0 ] -do - case "$1" in - "-h") - cat >&2 <<- . - usage: $(basename "$0") [-hpdt] <file|URI> ... - - -h help - -p plumb URIs - -d download URIs to default location - -t download URIs to menu specificed location - . - exit 2 - ;; - "-p") - input_def="plumb" - shift - ;; - "-d") - input_def="download" - shift - ;; - "-t") - input_def="download to" - shift - ;; - *"://"*) # URI - [ -n "$input_def" ] && - input="$input_def" || - input=$(printf "plumb\ndownload\ndownload to\n" | dmenu -i -p "$1") - case "$input" in - d*to) - dir="$HOME" - while true - do - ndir=$( - ( - printf '%s\n%s\n' "." ".." - find -L "$dir" -maxdepth 1 -mindepth 1 -type d | - awk -v "dir=$dir" '{sub(dir, ""); print}' - ) | sed -E 's~\.?/~~' | dmenu -p "$dir" - ) - - [ "$ndir" = "" ] && exit 1 - [ "$ndir" = "." ] && break - [ "$ndir" = ".." ] && - dir=$(trimlast "$dir") || - dir=$(append "$dir" "$ndir") - done - file="$dir/$(echo "$1" | uri2file)" - while [ -f $file ] - do - file="$dir/$(base64 </dev/urandom | tr '/' '+' | head -c10)" - done - curl "$(echo "$1" | sed -E 's/]|\[|}|\{/\\&/g')" > "$file" - herbe "Saved to: $file" - echo "$file" | xclip - ;; - 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 - shift - ;; - *) # 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 "$line" ] && continue - [ -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" | sha256sum | awk '{print $1}')" - [ -f "$file" ] && 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" - shift - ;; - esac -done