-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhorribledl.sh
executable file
·132 lines (114 loc) · 2.74 KB
/
horribledl.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
#!/usr/bin/env bash
# Dependency: xmlstarlet, curl, fzf, aria2c
baseurl="http://www.horriblesubs.info/rss.php"
res="720" # default resolution
btdler=/usr/bin/aria2c
filter=""
fzf_options="--reverse"
if [ -n "$XDG_DATA_HOME" ]; then
storage=$XDG_DATA_HOME/horribledl
else
storage=/home/japorized/.data/horribledl
fi
if [ ! -d $storage ]; then
echo "${storage} does not exist. Creating directory."
mkdir $storage
fi
usage() {
echo " horribledl.sh [OPTIONS]
OPTIONS:
-r Resolution [options: all, sd, 720, 1080] (default: ${res})
-R Refresh rss data before download (works alongside -r)
-U Refresh rss data only (works alongside -r)
-s Filter for show name
-f Force mode (no questions asked)"
}
updateRSS() {
echo "Downloading rss-${res}.xml"
url="${baseurl}?res=${res}"
curl "$url" > $storage/rss-${res}.xml
}
_fzf() {
fzf ${fzf_options}
}
f_flag=false
while getopts "r:RUs:f" opt; do
case "${opt}" in
r) res="$OPTARG" ;;
R)
updateRSS
if [ $? -ne 0 ]; then
echo "Database update failed"
exit 1
fi
;;
U)
updateRSS
exit 0
;;
s) filter="$OPTARG" ;;
f) f_flag=true ;;
\?)
usage
exit 1
;;
esac
done
shift $((OPTIND -1))
# set down working files
rssfile=$storage/rss-${res}.xml
tmpshowlist=$storage/tmp-showlist.txt
# check if rssfile exists
# download rss file if dne
if [ ! -f $rssfile ]; then
echo "${rssfile} does not exist. Creating file."
updateRSS
fi
# load parsed rss into memory
IFS=$'\n'
shows=($(/usr/bin/xmlstarlet sel -t -m "/rss/channel/item" -v title -n $rssfile))
links=($(/usr/bin/xmlstarlet sel -t -m "/rss/channel/item" -v link -n $rssfile))
unset IFS
rm $tmpshowlist # rm existing tmpfile of shows
# list files into tmpfile with respective index
for showidx in "${!shows[@]}"
do
if [ -n "$filter" ]; then
grepres=$(echo ${shows[$showidx]} | grep "$filter")
if [ -n "$grepres" ]; then
echo $showidx "${shows[$showidx]}" >> $tmpshowlist
fi
else
echo $showidx "${shows[$showidx]}" >> $tmpshowlist
fi
done
# query user
only_one_choice() {
lines=$(wc -l $tmpshowlist | cut -d' ' -f1)
test $lines = 1
}
if (only_one_choice); then
sel=$(cat $tmpshowlist)
else
sel=$(cat $tmpshowlist | _fzf)
fi
val=$?
if [ $val = 130 ]; then
echo "Cancelled"
exit 1
fi
selno=$(echo $sel | cut -d' ' -f1) # get selected number
if (! $f_flag); then
echo "Download ${shows[$selno]}?"
read confirm
confirm=$(echo $confirm | tr '[:upper:]' '[:lower:]')
case "${confirm}" in
n|"no")
echo "Cancelled"
exit 0
;;
esac
fi
magnet=$(echo ${links[$selno]} | sed 's/\&/\&/g') # get magnet of selected show
# echo $magnet # uncomment for debug
${btdler} "$magnet" # download