-
Notifications
You must be signed in to change notification settings - Fork 1
/
NyaaTorrent-CLI-Manager.sh
executable file
·302 lines (275 loc) · 11.8 KB
/
NyaaTorrent-CLI-Manager.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/bin/bash
#
# NyaaTorrent-CLI-Manager is a simple BASH script to manage torrents on the
# website nyaa.se.
#
# Author : Benoît.S « Benpro » <benpro@benprobox.fr>
# Website : benprobox.fr
# Licence : GPLv3
# Configuration, edit these variables.
nyaa_account_anonymous=0 # Set to 1 if you doesn't want to authenticate to NyaaTorrent.
nyaa_account_login="" # Set your login.
nyaa_account_passwd="" # Set you password.
# Optional configuration.
nyaa_website="http://www.nyaa.se"
curl_cookie=$(mktemp)
curl_output=$(mktemp)
curl_useragent="NyaaTorrent-CLI-Manager - https://github.com/benpro/NyaaTorrent-CLI-Manager"
torrent_name=""
torrent_tracker="udp://tracker.openbittorrent.com:80/announce"
torrent_url=""
torrent_info_url=""
torrent_description="None."
torrent_remake=0 # Set to 1 if the torrent is a remake.
torrent_anonymous=0 # Set to 1 to upload pseudo-anonymously.
torrent_hidden=0 # Set to 1 to hide the torrent.
torrent_catid="1_37" # This the category of the torrent, it can be:
#1_32 Anime - Anime Music Video
#1_37 Anime - English-translated Anime
#1_38 Anime - Non-English-translated Anime
#1_11 Anime - Raw Anime
#2_12 Books - English-scanlated Books
#2_39 Books - Non-English-scanlated Books
#2_13 Books - Raw Books
#5_19 Live Action - English-translated Live Action
#5_22 Live Action - Live Action Promotional Video
#5_21 Live Action - Non-English-translated Live Action
#5_20 Live Action - Raw Live Action
#33_34 Lossless Audio - Lossless Albums
#33_35 Lossless Audio - Lossless Singles
#33_36 Lossless Audio - Lossless Soundtracks
#3_14 Lossy Audio - Lossy Albums
#3_16 Lossy Audio - Lossy Singles
#3_15 Lossy Audio - Lossy Soundtracks
#4_18 Pictures - Graphics
#4_17 Pictures - Photos
#6_23 Software - Applications
#6_24 Software - Games
torrent_rules=1 # Required elsewhere the torrent won't be uploaded!
# Do not touch the rest of this script if you don't known BASH/Shell scripting.
usage() {
# Show possible options, syntax and help.
echo -e "=^..^= NyaaTorrent-CLI-Manager =^..^=\n" \
"It seems that you want to get the help usage or you have made an error.\n\n" \
"Usage: $0 [options]\n" \
"Options:\n\n" \
"Adding a torrent:\n" \
"-d Optional: Set the torrent description. Default is \"$torrent_description\".\n" \
"-n Optional: Set the torrent name. Default read the name contained in the .torrent.\n" \
"-t Optional: Set the field tracker. Default to $torrent_tracker.\n" \
"-u Optional: Set the field \"Info URL\" of the torrent. Default is \"$torrent_info_url\".\n" \
"-c Optional: Set the field \"Category ID\" of the torrent. Default to $torrent_catid.\n" \
"-r Optional: Set the field \"Remake\" of the torrent. Can be 0 or 1. Default to $torrent_remake.\n" \
"-o Optional: Set the field \"Anonymous\" of the torrent. Can be 0 or 1. Default to $torrent_anonymous.\n" \
"-i Optional: Set the field \"Hidden\" of the torrent. Can be 0 or 1. Default to $torrent_hidden.\n\n" \
"-a /path/to/the/file.torrent or http://website.com/path/to/file.torrent - Add the torrent.\n" \
"Other:\n" \
"-l List your torrents.\n" \
"-g Get the latest torrents of your account.\n" \
"-h Show this help.\n"
}
checkDepends() {
# Verify that the system have the prerequisite tools
# This script won't install them, just warn you if you don't have the
# necessary tools.
curl=$(which curl 2>/dev/null)
if (($? != 0)); then
echo "Warning: curl not found, you may exit the script right now by pressing ^C." \
"Otherwise press Enter to continue."
read
fi
w3m=$(which w3m 2>/dev/null)
if (($? != 0)); then
echo "Warning: w3m not found, you may exit the script right now by pressing ^C." \
"Otherwise press Enter to continue."
read
fi
}
authenticate() {
# Authenticate on Nyaa and get cookie.
$curl -# \
"$nyaa_website/?page=login" \
-A "$curl_useragent" \
-d login="$nyaa_account_login" \
-d method="1" \
-d password="$nyaa_account_passwd" \
-d submit="Submit" \
-c $curl_cookie \
-o $curl_output
grep -F -o "Login successful" $curl_output &>/dev/null
if (($? != 0)); then
echo "Can't login, check your login and password!"
exit 1
fi
}
checkIfFileIsURL() {
# This permit to determine
# /path/to/file.torrent vs http://website.com/file.torrent
grep -F "http://" <<< $torrent_file &>/dev/null
if (($? == 0)); then
return 1
fi
return 0
}
addTorrent() {
# Add a torrent.
# If you want to be anonymous, the script will not proceed to
# authentication on nyaa.
if ((!$nyaa_account_anonymous)); then
authenticate
fi
# Check if the torrent is local or remotely (http).
if ((!checkIfFileIsURL)); then
curl_set_torrent="-F torrent=@$torrent_file;type=application/x-bittorrent"
else
curl_set_torrent="-F torrenturl=$torrent_file"
fi
$curl -# \
"$nyaa_website/?page=upload" \
-A "$curl_useragent" \
-F name="$torrent_name" \
$curl_set_torrent \
-F catid="$torrent_catid" \
-F info="$torrent_info_url" \
-F remake="$torrent_remake" \
-F anonymous="$torrent_anonymous" \
-F hidden="$torrent_hidden" \
-F description="$torrent_description" \
-F rules="$torrent_rules" \
-F submit="Upload" \
-b $curl_cookie \
-o $curl_output
# Getting links
link_info=$(grep -E -o 'http://www.nyaa.se/\?page=view&tid=[0-9]+' $curl_output | head -1 | sed s'/\&/\&/')
link_download=$(sed "s/view/download/" <<< $link_info)
[ -z $link_download ] && return 1
return 0
}
listTorrents() {
# Lists your torrents.
# Get the User ID.
[ ! -s $curl_cookie ] && authenticate
user_id=$(grep -o "id\s\+[0-9]\+" $curl_cookie | cut -f2)
# Get the first page + number of pages.
$curl -# \
"$nyaa_website/?page=torrents&user=$user_id" \
-b $curl_cookie \
-o $curl_output
totalPages=$(grep -oe "offset=[0-9]*" $curl_output | tail -1 | cut -d"=" -f2)
# Displaying with w3m.
for i in $(seq 1 $totalPages); do
$curl -# \
"$nyaa_website/?page=torrents&user=$user_id&offset=$i" \
-b $curl_cookie \
-o $curl_output
$w3m $curl_output -T "text/html" -dump -cols 150
done
}
#
# latestTorrent() {
#
# # Get the link to the latest torrent in your account.
# }
egg() {
echo -e \
"\033[48;5;17m\n" \
" \n" \
" \n" \
" \033[48;5;0m \033[48;5;17m\n" \
" \033[48;5;9m \033[48;5;0m \033[48;5;230m \033[48;5;0m \033[48;5;17m\n" \
"\033[48;5;9m \033[48;5;0m \033[48;5;230m \033[48;5;175m \033[48;5;230m \033[48;5;0m \033[48;5;17m\n" \
"\033[48;5;9m \033[48;5;0m \033[48;5;230m \033[48;5;175m \033[48;5;162m \033[48;5;175m \033[48;5;162m \033[48;5;175m \033[48;5;230m \033[48;5;0m \033[48;5;17m\n" \
"\033[48;5;202m \033[48;5;0m \033[48;5;230m \033[48;5;175m \033[48;5;162m \033[48;5;175m \033[48;5;0m \033[48;5;175m \033[48;5;162m \033[48;5;175m \033[48;5;230m \033[48;5;0m \033[48;5;17m \033[48;5;0m \033[48;5;17m\n" \
"\033[48;5;202m \033[48;5;0m \033[48;5;230m \033[48;5;175m \033[48;5;0m \033[48;5;8m \033[48;5;0m \033[48;5;175m \033[48;5;230m \033[48;5;0m \033[48;5;8m \033[48;5;0m \033[48;5;17m\n" \
"\033[48;5;202m \033[48;5;0m \033[48;5;11m \033[48;5;0m \033[48;5;230m \033[48;5;175m \033[48;5;162m \033[48;5;175m \033[48;5;0m \033[48;5;8m \033[48;5;175m \033[48;5;230m \033[48;5;0m \033[48;5;8m \033[48;5;0m \033[48;5;17m\n" \
"\033[48;5;11m \033[48;5;8m \033[48;5;0m \033[48;5;11m \033[48;5;0m \033[48;5;230m \033[48;5;175m \033[48;5;0m \033[48;5;8m \033[48;5;0m \033[48;5;8m \033[48;5;0m \033[48;5;17m\n" \
"\033[48;5;11m \033[48;5;0m \033[48;5;8m \033[48;5;0m \033[48;5;230m \033[48;5;175m \033[48;5;0m \033[48;5;8m \033[48;5;0m \033[48;5;17m\n" \
"\033[48;5;11m \033[48;5;0m \033[48;5;8m \033[48;5;0m \033[48;5;230m \033[48;5;175m \033[48;5;162m \033[48;5;0m \033[48;5;8m \033[48;5;0m \033[48;5;17m\n" \
"\033[48;5;10m \033[48;5;0m \033[48;5;8m \033[48;5;0m \033[48;5;230m \033[48;5;175m \033[48;5;162m \033[48;5;175m \033[48;5;0m \033[48;5;8m \033[48;5;15m \033[48;5;0m \033[48;5;8m \033[48;5;15m \033[48;5;0m \033[48;5;8m \033[48;5;0m \033[48;5;17m\n" \
"\033[48;5;10m \033[48;5;0m \033[48;5;230m \033[48;5;175m \033[48;5;0m \033[48;5;8m \033[48;5;0m \033[48;5;8m \033[48;5;0m \033[48;5;8m \033[48;5;0m \033[48;5;8m \033[48;5;0m \033[48;5;17m\n" \
"\033[48;5;10m \033[48;5;33m \033[48;5;0m \033[48;5;230m \033[48;5;175m \033[48;5;162m \033[48;5;175m \033[48;5;0m \033[48;5;8m \033[48;5;175m \033[48;5;8m \033[48;5;175m \033[48;5;0m \033[48;5;17m\n" \
"\033[48;5;33m \033[48;5;0m \033[48;5;230m \033[48;5;175m \033[48;5;0m \033[48;5;8m \033[48;5;0m \033[48;5;8m \033[48;5;0m \033[48;5;17m\n" \
"\033[48;5;15m \033[48;5;33m \033[48;5;19m \033[48;5;0m \033[48;5;230m \033[48;5;0m \033[48;5;8m \033[48;5;0m \033[48;5;17m\n" \
"\033[48;5;19m \033[48;5;0m \033[48;5;8m \033[48;5;0m \033[48;5;17m\n" \
"\033[48;5;19m \033[48;5;0m \033[48;5;8m \033[48;5;0m \033[48;5;17m \033[48;5;0m \033[48;5;8m \033[48;5;0m \033[48;5;17m \033[48;5;0m \033[48;5;8m \033[48;5;0m \033[48;5;17m \033[48;5;0m \033[48;5;8m \033[48;5;0m \033[48;5;17m\n" \
"\033[48;5;19m \033[48;5;15m \033[48;5;19m \033[48;5;17m \033[48;5;0m \033[48;5;17m \033[48;5;0m \033[48;5;17m \033[48;5;0m \033[48;5;17m \033[48;5;0m \033[48;5;17m\n" \
" \n" \
" \n" \
" \033[1;37mNyanTorrent-CLI-Manager - by Benpro\033[48;5;17m"
# Thanks to https://github.com/klange/nyancat/blob/master/src/nyancat.c :)
}
clean() {
# Do some cleaning.
[ -f $curl_cookie ] && rm $curl_cookie
[ -f $curl_output ] && rm $curl_output
}
# Application entry point, script start here! \_o>
# Firstly check if there is parameters are set.
if (($# == 0)); then
usage
exit 1
fi
# Secondly checks the dependences.
checkDepends
# Then set a trap to do some cleaning when the script exit.
trap 'clean;' SIGINT SIGTERM EXIT
# Getopts to handle options and parameters.
while getopts "d:n:t:u:c:r:o:i:a: e l g h" opt; do
case $opt in
d)
torrent_description=$OPTARG
;;
n)
torrent_name=$OPTARG
;;
t)
torrent_tracker=$OPTARG
;;
u)
torrent_info_url=$OPTARG
;;
c)
torrent_catid=$OPTARG
;;
r)
torrent_remake=$OPTARG
;;
o)
torrent_anonymous=$OPTARG
;;
i)
torrent_hidden=$OPTARG
;;
a)
torrent_file=$OPTARG
addTorrent
if (($? == 0)); then
echo "Ok, torrent added successfully!"
echo "Link to torrent informations: \"$link_info\"."
echo "Link to download torrent: \"$link_download\"."
else
echo "Uh oh, something went wrong when adding the torrent!"
fi
;;
e)
egg
;;
l)
listTorrents
;;
g)
latestTorrent
;;
h)
usage
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done