dotfiles

Configuration files for shell, text editor, graphical environment, etc.
git clone git://src.adamsgaard.dk/dotfiles
Log | Files | Refs | Submodules | README | LICENSE

tpb (4373B)


      1 #!/usr/bin/env bash
      2 #
      3 # by Sairon Istyar, 2012
      4 # distributed under the GPLv3 license
      5 # http://www.opensource.org/licenses/gpl-3.0.html
      6 #
      7 set -e
      8 
      9 ### CONFIGURATION ###
     10 # program to use for torrent download
     11 # magnet link to torrent will be appended
     12 # you can add -- at the end to indicate end of options
     13 # (if your program supports it, most do)
     14 program='/usr/bin/transmission-remote --add --encryption-required'
     15 daemon='transmission-daemon'
     16 TPB="https://thepiratebay.org"
     17 
     18 # show N first matches by default
     19 limit=50
     20 
     21 # colors
     22 numbcolor='\x1b[1;35m'
     23 namecolor='\x1b[1;33m'
     24 sizecolor='\x1b[1;36m'
     25 seedcolor='\x1b[1;31m'
     26 peercolor='\x1b[1;32m'
     27 errocolor='\x1b[1;31m'
     28 mesgcolor='\x1b[1;37m'
     29 nonecolor='\x1b[0m'
     30 
     31 # default ordering method
     32 # 1 - name ascending; 2 - name descending;
     33 # 3 - recent first; 4 - oldest first;
     34 # 5 - size descending; 6 - size ascending;
     35 # 7 - seeds descending; 8 - seeds ascending;
     36 # 9 - leechers descending; 10 - leechers ascending;
     37 orderby=7
     38 ### END CONFIGURATION ###
     39 
     40 thisfile="$0"
     41 
     42 printhelp() {
     43 	echo -e "Usage:"
     44 	echo -e "\t$thisfile [options] search query"
     45 	echo
     46 	echo
     47 	echo -e "Available options:"
     48 	echo -e "\t-h\t\tShow help"
     49 	echo -e "\t-n [num]\tShow only first N results (default 15; max 100 [top] or 30 [search])"
     50 	echo -e "\t-C\t\tDo not use colors"
     51 	echo -e "\t-P [prog]\tSet torrent client command (\`-P torrent-client\` OR \`-P \"torrent-client --options\"\`)"
     52 	echo
     53 	echo -e "Current client settings: $program [magnet link]"
     54 }
     55 
     56 # change torrent client
     57 chex() {
     58 	sed "s!^program=.*!program=\'$program\'!" -i "$thisfile"
     59 	if [ $? -eq 0 ] ; then
     60 		echo "Client changed successfully."
     61 		exit 0
     62 	else
     63 		echo -e "${errocolor}(EE) ${mesgcolor}==> Something went wrong!${nonecolor}"
     64 		exit 1
     65 	fi
     66 }
     67 
     68 # script cmdline option handling
     69 while getopts :hn:CP:: opt ; do
     70 	case "$opt" in
     71 		h) printhelp; exit 0;;
     72 		n) limit="$OPTARG";;
     73 		C) unset numbcolor namecolor sizecolor seedcolor peercolor nonecolor errocolor mesgcolor;;
     74 		P) program="$OPTARG"; chex;;
     75 		*) echo -e "Unknown option(s)."; printhelp; exit 1;;
     76 	esac
     77 done
     78 
     79 shift $(expr $OPTIND - 1)
     80 
     81 # correctly encode query
     82 q=$(echo "$*" | tr -d '\n' | od -t x1 -A n | tr ' ' '%')
     83 
     84 # if not searching, show top torrents
     85 if [ -z "$q" ] ; then
     86 	url="top/all"
     87 else
     88 	url='search/'"$q"'/0/'"$orderby"'/0'
     89 fi
     90 
     91 # get results
     92 # Here be dragons!
     93 r=`torsocks curl -k -A Mozilla -b "lw=s" -m 15 -s "$TPB/$url" \
     94 	| grep -Eo '^<td><a href=\"/torrent/[^>]*>.*|^<td><nobr><a href=\"[^"]*|<td align=\"right\">[^<]*' \
     95 	| sed  's!^<td><a href=\"/torrent/[^>]*>!!; \
     96 		s!</a>$!!; \
     97 		s!^<td><nobr><a href=\"!!; \
     98 		s!^<td [^>]*>!!; \
     99 		s!&nbsp;!\ !g; \
    100 		s/|/!/g' \
    101 	| sed  'N;N;N;N;s!\n!|!g'`
    102 
    103 # number of results
    104 n=$(echo "$r" | wc -l)
    105 
    106 IFS=$'\n'
    107 
    108 # print results
    109 echo "$r" \
    110 	| head -n "$limit" \
    111 	| awk -v N=1 \
    112 		-v NU="$numbcolor" \
    113 		-v NA="$namecolor" \
    114 		-v SI="$sizecolor" \
    115 		-v SE="$seedcolor" \
    116 		-v PE="$peercolor" \
    117 		-v NO="$nonecolor" \
    118 		-F '|' \
    119 		'{print NU N ") " NA $1 " " SI $3 " " SE $4 " " PE $5 NO; N++}'
    120 
    121 # read ID(s), expand ranges, ignore everything else
    122 read -p ">> Torrents to download (eg. 1 3 5-7): " selection
    123 IFS=$'\n\ '
    124 for num in $selection ; do
    125 	if [ "$num" = "$(echo $num | grep -o '[[:digit:]][[:digit:]]*')" ] ; then
    126 		down="$down $num"
    127 	elif [ "$num" = "$(echo $num | grep -o '[[:digit:]][[:digit:]]*-[[:digit:]][[:digit:]]*')" ] ; then
    128 		seqstart="${num%-*}"
    129 		seqend="${num#*-}"
    130 		if [ $seqstart -le $seqend ] ; then
    131             down="$down $(seq $seqstart $seqend)"
    132 		fi
    133 	fi
    134 done
    135 
    136 # normalize download list, sort it and remove dupes
    137 down="$(echo $down | tr '\ ' '\n' | sort -n | uniq)"
    138 IFS=$'\n'
    139 
    140 # check whether we're downloading something, else exit
    141 if [ -z "$down" ] ; then
    142 	exit 0
    143 fi
    144 
    145 # starts daemon if it was not already running and download all torrents in list
    146 echo -n "Downloading torrent(s): "
    147 if ! [ "$(pidof $daemon)" ]; then
    148 	$daemon
    149 	for torrent in $down ; do
    150 		# check if ID is valid and in range of results, download torrent
    151 		if [ $torrent -ge 1 ] ; then
    152 			if [ $torrent -le $limit ] ; then
    153 				echo -n "$torrent "
    154 				command="$program $(echo "$r" | awk -F '|' 'NR=='$torrent'{print $2; exit}')"
    155 				status=$(eval "$command" 2>&1)
    156 				if [ $? -ne 0 ] ; then
    157 					echo -n '(failed!) '
    158 					report="$report\n(#$torrent) $status"
    159 				fi
    160 			fi
    161 		fi
    162 	done
    163 fi
    164 
    165 echo
    166 if [ -n "$report" ] ; then
    167 	echo -n "Exited with errors:"
    168 	echo -e "$report"
    169 fi
    170 unset IFS