updated on Thu Jan 26 00:18:00 UTC 2012
[aur-mirror.git] / pbtorrent / pbtorrent
blob3d064abd9bceb5bbd516f80144e3ee4fc8838f94
1 #! /bin/bash
2 # a pirate bay torrent browsing script
3 # v 0.2 by chochem
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # PLEASE EDIT TO REFLECT YOUR SETUP
19 watchdir=`pwd`
22 # FUNCTION DEFINITIONS BEGIN #
24 printhelp () {
25 echo "Pbtorrent is a script to browse/search the pirate bay from the command line. When torrents are picked the torrent file will be copied to a watch directoy, to be picked up by a torrent client, that automatically loads torrents in that directory (please edit the script to set the corrent 'watchdir' variable).
27 The syntax is fast-typed but somewhat inflexible (the order of apperance must be respected):
29 pbtorrent [search category key] ([sorting key]) ([search terms])
31 First, a (one and only one) search category is required. The options are:
32 a audio
33 v video
34 s software
35 g games
36 x anything else
37 A all of the above (requires search terms to work)
39 Second, a sorting key is optional (if none is entered, number of seeds is default). The options are:
40 s seeds
41 z size
42 r relevance (pirate bay default)
44 Finally you enter your search terms. If none are entered, you will be given the 'browse category' results.
46 Examples Meaning
47 pbtorrent a Browse category audio with default (seeds) sorting key
48 pbtorrent s r ubuntu Search software category for 'ubuntu' with relevance sorting key
49 pbtorrent v z Browse video category with size sorting key
50 pbtorrent x surprise me Search anything else category for term 'surprise me' with default sorting
52 Your search results will be presented in table format with a number, a name, size (caveat: you have to guess if it means kiB, MiB or GiB - sucks, i know, but it saves a lot of trouble, and it's not that hard to figure out), number of seeds and number of leeches. Just enter the number(s) of the torrent(s) you want (multiple numbers should be separated by a space) and hit enter, and the torrent will be downloaded and fed to transmission. In order to be able to keep browsing your results (say you're looking at the first page of the most popular video files, 'pbtorrent v', and want to see what's on the following page(s) too), the script will only exit when 'q' or 'x' is entered." | fmt -s -w $twidth
53 exit
56 quitter () {
57 echo "No connection."
58 exit
61 # This is what should be done with the downloaded torrents
62 whattodowithtorrent () {
63 for l in $k; do
64 wget -U "$userid" -qO ${watchdir}/pb${l}.torrent "${torrenturl[$l]}" && echo "${torrenturl[$l]} downloaded."
65 done
68 # FUNCTION DEFINITIONS END #
70 # initial settings
71 twidth=$(tput cols)
72 nwidth=$((${twidth}-29))
73 j=0
74 while [ $j -lt $twidth ]; do
75 hrule="${hrule}-"
76 let j++
77 done
79 # get help
80 if [ -z "$*" -o "$*" = "-h" -o "$*" = "--help" ]; then
81 printhelp;
84 # first is category of search, second is sorting key (seeds, size, relevance),
85 # third is search terms (if none are entered, browse category)
86 case "$1" in
87 a) searchcat="100";;
88 v) searchcat="200";;
89 s) searchcat="300";;
90 g) searchcat="400";;
91 x) searchcat="600";;
92 A) searchcat="100,200,300,400,600";;
93 *) echo "Not valid category. Printing help..."; printhelp;;
94 esac
95 shift
96 case "$1" in
97 s) sortby="7"; shift;;
98 z) sortby="5"; shift;;
99 r) sortby="99"; shift;;
100 *) sortby="7";;
101 esac
102 if [ -z "$*" ]; then
103 mode="browse"; searchterms="$searchcat"; searchcat=""
104 else
105 mode="search"; searchterms="$(echo "$*" | tr \ \+)"
108 page=0
109 loop=1
111 # main script loop to allow repeat runs with new page number (will stop once user quits)
112 while [ $loop -eq 1 ]; do
114 searchurl="http://thepiratebay.org/${mode}/${searchterms}/${page}/${sortby}/${searchcat}"
115 userid='Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008060701 Firefox/3.0'
117 # download and cut search results
118 wget -U "$userid" -qO /tmp/pbindex.htm "$searchurl" || quitter
119 grep -A 3 "http://torrents.thepiratebay.org" /tmp/pbindex.htm > /tmp/pbindex
121 # 'parsing' for torrent addresses and names
123 addresses="$(grep -oE 'http://torrents.thepiratebay.org/[0-9]+/.+\.[0-9]+\.TPB\.torrent' /tmp/pbindex)"
124 if [ -z "$addresses" ]; then
125 echo "Search produced no results. Please try again"
126 exit
128 for i in $addresses; do
129 let c++
130 torrenturl[$c]="$i"
131 tempname="$(echo $i | sed -r -e s/'http\:\/\/torrents\.thepiratebay\.org\/[0-9]+\///g' -e 's/\.[0-9]+\.TPB\.torrent$//g' -e 's/_/\ /g')"
132 tempname2="${tempname:0:${nwidth}}"
133 while [ ${#tempname2} -lt ${nwidth} ]; do
134 tempname2="$tempname2 "
135 done
136 torrentname[$c]="$tempname2"
137 done
139 # 'parsing' for size, seeds and leechers data
142 numbers="$(grep -v 'http://torrents.thepiratebay.org' /tmp/pbindex | grep -oE [0-9\.]+)"
143 for i in $numbers; do
144 let p++
145 case $p in
146 1) let c++; size="$i"; while [ ${#size} -lt 7 ]; do size=" $size"; done; size[$c]=$size ;;
147 2) seeds="$i"; while [ ${#seeds} -lt 5 ]; do seeds=" $seeds"; done; seeds[$c]=$seeds;;
148 3) leechers="$i"; while [ ${#leechers} -lt 5 ]; do leechers=" $leechers"; done; leechers[$c]=$leechers; p=0;;
149 esac
150 done
152 # produce rows of data
153 # prints a horizontal rule every five lines for better orientation
154 clear
157 echo -n "NO TORRENT"
158 while [ $j -lt $(($twidth-29)) ]; do
159 echo -n " "
160 let j++
161 done
162 echo "SIZE SEED LEECH"
165 while [ $j -lt $c ]; do
166 let j++
167 if [ $j -lt 10 ]; then j2=" $j"; else j2="$j"; fi
168 echo -e "$j2) ${torrentname[$j]} ${size[$j]} ${seeds[$j]} ${leechers[$j]}"
169 if [ $(($j % 5)) -eq 0 -a $j -ne 30 ]; then
170 echo "$hrule"
172 done
174 # user input and torrent download
175 echo; read -p "Enter the number(s) of the torrent(s) you wish to download (hit enter for next page, p for previous page, q for quit): " k
177 case "$k" in
178 q) loop=0; echo "Quitting...";;
179 x) loop=0; echo "Quitting...";;
180 p) let page--; echo "Getting previous page of results...";;
181 '') let page++; echo "Getting next page of results...";;
182 *) whattodowithtorrent;;
183 esac
185 # main script loop ends
186 done