-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhtoff.sh
executable file
·70 lines (62 loc) · 1.37 KB
/
htoff.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
#!/bin/sh
download()
{
html=$1
urls=$2
subdir=${html%.html}_files
for url in $urls ; do
echo "Downloading $url... \c"
mkdir -p "$subdir"
filename=${url##*/}
if wget -q -c -O "$subdir/$filename" "$url" ; then
if [ `uname` = "Darwin" ] ; then
sed -i '' "s|$url|$subdir/$filename|g" $html
else
sed -i "s|$url|$subdir/$filename|g" $html
fi
echo "Done."
else
echo "Failed."
fi
done
}
download_images()
{
html=$1
imgs=`grep -io '<img[^>]*' $html | sed 's/<[Ii][Mm][Gg].*[Ss][Rr][Cc]\s*=\s*"*//;s/[" ].*//'`
download "$html" "$imgs"
}
download_archives()
{
html=$1
archives=`grep -ioE 'href\s*=[^>]*(\.zip|\.rar|\.7z|\.tar\.gz|\.tar\.bz2)[^>]*' $html | sed 's/[Hh][Rr][Ee][Ff]\s*=\s*"*//;s/[" ].*//'`
download "$html" "$archives"
}
if [ $# -eq 0 ] ; then
echo "Create offline html files."
echo "Usage: htoff [-i] [-a] htmlfiles"
echo " -i Download images"
echo " -a Download archives"
exit 0
fi
for arg in "$@" ; do
if [ $arg = "-i" ] ; then
need_images="yes"
elif [ $arg = "-a" ] ; then
need_archives="yes"
fi
done
for html in "$@" ; do
if [ $html = "-i" ] || [ $html = "-a" ] ; then
continue
fi
echo "Processing $html ..."
cp "$html" "$html.bak"
if [ "$need_images" = "yes" ] ; then
download_images $html
fi
if [ "$need_archives" = "yes" ] ; then
download_archives $html
fi
echo "Finished processing $html."
done