forked from gotbletu/shownotes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtpb.sh
executable file
·166 lines (147 loc) · 4.17 KB
/
tpb.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/bin/sh
# demo: https://www.youtube.com/watch?v=IY56U1L6FCI
# source: https://github.com/saironiq/shellscripts
#
# 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=30
# 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 '^<td><a href=\"/torrent/[^>]*>.*|^<td><nobr><a href=\"[^"]*|<td align=\"right\">[^<]*' \
| sed 's!^<td><a href=\"/torrent/[^>]*>!!; \
s!</a>$!!; \
s!^<td><nobr><a href=\"!!; \
s!^<td [^>]*>!!; \
s! !\ !g; \
s/|/!/g' \
| sed 'N;N;N;N;s!\n!|!g'`
# number of results
n=`echo "$r" | wc -l`
IFS=$'\n'
# print results
echo "$r" \
| head -n "$limit" \
| awk -v N=1 \
-v NU="$numbcolor" \
-v NA="$namecolor" \
-v SI="$sizecolor" \
-v SE="$seedcolor" \
-v PE="$peercolor" \
-v NO="$nonecolor" \
-F '|' \
'{print NU N ") " NA $1 " " SI $3 " " SE $4 " " PE $5 NO; N++}'
# read ID(s), expand ranges, ignore everything else
read -p ">> Torrents to download (eg. 1 3 5-7): " selection
IFS=$'\n\ '
for num in $selection ; do
if [ "$num" = "`echo $num | grep -o '[[:digit:]][[:digit:]]*'`" ] ; then
down="$down $num"
elif [ "$num" = "`echo $num | grep -o '[[:digit:]][[:digit:]]*-[[:digit:]][[:digit:]]*'`" ] ; then
seqstart="${num%-*}"
seqend="${num#*-}"
if [ $seqstart -le $seqend ] ; then
down="$down `seq $seqstart $seqend`"
fi
fi
done
# normalize download list, sort it and remove dupes
down="$(echo $down | tr '\ ' '\n' | sort -n | uniq)"
IFS=$'\n'
# check whether we're downloading something, else exit
if [ -z "$down" ] ; then
exit 0
fi
# download all torrents in list
echo -n "Downloading torrent(s): "
for torrent in $down ; do
# check if ID is valid and in range of results, download torrent
if [ $torrent -ge 1 ] ; then
if [ $torrent -le $limit ] ; then
echo -n "$torrent "
command="$program `echo "$r" | awk -F '|' 'NR=='$torrent'{print $2; exit}'`"
status=$(eval "$command" 2>&1)
if [ $? -ne 0 ] ; then
echo -n '(failed!) '
report="$report\n(#$torrent) $status"
fi
fi
fi
done
echo
if [ -n "$report" ] ; then
echo -n "Exited with errors:"
echo -e "$report"
fi
unset IFS