1 |
#!/bin/sh |
2 |
|
3 |
# proxy settings (same as in firebird) |
4 |
fping proxy && 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 {} |
27 |
grep -l 'This is only a preview of the full book' * | xargs -i rm {} |
28 |
fi |
29 |
|
30 |
isbn=$1; |
31 |
|
32 |
isbn2=`echo $isbn | sed 's/-//g'` |
33 |
|
34 |
function mirror() { |
35 |
|
36 |
url="$1" |
37 |
|
38 |
file=`echo $url | sed -e s,http://[^?]*?,index.html?, -e s,#.*$,, -e s,/,%2F,g` |
39 |
if [ -e "$file" ] ; then |
40 |
# echo "skip $url" |
41 |
echo -n "." |
42 |
return |
43 |
fi |
44 |
|
45 |
if echo $url | grep '/index' >/dev/null ; then |
46 |
cookies="" |
47 |
echo -n "no login (index) " |
48 |
elif echo $url | grep 'mode=toc' >/dev/null ; then |
49 |
cookies="" |
50 |
echo -n "no login (toc) " |
51 |
else |
52 |
cookies="--load-cookies=/home/dpavlin/.phoenix/default/g6b45nt6.slt/cookies.txt" |
53 |
echo -n "with login " |
54 |
fi |
55 |
echo $url |
56 |
|
57 |
wget -p -nH -nc -k \ |
58 |
-t 1 -U "$ua" \ |
59 |
$cookies $url |
60 |
|
61 |
# -D safari.oreilly.com \ |
62 |
# -A 0-201-41975-0 \ |
63 |
|
64 |
perl -e '$t=rand($ENV{wait} || 30);print "sleep for $t sec.\n"; sleep($t);' |
65 |
} |
66 |
|
67 |
function geturl() { |
68 |
hindent -s $1 | grep -i href | grep mode=[st][eo]c | \ |
69 |
sed -e 's/^.*<a.*href="//i' \ |
70 |
-e 's/".*//' -e 's/amp;//g' \ |
71 |
-e 's,^[^\?]*\?,http://safari.oreilly.com/,' \ |
72 |
-e 's/#.*$//' \ |
73 |
-e 's/\&srchText=//' \ |
74 |
-e 's/open=false/open=true/' | \ |
75 |
grep '&s=1&b=1&f=1&t=1&c=1&u=1&r=&o=1' | \ |
76 |
grep $2 | \ |
77 |
grep -v "$2/[0-9][0-9][0-9][0-9][0-9][0-9][0-9]" | \ |
78 |
sort -u >> in |
79 |
} |
80 |
|
81 |
function uniqurl() { |
82 |
mv in in.tmp |
83 |
grep -v 'view=[A-Z]' in.tmp | sort -u > in |
84 |
grep 'view=[A-Z].*/index' in.tmp | sort -u >> in |
85 |
links=`wc -l in | cut -d" " -f1` |
86 |
echo "found $links unique links" |
87 |
} |
88 |
|
89 |
function mirror_in() { |
90 |
cat in | while read url ; do |
91 |
mirror "$url" |
92 |
#sleep $wait |
93 |
|
94 |
if grep 'promo.asp' `ls -t index.html* | head -3` >/dev/null ; then |
95 |
echo "WARNING: safari seems to logout you as user. Aborting." |
96 |
exit 1 |
97 |
fi |
98 |
|
99 |
if grep -i '>Account locked<' `ls -t index.html* | head -3` >/dev/null ; then |
100 |
echo "WARNING: your safari account is locked. Aborting." |
101 |
exit 1 |
102 |
fi |
103 |
|
104 |
if grep -i 'session disabled' `ls -t index.html* | head -3` >/dev/null ; then |
105 |
echo "WARNING: your safari session is disabled. Aborting." |
106 |
exit 1 |
107 |
fi |
108 |
|
109 |
if grep -i 'This is only a preview of the full book' `ls -t index.html* | head -3` >/dev/null ; then |
110 |
echo "WARNING: you didn't add this book to your bookshelf!" |
111 |
exit 1 |
112 |
fi |
113 |
done |
114 |
echo |
115 |
} |
116 |
|
117 |
echo -n > in |
118 |
mirror "http://safari.oreilly.com/?XmlId=$isbn" |
119 |
echo |
120 |
|
121 |
echo "extract URLs from first page..." |
122 |
geturl "index.html?XmlId=$isbn" $isbn |
123 |
uniqurl |
124 |
|
125 |
mirror_in |
126 |
|
127 |
echo -n "extracting URLs [1]" |
128 |
ls index.html* | while read file ; do |
129 |
echo -n "." |
130 |
geturl $file $isbn |
131 |
done |
132 |
echo |
133 |
|
134 |
uniqurl |
135 |
|
136 |
mirror_in |
137 |
|
138 |
echo -n > in |
139 |
echo -n "extracting URLs [2]" |
140 |
ls index.html* | while read file ; do |
141 |
echo -n "." |
142 |
geturl $file $isbn |
143 |
done |
144 |
|
145 |
uniqurl |
146 |
|
147 |
mirror_in |
148 |
|
149 |
# convert links in html |
150 |
bn=`basename $0` |
151 |
dir=`echo $0 | sed "s/$bn$//"` |
152 |
ls index.html* | xargs -i $dir/filter.pl {} |
153 |
mkdir orig |
154 |
mv index.html* orig/ |
155 |
|