summaryrefslogtreecommitdiff
path: root/shell-rss-torrent
blob: 16d416c68624162287ec07b34128273e0b00d376 (plain)
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/bin/sh

echo "
------------------------
 SHELL RSS TORRENT v2.5
   Script by Rafostar
------------------------
"

INDENT1="  "
INDENT2="     "
UPPER="ABCDEFGHIJKLMNOPQRSTUVWXYZ/ĄĆĘŁŃÓŚŹŻ"
LOWER="abcdefghijklmnopqrstuvwxyz/ąćęłńóśźż"
UA="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/72.0"

parse_config() {
    WatchDir=$(echo "$ConfigData" | xmllint --xpath "string(//config/watchdir[1]/text())" - 2> /dev/null)
    if [ "$?" -ne 0 ]
    then
        echo "Error: invalid config file"
        exit 1
    fi

    Downloader=$(xmllint_from_config "string(//config/downloader[1]/text())")
    if [ ! -z "$Downloader" ]
    then
        echo "Using <downloader> tag command"
        DwnPrint=$(xmllint_from_config "count(//config/downloader[1][@print='1'])")

        if [ -z "$WatchDir" ]
        then
            echo "Downloading without <watchdir> tag"
        fi
    else
        if [ ! -z "$WatchDir" ]
        then
            echo "Torrents will be downloaded to: $WatchDir"
        else
            echo "Error: no <watchdir> in config file"
            exit 1
        fi
    fi

    HistoryPath=$(xmllint_from_config "string(//config/history[1]/text())")
    if [ ! -z "$HistoryPath" ]
    then
        echo "Using download history file: $HistoryPath"

        if [ -f "$HistoryPath" ]
        then
            HistoryData=$(cat "$HistoryPath")
        fi
    fi

    TimeMax=$(xmllint_from_config "string(//config/time-max[1]/text())")
    if [ ! -z "$TimeMax" ]
    then
        echo "Downloading releases within seconds: $TimeMax"
    fi

    FeedsCount=$(xmllint_from_config "count(//config/feed)")
    echo "Total feeds in config: $FeedsCount"
    echo ""

    feed=1
    while [ $feed -le $FeedsCount ]
    do
        FeedUrl=$(xmllint_from_config "string(//config/feed[$feed]/@url)")
        if [ -z "$FeedUrl" ]
        then
            echo "Error: feed $feed has no url"
        else
            FeedUser=$(xmllint_from_config "string(//config/feed[$feed]/@user)")
            if [ ! -z "$FeedUser" ]
            then
                FeedUser="--user=$FeedUser"
            fi

            FeedPass=$(xmllint_from_config "string(//config/feed[$feed]/@pass)")
            if [ ! -z "$FeedPass" ]
            then
                FeedPass="--password=$FeedPass"
            fi

            FeedData=$(wget "$FeedUser" "$FeedPass" -U "$UA" -O - "$FeedUrl" 2> /dev/null)
            if [ -z "$FeedData" ]
            then
                FeedData=$(wget "$FeedUser" "$FeedPass" -O - "$FeedUrl" 2> /dev/null)
            fi

            if [ -z "$FeedData" ]
            then
                echo "Error: feed $feed has no data"
            else
                echo "feed $feed: $FeedUrl"

                for QueryFn in "starts-with" "contains"
                do
                    find_in_feed $feed
                done

                find_in_feed_multi $feed
            fi
        fi

        feed=$(( feed + 1 ))
    done
}

find_in_feed() {
    QueryPath="//config/feed[$1]/$QueryFn"
    QueryCount=$(xmllint_from_config "count($QueryPath)")

    if [ "$QueryCount" -ge 1 ]
    then
        query=1
        while [ $query -le $QueryCount ]
        do
            find_attributes_matches $query
            TorrentCount=$(xmllint_from_feed "count($Matches/link)")
            echo "$INDENT1 $QueryFn $query: $QueryText, found: $TorrentCount"

            if [ "$TorrentCount" -ge 1 ]
            then
                get_torrents
            fi

            query=$(( query + 1 ))
        done
    fi
}

find_in_feed_multi() {
    MultiPath="//config/feed[$1]/multi"
    MultiCount=$(xmllint_from_config "count($MultiPath)")

    if [ "$MultiCount" -ge 1 ]
    then
        multi=1
        while [ $multi -le $MultiCount ]
        do
            MultiText=""
            CustomFeed=""

            for QueryFn in "starts-with" "contains"
            do
                QueryPath="$MultiPath[$multi]/$QueryFn"
                QueryCount=$(xmllint_from_config "count($QueryPath)")

                if [ "$QueryCount" -ge 1 ]
                then
                    query=1
                    while [ $query -le $QueryCount ]
                    do
                        find_attributes_matches "$query"
                        CustomFeed=$(xmllint_from_feed "$Matches")

                        if [ ! -z "$MultiText" ]
                        then
                            MultiText="$MultiText | $QueryText"
                        else
                            MultiText="$QueryText"
                        fi

                        if [ ! -z "$CustomFeed" ]
                        then
                            CustomFeed="<rss>$CustomFeed</rss>"
                        else
                            CustomFeed=""
                            echo "$INDENT1 multi $multi: $MultiText, found: 0"
                            break
                        fi

                        query=$(( query + 1 ))
                    done
                fi
            done

            if [ ! -z "$CustomFeed" ]
            then
                get_torrents_multi $multi
                CustomFeed=""
            fi

            multi=$(( multi + 1 ))
        done
    fi
}

