-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdmzj.down.sh
executable file
·244 lines (225 loc) · 5.68 KB
/
dmzj.down.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
#!/bin/bash
ID=0
IFLIST=false
IFDOWN=false
DOWNID=""
DOWNALL=false
FIRSTLETTER=""
JSOUT=false
export MAIN_PID=$$
if [ ! -d "~/.dmzj" ]; then
mkdir ~/.dmzj
fi
isNotNum() {
if [[ $# == 0 ]]; then
return 2
fi
if [ -n "$(echo $1 | sed '/^[0-9][0-9]*$/d')" ]; then
return 0
fi
return 1
}
getParams(){
if [[ $# == 0 ]]; then
printf 'Message: id needed\n'
defer 0
fi
while (( $# >= 1 ))
do
case $1 in
-i | --id )
#Get id of manga
shift
ID=$1
if [[ $ID == "" ]]; then
printf 'Error: ID empty'
# elif [ -n "$(echo $ID | sed '/^[0-9][0-9]*$/d')" ]; then
elif isNotNum $ID; then
printf "Error: ID \`%s' not number\n" $ID
defer 1
fi
shift
;;
-l | --list )
#List contents
IFLIST=true
shift
;;
-I | --chapter-id )
#Get the id of chapters for downloading
IFDOWN=true
shift
if [[ $1 == "all" ]]; then
DOWNALL=true
fi
DOWNID=$@
break
;;
-f | --first-letter)
#Get first letter
shift
FIRSTLETTER=$1
shift
;;
-j | --json)
#out put json info file
shift
JSOUT=true
IFLIST=true
;;
-h | --help)
USAGE
;;
*)
echo Unknown param:$1
shift
;;
esac
done
}
USAGE() {
printf "Help:\n
-i or -id xxx : specific comic id you want to process (must have)\n
-l or --list : list info of this comic\n
-I or --chaptere-id xxx xxx ... : install the chapters of a comic specified by -i, following \`all\` to download all\n
-f or --first-letter x : specify the first letter of the comic when downloading, if it's not set, the script will get it online\n
-j or --json: output the info of comic in json and defer, if it's set, no download will process\n
-h or --help: print this help\n"
defer 0
}
DownLink() {
F=$1 #first letter
ID=$2
index=$3
printf "Downloading: ID %6d,\tchapter id:%6d\n" $ID $index
wget -c -t 1 "http://images.dmzj.com/$F/$ID/$index.zip" --header Host:imgzip.dmzj.com
if [[ $? == 4 ]]; then
RaiseError "RMD"
fi
}
CharacterLen() {
chnum=$(($(echo $1 | wc -m ) -1))
chlen=$(($(echo $1 | wc -c ) -1))
zhnum=$(( ($chlen - $chnum) /2 ))
ennum=$(( $chnum - $zhnum ))
echo $(( $zhnum*2 + $ennum ))
}
printInfo() {
json=$1
#$2 tells whether ouput pure json
if [[ $2 == true ]]; then
echo $json | jq .
return
fi
ifhidden=$(echo "$json" | jq '.hidden' )
is_lock=$(echo "$json" | jq '.is_lock')
first_letter=$(echo "$json" | jq '.first_letter')
comic_py=$(echo "$json" | jq '.comic_py')
name=$(echo "$json" | jq '.title')
ID=$(echo "$json" | jq '.id')
length=$(echo "$json" | jq '[.chapters[0].data[]] | length' )
tags=$(echo "$json" | jq -c '[.types[].tag_name] | join(",")' 2>/dev/null | sed 's/"//g' )
describe=$(echo "$json" | jq .description 2>/dev/null | sed 's/"//g')
if [[ $ifhidden == 1 || $is_lock == 1 ]]; then
printf "\033[43;91mWARNNING\033[0m\033[33m This Comic is hiddeen or locked by 动漫之家, please get comic info at night!(Around 20 o'clock at night)\033[0m\n"
fi
namelength=$(( $(CharacterLen $name) + 20))
printf "\033[32m NAME\033[0m %*s\t\033[32m ID\033[0m %s\n" $namelength "$name" $ID
printf "\033[32m LOCK STATUS\033[0m %*d\t\033[32m HIDE STATUS\033[0m %s\n" $namelength $is_lock $ifhidden
printf "\033[32mCOMIC STRING\033[0m %*s\t\033[32mFIRST LETTER\033[0m %s\n" $namelength $comic_py $first_letter
printf "\033[32m Tags\033[0m %s\n" $tags
printf "\033[32m Description\033[0m %s\n" $describe
printf "%5s\t%s\n" id name
echo $json | jq '.chapters[0].data[] | [(.chapter_id|tostring),.chapter_title] | join(" ")' |
sed -e $'s/"//g
s/ /\t/g' | sort
printf "\033[33mtotal\033[0m:%d\n" $length
}
getFirstLetter() {
json=$1
echo "$json" | jq '.first_letter' | sed 's/"//g'
}
fetchJsInfo() {
ID=$1
if isNotNum $ID ; then
echo "ID ($ID) not num"
defer 1
fi
res=$(curl "http://v3api.dmzj.com/comic/comic_$ID.json" 2>/dev/null)
if [[ ! $(echo $res | jq . 2>/dev/null) ]]; then
RaiseError "NJF" $res
fi
echo $res
}
getAllIndex() {
json=$1
echo $json | jq '.chapters[0].data[] | [.chapter_id | tostring] | join(" ")' | sed 's/"//g'
}
printNotice() {
printf "\033[41;93mNOTICE\033[0m\033[33m Some Comic has been hidden or lock by 动漫之家 due to \033[31mDMCA\033[33m or local law, usually you can try fetching the comic info again at night (About 20 o'clock)\033[0m\n"
defer 1
}
printDownErr() {
printf "\033[41;33mFAILURE\033[0m\033[33m Downloading failed: This manga might be removed by 动漫之家\033[0m\n"
}
defer() {
rm -rf ~/.dmzj
exit $1
}
RaiseError() {
kill -s TERM $MAIN_PID
echo $1 > ~/.dmzj/err
echo $2 > ~/.dmzj/err_msg
}
HandleError() {
ERROR=$(cat ~/.dmzj/err)
ERROR_MSG=$(cat ~/.dmzj/err_msg)
case "$ERROR" in
"HoL") # hidden or locked
printNotice
;;
"RMD") # removed by dmzj
printDownErr
defer 2
;;
"NJF") #not json file
printf "\033[33mERROR: Not a json file\033[0m: %s\n", $ERROR_MSG 1>&2
printNotice
defer 3
;;
esac
echo > ~/.dmzj/err
echo > ~/.dmzj/err_msg
# empty the $ERROR_MESSAGE
}
getParams $@
trap 'HandleError' TERM
if [[ $IFLIST == true ]]; then
printInfo "$(fetchJsInfo $ID)" $JSOUT
elif [[ $IFDOWN == true ]]; then
echo downloading
js=""
if [[ "$FIRSTLETTER" == "" ]]; then
js=$(fetchJsInfo $ID)
FIRSTLETTER=$(getFirstLetter "$js")
printf "Using first letter: \`%s'\n" "$FIRSTLETTER"
fi
if [[ $DOWNALL == true ]]; then
echo downloads all
if [[ $js == "" ]]; then
js=$(fetchJsInfo $ID)
fi
for id in $(getAllIndex "$js")
do
DownLink "$FIRSTLETTER" $ID $id
done
else
echo downloads: $DOWNID
for id in $DOWNID
do
DownLink "$FIRSTLETTER" $ID $id
done
fi
elif [[ $IFDOWN == false && $IFLIST == false ]];then
printf "Do you mean \`dmzj.down.sh -i %s -l\` ?\n" $ID
fi