This script will make a dump of the Django Book to your local filesystem so you can read it offline. Like say on a flight and you don’t want to pay 10 bucks for an hour of wifi. Even though you’re stoked you finally can, if you need/want to.
This script makes all the css links work so everything’s pretty. Also, Firefox by default won’t render index.html files as directory indexes when working in the ‘file://’ scheme, so this script rewrites internal links so that the index.html is explicit.
#!/bin/sh
# this script downloads a copy of the 2.0 django book, complete with css and
# sets it up so you can read it in-flight
WEB_RT="http://www.djangobook.com/en/2.0"
MIN_CHP=1
MAX_CHP=20
CSS1=http://new-media.djangobook.com/yui/container/assets/container.css
CSS2=http://new-media.djangobook.com/yui-ext/css/resizable.css
CSS3=http://new-media.djangobook.com/yui-ext/css/tabs.css
CSS4=http://new-media.djangobook.com/djangobook.css
CSS5=http://new-media.djangobook.com/yui/grids/grids-min.css
CSS6=http://new-media.djangobook.com/yui/reset/reset-min.css
CSS1_FILE=container.css
CSS2_FILE=resizable.css
CSS3_FILE=tabs.css
CSS4_FILE=djangobook.css
CSS5_FILE=grids-min.css
CSS6_FILE=reset-min.css
DIR_RT=django_book
echo "************ Setting up our dir structure *****************"
mkdir $DIR_RT
mkdir $DIR_RT/css
for i in `seq -f 'chapter%02.0f' $MIN_CHP $MAX_CHP`; do
mkdir $DIR_RT/$i
done
echo "*********** Downloading all the content *****************"
wget $WEB_RT/ -P $DIR_RT
for i in $CSS1 $CSS2 $CSS3 $CSS4 $CSS5 $CSS6; do
wget $i -P $DIR_RT/css/
done
for i in `seq -f 'chapter%02.0f' $MIN_CHP $MAX_CHP`; do
wget $WEB_RT/$i/ -P $DIR_RT/$i/
done
echo "************** Fixing up the css links ****************"
for i in `seq 1 6`; do
eval "CSS_F=\"\$CSS${i}_FILE\""
sed -i "s/href=\".*$CSS_F\"/href=\"css\/$CSS_F\"/" $DIR_RT/index.html
done
for i in `seq -f 'chapter%02.0f' $MIN_CHP $MAX_CHP`; do
for j in `seq 1 6`; do
eval "CSS_F=\"\$CSS${j}_FILE\""
sed -i "s/href=\".*$CSS_F\"/href=\"..\/css\/$CSS_F\"/" $DIR_RT/$i/index.html
done
done
echo "*************** Fixing up the page links **************"
sed -i "s/href='chapter[0-9]\{2\}\//&index.html/" $DIR_RT/index.html
for i in `seq -f 'chapter%02.0f' $MIN_CHP $MAX_CHP`; do
sed -i "s/href=\"..\"/href=\"..\/index.html\"/" $DIR_RT/$i/index.html
sed -i "s/href='..\/chapter[0-9]\{2\}\//&index.html/" $DIR_RT/$i/index.html
done
Just came across this as a result of a search, looking for a way to download the book so I could look at it when offline.
When I try it, the last step bombs out with a “Syntax error: Unterminated quoted string”, but apart from the index page not taking me striaght to the chapter (yeah, I’m using Firefox), it works fine.
So, thanks :-)
Roberto/.
@Roberto – cool. It’s probably that the shell I was using when I wrote the script has slightly different quoting rules than the shell you’re using. If you write up a fix for that last line that works in your shell, post it here… and also the shell you’re using (dash? bash?) that’s likely also the reason the index page links are broken for you.
cheers
I also found this via a google search and I must say it works splendid! Train rides will be so much more fun/educating :D
For OSX (Leopard) users: SEQ isn’t included in os x, so you need to get “gseq” from the “coreutils” package at macports.
(sudo port install coreutils)
After this, you can either change all seq’s to gseq, or make a symbolic link like this: sudo ln -s /opt/local/bin/gseq /opt/local/bin/seq
The version of sed installed on OSX (at least the version for Snow Leopard), does not support the syntax above (I was receiving error messages for ‘extra characters at the end of d command’). To get around this OSX users should use gsed instead of sed.
To install gsed via macports use the following command: ’sudo port install gsed’
Thanks for this – worked perfectly!
For those that don’t know, copy/paste the above script into a file called (for example) dumpbook.sh
Place dumpbook.sh in a convenient directory – this directory will become the parent directory of the book, as the script creates a directory called ‘django_book’ in the same directory as the script
Next, chmod +x dumpbook.sh
Then finally, ./dumpbook.sh
The script will run and download the book into ./django_book/
Navigate to ./django_book/index.html with your browser (using the local file system) and enjoy!
- Dave