find_attributes_matches() {
    QueryText=$(xmllint_from_config "string($QueryPath[$1]/text())")
    IgnoreCase=$(xmllint_from_config "count($QueryPath[$1][@ignore-case='1'])")

    if [ "$IgnoreCase" -eq 1 ]
    then
        ParsedQuery=$(xmllint_from_config "translate($QueryPath[$query]/text(), '$UPPER', '$LOWER')")
        Matches="//item[title[$QueryFn(translate(., '$UPPER', '$LOWER'), '$ParsedQuery')]]"
    else
        Matches="//item[title[$QueryFn(., '$QueryText')]]"
    fi
}

find_torrent_attr() {
    ATTR=$(xmllint_from_feed "count($Matches[$torrent]/$1)")
    if [ "$ATTR" -eq 1 ]
    then
        ATTR_TORRENT=$(xmllint_from_feed "count($Matches[$torrent]/$1[@type='application/x-bittorrent'])")
        if [ "$ATTR_TORRENT" -eq 1 ]
        then
            ATTR_URL=$(xmllint_from_feed "string($Matches[$torrent]/$1/@url)")
            if [ ! -z "$ATTR_URL" ]
            then
                echo "$ATTR_URL"
            fi
        fi
    fi
}

get_torrents() {
    torrent=1
    while [ $torrent -le $TorrentCount ]
    do
        TorrentLink=$(find_torrent_attr "enclosure")

        if [ -z "$TorrentLink" ]
        then
            TorrentLink=$(xmllint_from_feed "string($Matches[$torrent]/link/text())")
        fi

        if [ ! -z "$TorrentLink" ]
        then
            if [ ! -z "$HistoryData" ]
            then
                case "$HistoryData" in
                    *"$TorrentLink"*) echo "$INDENT2 torrent $torrent in download history" ;;
                    *) try_download ;;
                esac
            else
                try_download
            fi
        else
            echo "$INDENT2 torrent $torrent without a link"
        fi

        torrent=$(( torrent + 1 ))
    done
}

get_torrents_multi() {
    TorrentCount=$(xmllint_from_feed "count($Matches/link)")
    echo "$INDENT1 multi $1: $MultiText, found: $TorrentCount"

    if [ "$TorrentCount" -ge 1 ]
    then
        get_torrents
    fi
}

xmllint_from_config() {
    RESULT=$(echo "$ConfigData" | xmllint --xpath "$1" - 2> /dev/null)
    echo "$RESULT"
}

xmllint_from_feed() {
    if [ ! -z "$CustomFeed" ]
    then
        XmlData="$CustomFeed"
    else
        XmlData="$FeedData"
    fi

    RESULT=$(echo "$XmlData" | xmllint --format --xpath "$1" - 2> /dev/null)
    echo "$RESULT"
}

write_to_history() {
    if [ ! -z "$HistoryPath" ]
    then
        echo "$1" >> "$HistoryPath"
    fi

    HistoryData=$(echo -e "$HistoryData\n$1")
}

try_download() {
    if [ ! -z "$TimeMax" ]
    then
        check_date
    else
        download_torrent "$TorrentLink"
    fi
}

check_date() {
    DateCount=$(xmllint_from_feed "count($Matches[$torrent]/pubDate)")
    if [ "$DateCount" -ge 1 ]
    then
        PubDate=$(xmllint_from_feed "string($Matches[$torrent]/pubDate/text())")
        if [ ! -z "$PubDate" ]
        then
            TorrentEpoch=$(date -d "$PubDate" +%s)
            CurrentEpoch=$(date +%s)
            Elapsed=$((CurrentEpoch - TorrentEpoch))
            if [ "$Elapsed" -le $TimeMax ]
            then
                download_torrent "$TorrentLink"
            else
                echo "$INDENT2 torrent $torrent is too old"
            fi
        fi
    else
        download_torrent "$TorrentLink"
    fi
}

download_torrent() {
    echo "$INDENT2 torrent $torrent: $1"

    if [ ! -z "$Downloader" ]
    then
        Title=$(xmllint_from_feed "string($Matches[$torrent]/title/text())")

        if [ "$DwnPrint" -eq 1 ]
        then
            eval "$Downloader "$1""
        else
            eval "$Downloader "$1"" > /dev/null 2> /dev/null
        fi

        if [ $? -eq 0 ]
        then
            write_to_history "$1"
        else
            echo "$INDENT1 $INDENT2 download failed"
        fi
    else
        wget -q --content-disposition -U "$UA" -P "$WatchDir" "$1" 2> /dev/null
        if [ $? -ne 0 ]
        then
            wget -P "$WatchDir" "$1" > /dev/null 2> /dev/null
            if [ $? -eq 0 ]
            then
                write_to_history "$1"
            else
                echo "$INDENT1 $INDENT2 download failed"
            fi
        else
            write_to_history "$1"
       fi
    fi
}

ConfigPath=$1
if [ -z "$ConfigPath" ]
then
    ME=$(basename "$0")
    echo "Usage: $ME <config_path>"
else
    if [ -f "$ConfigPath" ]
    then
        XmlInstalled=$(xmllint --version > /dev/null 2> /dev/null)
        if [ $? -eq 0 ]
        then
            ConfigData=$(cat "$ConfigPath")
            parse_config
        else
            echo "Error: xmllint is not installed"
            exit 1
        fi
    else
        echo "Error: file $ConfigPath does not exist"
        exit 1
    fi
fi