-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanga.sh
executable file
·97 lines (76 loc) · 2.44 KB
/
manga.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
#!/usr/bin/env bash
#TODO: setup an XML configuration file
#TODO: remove xml option
# default target directory
TARGET=`pwd`
# download none, by default
ALL=''
# initialize array of titles
TITLES=( )
function usage {
cat <<-EOF
Usage: manga [options] <manga title> [<manga title> ...]
The titles don't need to be accurate, and if it has more than one word,
enclose it with quotes (eg: "liar game").
You can insert the chapters interactively, or use the option --all to
download them all.
Options:
-h, --help Display this help and exit
-a, --all Download all chapters for given titles
-d, --directory <target dir> Target directory, if not set uses pwd
-x, --xml <config path> Path of the XML configuration file
(please see manga_downloader example)
This script uses:
- manga_downloader (git://github.com/jiaweihli/manga_downloader)
EOF
}
function download_manga {
# get the dirname of this script, so we know manga_downloader's path
# we use readlink so, in case this is called with a link, we still get the
# real path
local this_dir=`dirname $(readlink -f $0)`
local dest_zip='/tmp/manga'
local dest_img=''
mkdir $dest_zip
# clean temporary directory in case it already existed
rm -rf "$dest_zip/*"
# download zip file to temporary directory
python "$this_dir/manga_downloader/src/manga.py" --zip $ALL \
-d $dest_zip "${TITLES[@]}"
# if we get an error status from python, exit
if [[ $? != 0 ]]; then
exit $?
fi
# unzip files
# list 1 file per line
for zip in `ls -1 $dest_zip/*.zip`; do
local filename=`basename "$zip"`
# get the filename without the extension
local dirname="${filename%.*}"
unzip -d "$dest_zip/$dirname" "$zip"
# use `convert` to generate pdf files in the target directory
convert "$dest_zip/$dirname/*" "$TARGET/$dirname.pdf"
done
# delete temporary directory
rm -rf "$dest_zip"
}
while [[ $# -ne 0 ]]; do
option=$1
shift
case $option in
# display help message
-h|--help) usage; exit ;;
# set option to download all chapters
-a|--all) ALL='--all' ;;
# expand the path of the target directory
-d|--directory) TARGET=`readlink -f $1`; shift ;;
# add current option to titles array
*) TITLES=( "${TITLES[@]}" $option )
esac
done
# show usage and exit if no titles are set
if [[ ! $TITLES ]]; then
usage
exit
fi
download_manga