forked from rhinospray/extract_torrents
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_torrents.sh
167 lines (156 loc) · 5.63 KB
/
extract_torrents.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
#!/bin/bash
#--Default configuration data: modify for your installation
SessionDir="/home/rhino/.sessions/"
DownloadDir="/home/rhino/torrents/"
OutputBaseDir="/home/rhino/.torrents/"
#------------------USAGE HELP------------------------------------------------->
function usage {
cat <<EOM
Usage: $(basename "$0") [OPTION]...
This script will extract infohash.torrents from the .session directory, rename
them to their original name (or optionally like the downloaded files).
The extracted .torrents will be copied to a base output directory with the same
subdirectory structure than their downloaded files.
-a Use an alternative name for the .torrent based on the downloaded
filename or directory (for multi-file downloads).
This makes easier to associate .torrents and downloads.
-x Execute the commands. Without this switch the script will only
show the commands without executing them.
-t TRACKER Extract only .torrents from TRACKER. Use the tracker name as it
appears in rutorrent (i.e. hdbits.org apollo.rip ....)
-l LABEL Extract only .torrents with rutorrent label LABEL.
-s PATH Full path of the .session directory including trailing slash.
Default: "$SessionDir"
-d PATH Full path of the Download base directory including trailing slash.
Default: "$DownloadDir"
-o PATH Full path of the Output base directory including trailing slash.
Default: "$OutputBaseDir"
-h Display this help
EOM
exit 2
}
#------------------PARSE OPTIONS---------------------------------------------->
# init switch flags
filterTracker=""
filterLabel=""
altname=0
execute=0
while getopts ":s:d:o:t:l:axh" optKey; do
case $optKey in
s)
SessionDir="$OPTARG"
if [ ! -d "$SessionDir" ];then
echo "ERROR: Parameter -s path ($SessionDir) doesn't exist"
exit 2
fi
;;
d)
DownloadDir="$OPTARG"
if [ ! -d "$DownloadDir" ];then
echo "ERROR: Parameter -d path ($DownloadDir) doesn't exist"
exit 2
fi
;;
o)
OutputBaseDir="$OPTARG"
;;
t)
filterTracker="$OPTARG"
echo "Will extract only .torrents for $filterTracker"
;;
l)
filterLabel="$OPTARG"
;;
a)
altname=1
echo "Will use alternative names for the .torrents"
;;
x)
execute=1
;;
h|*)
usage
;;
esac
done
shift $((OPTIND - 1))
#------------------DEFINE FUNCTIONS------------------------------------------->
# sustitute urlencoded characters like %20 by normal characters
urldecode() { : "${*//+/ }"; echo -e "${_//%/\\x}"; }
# extract the value of key $1 from a bencoded file $2 ej: bdecode x-filename bencoded.txt
bdecode() {
local key="${#1}:$1"
local value=$(sed -nE -e "s/^.*$key//p" $2)
local valueLen=$(echo $value | cut -d: -f1)
local valueText=$(echo "$value" | cut -d: -f2-)
if [ "$valueLen" = "0" ]; then
echo ""
else
echo "$valueText" | cut -c1-$valueLen
fi
}
# quote special metacharacters with \
metaquote() {
printf %s "$1" | sed 's/[][{}()\/.^$?*+]/\\&/g'
}
#------------------MAIN CODE-------------------------------------------------->
# --be sure that there is a trailing slash on the paths
SessionDir="${SessionDir%/}/"
DownloadDir="${DownloadDir%/}/"
OutputBaseDir="${OutputBaseDir%/}/"
# --process the .torrent files in .session
for torfile in "$SessionDir"*.torrent
do
rtorfile="$torfile.rtorrent"
# --extract tracker name (as shown in rutorrent, i.e. hdbits.org ) from hash.torrent (key=announce)
announce="$(bdecode announce $torfile)"
domain="$(echo $announce | sed -e "s/[^/]*\/\/\([^@]*@\)\?\([^:/]*\).*/\2/")"
tracker="$(echo $domain | rev | cut -d. -f-2 | rev )"
if [ "$filterTracker" != "" ] && [ "$filterTracker" != "$tracker" ] ; then
printf "."
continue
fi
# --extract rutorrent label from hash.rtorrent (key=custom1
label="$(bdecode custom1 $rtorfile)"
label="$(urldecode $label)"
if [ "$filterLabel" != "" ] && [ "$filterLabel" != "$label" ] ; then
printf "."
continue
fi
# --extract the original xxx.torrent name from hash.rtorrent (key=x-filename, urlencoded)
TorrName="$(bdecode x-filename $rtorfile)"
TorrName=$(urldecode $TorrName)
# --extract the filename (or directory if multifile) of the download from hash.torrent (key=name)
DownName=$(bdecode name $torfile)
DownNameQ=$(metaquote "$DownName")
# --extract full download path from hash.rtorrent (key=directory)
DownPath="$(bdecode directory $rtorfile)"
# -- take out the last path segment when it is a multifile download to a directory
DownPathS=$(printf %s "$DownPath" | sed -E -e "s/$DownNameQ//g")
# -- take out the trailing slash to unify (sometimes is not present)
DownPathS=$(printf %s "$DownPathS" | sed -e "s/\/$//g")
# -- replace DownloadDir with OutputBaseDir
DownloadDirQ=$(metaquote "$DownloadDir")
OutputBaseDirQ=$(metaquote "$OutputBaseDir")
OutputPath=$(printf %s "$DownPathS" | sed -E -e "s/$DownloadDirQ/$OutputBaseDirQ/g")
# --extract hash file name
HashName=$(echo "$torfile" | grep -o '[^/]*$' )
# echo "----- $torfile"
# echo "DOMAIN=$domain TRACKER=$tracker LABEL=$label"
# echo "REALNAME=$TorrName"
# echo "ALTNAME=$DownName"
# echo "ALTNAMEQ=$DownNameQ"
# echo "TARGETDIR=$OutputPath"
# --use alternative name if requested or when real name not valid
if [ $altname -eq 1 ] || [ "$TorrName" == "" ] || [ "$TorrName^^" == "$HashName^^" ] ; then
TorrName="$DownName.torrent"
fi
# --execute the mkdir/cp commands if -x, or just explain what will do
if [ $execute -eq 1 ]; then
printf "o"
mkdir -p "$OutputPath"
cp "$torfile" "$OutputPath/$TorrName"
else
printf "\ncp $HashName to $OutputPath/$TorrName"
fi
done