/[safari]/get_book.sh
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /get_book.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (show annotations)
Wed Jul 21 13:58:35 2004 UTC (19 years, 8 months ago) by dpavlin
Branch: MAIN
CVS Tags: HEAD
Changes since 1.11: +7 -5 lines
File MIME type: application/x-sh
major fix

1 #!/bin/sh
2
3 # proxy settings (same as in firebird)
4 fping -q proxy && export http_proxy=http://proxy:8080
5 if [ -z "$http_proxy" ] ; then
6 fping -q proxy.lan && export http_proxy=http://proxy.lan:8080
7 fi
8 # user agent (same as in firebird)
9 ua="Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040506 Firefox/0.8"
10 cookie_file="cookie.txt"
11
12 # wait between pages
13 export wait=120
14
15 if [ ! -z "$http_proxy" ] ; then
16 echo "Using proxy $http_proxy"
17 fi
18
19 if [ -z "$1" ] ; then
20 echo "Usage: $0 ISBN"
21 exit 1
22 fi
23
24 if [ -e orig ] ; then
25 echo "orig directory found. Resume download? [Y/n]"
26 read ans
27 if [ "$ans" = "n" ] ; then
28 exit 1;
29 fi
30 mv orig/* .
31 rm -Rf orig
32
33 grep -l 'promo.asp' * | xargs -i rm {}
34 grep -l 'This is only a preview of the full book' * | xargs -i rm {}
35 fi
36
37 isbn=`echo $1 | sed 's/-//g' | tr '[a-z]' '[A-Z]'`
38
39 function mirror() {
40
41 url="$1"
42
43 file=`echo $url | sed -e s,http://[^?]*?,index.html?, -e s,#.*$,, -e s,/,%2F,g`
44 if [ -e "$file" ] ; then
45 # echo "skip $url"
46 echo -n "."
47 return
48 fi
49
50 cookie=""
51 if echo $url | grep '/index' >/dev/null ; then
52 echo -n "no login (index) "
53 elif echo $url | grep 'mode=toc' >/dev/null ; then
54 echo -n "no login (toc) "
55 else
56 if [ ! -e $cookie_file ] ; then
57 echo "cookies file $cookie_file doesn't exits! Please create it."
58 echo "It should be in format:"
59 echo "Cookie: Site=UICode=&Portal=oreilly&GUID=..."
60 exit 1
61 fi
62 read cookie < $cookie_file
63 if [ -z "$cookie" ] ; then
64 echo "Empty cookie file $cookie_file !"
65 exit 1
66 fi
67 fi
68
69 if [ -z "$cookie" ] ; then
70 echo "$url [no cookie]"
71 else
72 echo "$url [with cookie]"
73 fi
74
75 wget -q -p -nH -nc -k -t 1 -U "$ua" --cookies=off --header="$cookie" $url
76 perl -e '$t=rand($ENV{wait} || 120);print "sleep for $t sec.\n"; sleep($t);'
77
78 }
79
80 function geturl() {
81 hindent -s $1 | grep -i href | grep mode=[st][eo]c | \
82 sed -e 's/^.*<a.*href="//i' \
83 -e 's/".*//' -e 's/amp;//g' \
84 -e 's,^[^\?]*\?,http://safari.oreilly.com/,' \
85 -e 's/#.*$//' \
86 -e 's/\&srchText=//' \
87 -e 's/open=false/open=true/' | \
88 grep '&s=1&b=1&f=1&t=1&c=1&u=1&r=&o=1&n=1&d=1&p=1&a=0' | \
89 grep -i "xmlid=[0-9A-Za-z\-][0-9A-Za-z\-]*" | \
90 grep -vi "xmlid=[0-9A-Za-z\-]*/[0-9][0-9][0-9][0-9][0-9][0-9][0-9]" | \
91 sort -u >> in
92 }
93
94 function uniqurl() {
95 mv in in.tmp
96 grep -v 'view=[A-Z]' in.tmp | grep 'a=0$' | sort -u > in
97 grep 'view=[A-Z].*/index' in.tmp | grep 'a=0$' | sort -u >> in
98 links=`wc -l in | cut -d" " -f1`
99 echo "found $links unique links"
100 }
101
102 function mirror_in() {
103 cat in | while read url ; do
104 mirror "$url"
105 #sleep $wait
106
107 if grep 'promo.asp' `ls -t index.html* | head -3` >/dev/null ; then
108 echo "WARNING: safari seems to logout you as user. Aborting."
109 exit 1
110 fi
111
112 if grep -i '>Account locked<' `ls -t index.html* | head -3` >/dev/null ; then
113 echo "WARNING: your safari account is locked. Aborting."
114 exit 1
115 fi
116
117 if grep -i 'session disabled' `ls -t index.html* | head -3` >/dev/null ; then
118 echo "WARNING: your safari session is disabled. Aborting."
119 exit 1
120 fi
121
122 if grep -i 'This is only a preview of the full book' `ls -t index.html* | head -3` >/dev/null ; then
123 echo "WARNING: you didn't add this book to your bookshelf!"
124 exit 1
125 fi
126 done
127 echo
128 }
129
130 echo -n > in
131 mirror "http://safari.oreilly.com/$isbn"
132 echo
133
134 file=`ls *index.html?XmlId=*`
135 isbn=`echo $file | cut -d= -f2`
136 echo "extract URLs from first page $file... [$isbn]"
137 geturl $file $isbn
138 uniqurl
139
140 mirror_in
141
142 echo -n "extracting URLs [1]"
143 ls index.html* | while read file ; do
144 echo -n "."
145 geturl $file $isbn
146 done
147 echo
148
149 uniqurl
150
151 mirror_in
152
153 echo -n > in
154 echo -n "extracting URLs [2]"
155 ls index.html* | while read file ; do
156 echo -n "."
157 geturl $file $isbn
158 done
159
160 uniqurl
161
162 mirror_in
163
164 # convert links in html
165 bn=`basename $0`
166 dir=`echo $0 | sed "s/$bn$//"`
167 ls index.html* | xargs -i $dir/filter.pl {}
168 mkdir orig
169 mv index.html* orig/
170

  ViewVC Help
Powered by ViewVC 1.1.26