diff options
| author | Raymaekers Luca <raymaekers.luca@gmail.com> | 2024-07-11 21:01:23 +0200 | 
|---|---|---|
| committer | Raymaekers Luca <raymaekers.luca@gmail.com> | 2024-07-11 21:01:23 +0200 | 
| commit | 6cf892bcb7f444d7941cb70308038193b8d9c602 (patch) | |
| tree | c949fc548f4670b894511bd73aa90071c29eb965 /bin/menuscripts/tsh | |
| parent | f039e82cac63f24ee7386bb3efd1fc6921d14d12 (diff) | |
checkpoint
Diffstat (limited to 'bin/menuscripts/tsh')
| -rwxr-xr-x | bin/menuscripts/tsh | 250 | 
1 files changed, 112 insertions, 138 deletions
diff --git a/bin/menuscripts/tsh b/bin/menuscripts/tsh index 19a7373..0e2b64d 100755 --- a/bin/menuscripts/tsh +++ b/bin/menuscripts/tsh @@ -3,7 +3,7 @@  PROG="$(basename "$0")"  ## VARIABLES  # copy command and deps variable -deps="pup curl clipp" +deps="pup curl clipp transmission-cli"  MODULES_PATH=$HOME/.local/share/tsh  module='nyaa.sh' # default module @@ -13,8 +13,8 @@ export tmp="/tmp/$PROG"  files="seeds sizes names html tmp_types"  # Use export so that these variables can be used inside of modules -for file in $files -do eval "export $file=$tmp/$file" +for file in $files; do +	eval "export $file=$tmp/$file"  done  # Files not in $files won't be deleted @@ -23,175 +23,149 @@ export links="$tmp/links"  categories="music anime movies shows other software games isos books" -## FUNCTIONS -help () -{ -	>&2 cat <<-EOF -	Usage: $PROG [options] query -	Options: -	    -h           Show this help message and exit -	    -r           Skip getting pages and use last results -	    -s OPTION    Sort results based on the specified OPTION -	                 Available options: seeds, size, name -	    -m MODULE    Select a module, if MODULE is 'list', -	                 lists out available modules -            -c CATEGORY  Select category -            -f           Do not list files -            -d		 Download without confirming -	EOF +logn() { >&2 printf '%s\n' "$*"; } +die() { +	logn "$@" +	exit 1  } - -log () { >&2 printf '%s' "$*"; } -logn () { >&2 printf '%s\n' "$*"; } -die () { logn "$@"; exit 1; } - -confirm() -{ -	log "$1" +confirm() { +	>&2 printf '%s' "$1"  	head -n 1 | grep -q "[yY]"  } -dependencies () -{ -	for p in $deps -	do -		if ! command -v "$p" > /dev/null -		then -			logn "E: Program '$p' not found." -			error=1 -		fi -	done -	[ "${error:-0}" -eq 1 ] && exit 1 -} -  # Remove temp files and quit -cleanup () -{ -	for file in $files -	do eval "rm -f \$$file" +cleanup() { +	for file in $files; do +		eval "rm -f \$$file"  	done  } -list_modules () { find -L "$MODULES_PATH" -type f -printf "%f\n"; } - -# get a query from user based on MENUCMD -get_query () -{ -	isOnline || exit 1 -	if [ "${query:="$*"}" ] -	then -		printf "%s" "$query" -	else -		log ' > ' -		head -n 1 -	fi | tr ' ' '+' -} - -# Select a result from the result file sorterd with sort_results -# and select with fzf -select_result () -{ -	command -v sort_results > /dev/null || sort_results() { sort -k3 -n -r; } -	awk '{print NR, $0}' "$results" | -		sort_results | -		column -t -l 3 | -		fzf -m --with-nth 2.. | -		awk '{print $1}' -} - -show_files() -{ -	hash="${1##*btih:}" -	hash="${hash%%&*}" - -	# Download the torrent file from a torrent website -	curl -s "https://itorrents.org/torrent/${hash}.torrent" > "$tmp"/.torrent -	transmission-show "$tmp"/.torrent | sed -n '/^FILES/,$p' | head -n -1 | tail -n +3 >&2 -	rm -f "$tmp"/.torrent -} +list_modules() { find -L "$MODULES_PATH" -type f -printf "%f\n"; }  trap "exit 1" INT  trap "cleanup" EXIT  ## OPTIONS  skip=0 -while getopts ":hm:rs:c:fd" opt -do +while getopts ":hm:rs:c:fd" opt; do  	case $opt in -		h) help && exit ;; -		c) -			[ "$OPTARG" = "list" ] && >&2 printf '%s\n' "$categories" && exit -			category="$(printf '%s\n' "$categories" | tr ' ' '\n' | grep -m 1 "^$OPTARG")" -			[ -z "$category" ] && die "No valid category for '$OPTARG'" -			logn "category: $category" ;; -		f) noaskfiles="1" ;; -		d) noaskdownload="1" ;; -		m) -			[ "$OPTARG" = "list" ] && list_modules && exit -			module="$(list_modules | grep -m 1 "^$OPTARG")" -			[ -z "$module" ] && die "No valid module for '$OPTARG'" -			logn "module: $module" ;; -		r)  -			[ ! -r "$results" ] && die "No previous results found." -			skip=1 ;; -		s) -			case $OPTARG in -				"seeds")	sort_results() { sort -k3 -n -r; } ;; -				"size") 	sort_results() { sort -k2 -h -r; } ;; -				"name") 	sort_results() { sort -k4; } ;; -				*) die "argument '$OPTARG' not seeds,size,name" ;; -			esac ;; -		:) die "Option -$OPTARG requires an argument" ;; -		?) die "Invalid option: -$OPTARG" ;; +	h) +		>&2 cat <<-EOF +			Usage: $PROG [options] query +			Options: +			    -h           Show this help message and exit +			    -r           Skip getting pages and use last results +			    -s OPTION    Sort results based on the specified OPTION +			                 Available options: seeds, size, name +			    -m MODULE    Select a module, if MODULE is 'list', +			                 lists out available modules +			            -c CATEGORY  Select category +			            -f           Do not list files +			            -d   Download without confirming +		EOF + +		exit +		;; +	c) +		[ "$OPTARG" = "list" ] && >&2 printf '%s\n' "$categories" && exit +		category="$(printf '%s\n' "$categories" | tr ' ' '\n' | grep -m 1 "^$OPTARG")" +		[ -z "$category" ] && die "No valid category for '$OPTARG'" +		logn "category: $category" +		;; +	f) noaskfiles="1" ;; +	d) noaskdownload="1" ;; +	m) +		[ "$OPTARG" = "list" ] && list_modules && exit +		module="$(list_modules | grep -m 1 "^$OPTARG")" +		[ -z "$module" ] && die "No valid module for '$OPTARG'" +		logn "module: $module" +		;; +	r) +		[ ! -r "$results" ] && die "No previous results found." +		skip=1 +		;; +	s) +		case $OPTARG in +		"seeds") sort_results() { sort -k3 -n -r; } ;; +		"size") sort_results() { sort -k2 -h -r; } ;; +		"name") sort_results() { sort -k4; } ;; +		*) die "argument '$OPTARG' not seeds,size,name" ;; +		esac +		;; +	:) die "Option -$OPTARG requires an argument" ;; +	?) die "Invalid option: -$OPTARG" ;;  	esac  done  shift $((OPTIND - 1)) -dependencies +for p in $deps; do +	if ! command -v "$p" >/dev/null; then +		logn "E: Program '$p' not found." +		error=1 +	fi +done +[ "${error:-0}" -eq 1 ] && exit 1  # Get the torrents with module -if [ $skip -ne 1 ] -then +if [ $skip -eq 1 ]; then +	module="$(tail -n 1 "$links")" +else  	mkdir -p "$tmp" -	query="$(get_query "$*")" +	query="$( +		if [ "${query:="$*"}" ]; then +			printf "%s" "$query" +		else +			>&2 printf ' > ' +			head -n 1 +		fi | tr ' ' '+' +	)"  	[ "$query" ] || exit 1 -	# Get results -	rm -f "$results" "$links" -  	# shellcheck source=/usr/local/lib/$PROG/nyaa.sh disable=SC1091  	. "$MODULES_PATH/$module" +	rm -f "$results" "$links"  	get_torrents  	[ -f "$results" ] || die "No results." -	# Save which module was used -	printf "%s\n" "$module" >> "$links" -else -	module="$(tail -n 1 "$links")" +	# Save which module was used (hack) +	printf "%s\n" "$module" >>"$links"  fi +command -v sort_results >/dev/null || sort_results() { sort -k3 -n -r; }  # select result from "$results" -for choice in $(select_result | xargs) -do -    printf 'choice: %s\n' "$(sed -n "${choice}p" "$results" | cut -f 3-)" -    magnet="$(get_magnet "$choice")" -    [ "$magnet" ] || exit 1 - -    if [ -z "$noaskfiles" ] && confirm 'files?'; then -	    show_files "$magnet" -    fi - -    if [ "$noaskdownload" ] || confirm 'download?' -    then -    	    [ "$category" ] || category="$(printf '%s' "$categories" | tr ' ' '\n' | fzf)" -    	    [ "$category" ] || exit 2 -	    transmission-remote debuc.com -a "$magnet" -w "/downloads/$category" -    elif confirm "copy?" -    then -	    echo "$magnet" | clipp -    fi +for choice in $( +	# Select result(s) from the result file sorted with sort_results +	# with fzf +	awk '{print NR, $0}' "$results" | +		sort_results | +		column -t -l 3 | +		fzf -m --with-nth 2.. | +		awk 'BEGIN{OFS=" "} {print $1}' +); do + +	printf 'choice: %s\n' "$(sed -n "${choice}p" "$results" | cut -f 3-)" +	magnet="$(get_magnet "$choice")" +	[ "$magnet" ] || exit 1 + +	if [ -z "$noaskfiles" ] && confirm 'files?'; then +		hash="${1##*btih:}" +		hash="${hash%%&*}" + +		# Download the torrent file from a torrent website +		curl -s "https://itorrents.org/torrent/${hash}.torrent" >"$tmp"/.torrent +		transmission-show "$tmp"/.torrent | sed -n '/^FILES/,$p' | head -n -1 | tail -n +3 >&2 +		rm -f "$tmp"/.torrent +	fi + +	if [ "$noaskdownload" ] || confirm 'download?'; then +		[ "$category" ] || category="$(printf '%s' "$categories" | tr ' ' '\n' | fzf)" +		[ "$category" ] || exit 2 +		transmission-remote debuc.com -a "$magnet" -w "/downloads/$category" +	elif confirm "copy?"; then +		echo "$magnet" | clipp +	fi  done  | 
