1 |
#!/bin/sh |
2 |
|
3 |
# proxy settings (same as in firebird) |
4 |
( ping proxy 2>&1 ) >/dev/null && export http_proxy=http://proxy:8080 |
5 |
# user agent (same as in firebird) |
6 |
ua="Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.5) Gecko/20031225 Firebird/0.7" |
7 |
|
8 |
# wait between pages |
9 |
export wait=120 |
10 |
|
11 |
|
12 |
if [ -z "$1" ] ; then |
13 |
echo "Usage: $0 ISBN" |
14 |
exit 1 |
15 |
fi |
16 |
|
17 |
if [ -e orig ] ; then |
18 |
echo "orig directory found. Resume download? [Y/n]" |
19 |
read ans |
20 |
if [ "$ans" = "n" ] ; then |
21 |
exit 1; |
22 |
fi |
23 |
mv orig/* . |
24 |
rm -Rf orig |
25 |
|
26 |
grep -l 'promo.asp' * | xargs -i rm -v {} |
27 |
fi |
28 |
|
29 |
isbn=$1; |
30 |
|
31 |
isbn2=`echo $isbn | sed 's/-//g'` |
32 |
|
33 |
function mirror() { |
34 |
|
35 |
url="$1" |
36 |
|
37 |
file=`echo $url | sed -e s,http://[^?]*?,index.html?, -e s,#.*$,, -e s,/,%2F,g` |
38 |
if [ -e "$file" ] ; then |
39 |
# echo "skip $url" |
40 |
return |
41 |
fi |
42 |
|
43 |
if echo $url | grep '/index' >/dev/null ; then |
44 |
cookies="" |
45 |
echo -n "no login (index) " |
46 |
elif echo $url | grep 'mode=toc' >/dev/null ; then |
47 |
cookies="" |
48 |
echo -n "no login (toc) " |
49 |
else |
50 |
cookies="--load-cookies=/home/dpavlin/.phoenix/default/g6b45nt6.slt/cookies.txt" |
51 |
echo -n "with login " |
52 |
fi |
53 |
echo $url |
54 |
|
55 |
wget -p -nH -nc -k \ |
56 |
-t 1 -U "$ua" \ |
57 |
$cookies $url |
58 |
|
59 |
# -D safari.oreilly.com \ |
60 |
# -A 0-201-41975-0 \ |
61 |
|
62 |
perl -e '$t=rand($ENV{wait} || 30);print "sleep for $t sec.\n"; sleep($t);' |
63 |
} |
64 |
|
65 |
function geturl() { |
66 |
hindent -s $1 | grep -i href | grep mode=[st][eo]c | \ |
67 |
sed -e 's/^.*<a.*href="//i' \ |
68 |
-e 's/".*//' -e 's/amp;//g' \ |
69 |
-e 's,^[^\?]*\?,http://safari.oreilly.com/,' \ |
70 |
-e 's/#$//' \ |
71 |
-e 's/\&srchText=//' \ |
72 |
-e 's/open=false/open=true/' | \ |
73 |
grep '&s=1&b=1&f=1&t=1&c=1&u=1&r=&o=1' | \ |
74 |
grep $2 | \ |
75 |
grep -v "$2/[0-9][0-9][0-9][0-9][0-9][0-9][0-9]" | \ |
76 |
sort -u >> in |
77 |
} |
78 |
|
79 |
function uniqurl() { |
80 |
mv in in.tmp |
81 |
grep -v 'view=[A-Z]' in.tmp | sort -u > in |
82 |
grep 'view=[A-Z].*/index' in.tmp | sort -u >> in |
83 |
} |
84 |
|
85 |
function mirror_in() { |
86 |
cat in | while read url ; do |
87 |
mirror "$url" |
88 |
#sleep $wait |
89 |
|
90 |
if grep 'promo.asp' `ls -t index.html* | head -3` >/dev/null ; then |
91 |
echo "WARNING: safari seems to logout you as user. Aborting." |
92 |
exit 1 |
93 |
fi |
94 |
|
95 |
if grep -i '>Account locked<' `ls -t index.html* | head -3` >/dev/null ; then |
96 |
echo "WARNING: your safari account is locked. Aborting." |
97 |
exit 1 |
98 |
fi |
99 |
|
100 |
if grep -i 'session disabled' `ls -t index.html* | head -3` >/dev/null ; then |
101 |
echo "WARNING: your safari session is disabled. Aborting." |
102 |
exit 1 |
103 |
fi |
104 |
done |
105 |
} |
106 |
|
107 |
echo -n > in |
108 |
mirror "http://safari.oreilly.com/?XmlId=$isbn" |
109 |
|
110 |
echo "extract URLs from first page..." |
111 |
geturl "index.html?XmlId=$isbn" $isbn |
112 |
uniqurl |
113 |
|
114 |
mirror_in |
115 |
|
116 |
echo -n "extracting URLs [1]" |
117 |
ls index.html* | while read file ; do |
118 |
echo -n "." |
119 |
geturl $file $isbn |
120 |
done |
121 |
echo |
122 |
|
123 |
uniqurl |
124 |
|
125 |
mirror_in |
126 |
|
127 |
echo -n > in |
128 |
echo -n "extracting URLs [2]" |
129 |
ls index.html* | while read file ; do |
130 |
echo -n "." |
131 |
geturl $file $isbn |
132 |
done |
133 |
|
134 |
uniqurl |
135 |
|
136 |
mirror_in |
137 |
|
138 |
# convert links in html |
139 |
bn=`basename $0` |
140 |
dir=`echo $0 | sed "s/$bn$//"` |
141 |
ls index.html* | xargs -i $dir/filter.pl {} |
142 |
mkdir orig |
143 |
mv index.html* orig/ |
144 |
|