diff --git a/aria2sel b/aria2sel new file mode 100755 index 0000000..4bc72f1 --- /dev/null +++ b/aria2sel @@ -0,0 +1,37 @@ +#!/bin/sh +set -euf + +# wrapper script over --select-file for downloading from aria2c torrents + +# escape stuff for evaluating +escape() { + sed -e "s/^$/''/" -e "s/'/'\\\\''/g" <<-EOF + $1 + EOF +} + +errecho() { printf '%s\n' "$*" >&2; } + +FILTER_CMD="" FILTER_STR="" DOWNLOAD="" +while getopts ":dFin:" OPT; do + case "$OPT" in + d) DOWNLOAD=1 ;; + F|i) FILTER_STR="${FILTER_STR} -$OPTARG" ;; + n) + FILTER_CMD="$FILTER_CMD | grep -E ${FILTER_STR} -e '$(escape "$OPTARG")'" + ;; + *) + esac +done +shift $((OPTIND - 1)) + +[ -z "${1:-}" ] && errecho "error: torrent file needed" && exit 1 +[ -z "$FILTER_CMD" ] && errecho "error: no filters! use -n" && exit 1 + +inds=$(eval "aria2c -S '$(escape "$1")' $FILTER_CMD | cut -d '|' -f 1 | tr -d ' ' | paste -s -d ','") + +echo "$inds" + +if [ -n "$DOWNLOAD" ]; then + aria2c --select-file="$inds" "$1" +fi