Monday, 21 December 2009

iPhone Stanza Recovery

With a recent update of the iphone OS, a jailbreak here and there saw my Stanza library lost. Most of the books were sourced from online catalogs, some of which could be easily recovered, but this was a pain. So I set out with the aim of recovering and backing up my library for future use. Also some of the solutions I found do not name the books properly, so when importing to Calibre it messes up. I have more or less got around this.

The first part was easy, Mac, iTunes and iPhone backup. Check, this will also work if you want to save your iPhone library to your PC. Anyway that's enough rambling, lets get on with the good stuff.

1. Mac - all the tools mentioned are available for Windows, I love command line, so you are on your own (with Cygwin) if you choose that route.
2. iTunes and a backup / iPhone Jailbroken. For Mac you can use "iPhone Backup Extractor" Its free, easy and does the job. I'll let you work it out, likewise use Diskaid to get to your Stanza library, again I'll leave the discovery up to you. If there is interest I will help out.
3. Calibre This is where we will import the books to. This is not a necessity (just makes it easier)
4. There are plenty of tutorials as well using: CalibredbExtract This again is a must if you plan on hosting / Dropboxing Catalogs.

Once you have your extracted Stanza library, you should see something like this:


drwxr-xr-x 393 user user 13362 20 Dec 16:38 0
drwxr-xr-x 393 user user 13362 20 Dec 16:40 1
drwxr-xr-x 392 user user 13328 20 Dec 16:41 2
drwxr-xr-x 389 user user 13226 20 Dec 16:43 3
drwxr-xr-x 394 user user 13396 20 Dec 16:44 4
drwxr-xr-x 393 user user 13362 20 Dec 16:45 5
drwxr-xr-x 390 user user 13260 20 Dec 16:46 6
drwxr-xr-x 397 user user 13498 20 Dec 16:48 7
drwxr-xr-x 385 user user 13090 20 Dec 16:49 8
drwxr-xr-x 387 user user 13158 20 Dec 16:50 9


These are where the books are stored. The epub, is inside each of these folders unpacked as .explode folder. What we need to do is pack it back up, the easy part thanks to: This Blog. The zip part I am grateful for, as well as figuring it out, saved me time. That blog tells you how to do it for each book, with a for loop, some creative shell scripting we can convert all the books back to epub. 1st part done, the next is to rename the and give it a more meaningful .epub name. A bit of searching the xml, sed and awk and we get there.

The script, I copied into each folder and ran from there, and then issued a mv *.epub ../books to put them into one folder. Again if people want me to alter / provide a more rounded solution e-mail me.

for file in $( ls -1 | grep explode )
do
    filer=$( echo ${file} | sed 's/explode//g' )
    zip -Xr9D ${filer}epub mimetype ./${file}/*

    if [ -f ./${file}/OEBPS/content.opf ]
    then 
        TITLE=$(cat ./${file}/OEBPS/content.opf | egrep "dc:title" | sed 's/<\/.*>//g' | sed 's/.*<.*>//g' )
        CREATOR=$(cat ./${file}/OEBPS/content.opf | egrep "dc:creator" | sed 's/<\/.*>//g' | sed 's/.*<.*>//g' | awk -F, ' { print $2 " " $1 } ')
        mv ${filer}epub "${TITLE} - ${CREATOR}.epub"
    fi
done

Thats it, simple, easy, to the point and gets the job done. With a little searching, creative license you can turn the script into a recover, build and publish and make your own repository! That's for another day though.