diff --git a/.scripts/tpb.sh b/.scripts/tpb.sh new file mode 100755 index 0000000..fb1b5b6 --- /dev/null +++ b/.scripts/tpb.sh @@ -0,0 +1,164 @@ +#!/bin/sh +# +# by Sairon Istyar, 2012 +# distributed under the GPLv3 license +# http://www.opensource.org/licenses/gpl-3.0.html +# + +### CONFIGURATION ### +# program to use for torrent download +# magnet link to torrent will be appended +# you can add -- at the end to indicate end of options +# (if your program supports it, most do) +program='/usr/bin/transmission-remote -a' +TPB="https://thepiratebay.org" + +# show N first matches by default +limit=50 + +# colors +numbcolor='\x1b[1;35m' +namecolor='\x1b[1;33m' +sizecolor='\x1b[1;36m' +seedcolor='\x1b[1;31m' +peercolor='\x1b[1;32m' +errocolor='\x1b[1;31m' +mesgcolor='\x1b[1;37m' +nonecolor='\x1b[0m' + +# default ordering method +# 1 - name ascending; 2 - name descending; +# 3 - recent first; 4 - oldest first; +# 5 - size descending; 6 - size ascending; +# 7 - seeds descending; 8 - seeds ascending; +# 9 - leechers descending; 10 - leechers ascending; +orderby=7 +### END CONFIGURATION ### + +thisfile="$0" + +printhelp() { + echo -e "Usage:" + echo -e "\t$thisfile [options] search query" + echo + echo + echo -e "Available options:" + echo -e "\t-h\t\tShow help" + echo -e "\t-n [num]\tShow only first N results (default 15; max 100 [top] or 30 [search])" + echo -e "\t-C\t\tDo not use colors" + echo -e "\t-P [prog]\tSet torrent client command (\`-P torrent-client\` OR \`-P \"torrent-client --options\"\`)" + echo + echo -e "Current client settings: $program [magnet link]" +} + +# change torrent client +chex() { + sed "s!^program=.*!program=\'$program\'!" -i "$thisfile" + if [ $? -eq 0 ] ; then + echo "Client changed successfully." + exit 0 + else + echo -e "${errocolor}(EE) ${mesgcolor}==> Something went wrong!${nonecolor}" + exit 1 + fi +} + +# script cmdline option handling +while getopts :hn:CP:: opt ; do + case "$opt" in + h) printhelp; exit 0;; + n) limit="$OPTARG";; + C) unset numbcolor namecolor sizecolor seedcolor peercolor nonecolor errocolor mesgcolor;; + P) program="$OPTARG"; chex;; + *) echo -e "Unknown option(s)."; printhelp; exit 1;; + esac +done + +shift `expr $OPTIND - 1` + +# correctly encode query +q=`echo "$*" | tr -d '\n' | od -t x1 -A n | tr ' ' '%'` + +# if not searching, show top torrents +if [ -z "$q" ] ; then + url="top/all" +else + url='search/'"$q"'/0/'"$orderby"'/0' +fi + +# get results +# Here be dragons! +r=`curl -k -A Mozilla -b "lw=s" -m 15 -s "$TPB/$url" \ + | grep -Eo '^