/[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.7 - (show annotations)
Sun Feb 15 11:40:43 2004 UTC (20 years, 2 months ago) by dpavlin
Branch: MAIN
Changes since 1.6: +8 -2 lines
File MIME type: application/x-sh
fping proxy and check if book is on bookshelf

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

  ViewVC Help
Powered by ViewVC 1.1.26