Django Book Dump
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-flightWEB_RT="http://www.djangobook.com/en/2.0"
MIN_CHP=1
MAX_CHP=20CSS1=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.cssCSS1_FILE=container.css
CSS2_FILE=resizable.css
CSS3_FILE=tabs.css
CSS4_FILE=djangobook.css
CSS5_FILE=grids-min.css
CSS6_FILE=reset-min.cssDIR_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
doneecho "*********** 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/
doneecho "************** Fixing up the css links ****************"
for i in `seq 1 6`; do
eval "CSS_FILE=\"\$CSS${i}_FILE\""
sed -i "s/href=\".*$CSS_FILE\"/href=\"css\/$CSS_FILE\"/" $DIR_RT/index.html
donefor i in `seq -f 'chapter%02.0f' $MIN_CHP $MAX_CHP`; do
for j in `seq 1 6`; do
eval "CSS_FILE=\"\$CSS${j}_FILE\""
sed -i "s/href=\".*$CSS_FILE\"/href=\"..\/css\/$CSS_FILE\"/" $DIR_RT/$i/index.html
done
doneecho "*************** 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
Roberto Nerici said,
August 10, 2009 @ 10:49 am
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/.
Michael J. said,
August 10, 2009 @ 1:36 pm
@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
Rasmus said,
September 16, 2009 @ 3:40 pm
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
Buckley said,
December 16, 2009 @ 10:38 am
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’