1 |
#!/bin/sh |
#!/bin/sh |
2 |
|
|
3 |
#export http_proxy=http://proxy:8080 |
# 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 |
if [ -z "$1" ] ; then |
13 |
echo "Usage: $0 ISBN" |
echo "Usage: $0 ISBN" |
14 |
exit 1 |
exit 1 |
15 |
fi |
fi |
16 |
|
|
17 |
isbn=$1; |
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 |
wait=10 |
grep -l 'promo.asp' * | xargs -i rm -v {} |
27 |
|
fi |
28 |
|
|
29 |
|
isbn=$1; |
30 |
|
|
31 |
isbn2=`echo $isbn | sed 's/-//g'` |
isbn2=`echo $isbn | sed 's/-//g'` |
32 |
|
|
33 |
function mirror() { |
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 \ |
wget -p -nH -nc -k \ |
56 |
--random-wait --wait=$wait -t 0 \ |
-t 1 -U "$ua" \ |
57 |
--load-cookies=/home/dpavlin/.phoenix/default/g6b45nt6.slt/cookies.txt \ |
$cookies $url |
|
-U "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.5) Gecko/20031206 Firebird/0.7" \ |
|
|
$1 |
|
58 |
|
|
59 |
# -D safari.oreilly.com \ |
# -D safari.oreilly.com \ |
60 |
# -A 0-201-41975-0 \ |
# -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() { |
function geturl() { |
72 |
-e 's/open=false/open=true/' | \ |
-e 's/open=false/open=true/' | \ |
73 |
grep '&s=1&b=1&f=1&t=1&c=1&u=1&r=&o=1' | \ |
grep '&s=1&b=1&f=1&t=1&c=1&u=1&r=&o=1' | \ |
74 |
grep $2 | \ |
grep $2 | \ |
75 |
|
grep -v "$2/[0-9][0-9][0-9][0-9][0-9][0-9][0-9]" | \ |
76 |
sort -u >> in |
sort -u >> in |
77 |
} |
} |
78 |
|
|
82 |
grep 'view=[A-Z].*/index' in.tmp | sort -u >> in |
grep 'view=[A-Z].*/index' in.tmp | sort -u >> in |
83 |
} |
} |
84 |
|
|
85 |
function checklogin() { |
function mirror_in() { |
86 |
if grep 'promo.asp' index.html* >/dev/null ; then |
cat in | while read url ; do |
87 |
echo "WARNING: safari seems to logunt you as user. Aborting." |
mirror "$url" |
88 |
exit 1 |
#sleep $wait |
89 |
fi |
|
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 > in |
echo -n > in |
108 |
mirror "http://safari.oreilly.com/?XmlId=$isbn" |
mirror "http://safari.oreilly.com/?XmlId=$isbn" |
109 |
|
|
110 |
echo "extract URLs from first page..." |
echo "extract URLs from first page..." |
111 |
geturl "index.html?XmlId=$isbn" $isbn |
geturl "index.html?XmlId=$isbn" $isbn |
112 |
uniqurl |
uniqurl |
113 |
|
|
114 |
mirror "-i in" |
mirror_in |
|
checklogin |
|
115 |
|
|
116 |
echo -n "extracting URLs [1]" |
echo -n "extracting URLs [1]" |
117 |
ls index.html* | while read file ; do |
ls index.html* | while read file ; do |
122 |
|
|
123 |
uniqurl |
uniqurl |
124 |
|
|
125 |
mirror "-i in" |
mirror_in |
|
checklogin |
|
126 |
|
|
127 |
echo > in |
echo -n > in |
128 |
echo -n "extracting URLs [2]" |
echo -n "extracting URLs [2]" |
129 |
ls index.html* | while read file ; do |
ls index.html* | while read file ; do |
130 |
echo -n "." |
echo -n "." |
133 |
|
|
134 |
uniqurl |
uniqurl |
135 |
|
|
136 |
mirror "-i in" |
mirror_in |
|
checklogin |
|
137 |
|
|
138 |
# convert links in html |
# convert links in html |
139 |
bn=`basename $0` |
bn=`basename $0` |