Mobilizing Your Delicious Library

Update: Added some screenshots of an updated version running on my - see below.

One of the things that drew me to the strange niche of personal cataloguing software was the fact that I wanted some way to check if I already owned a book. I own so many of them that I have been known to occasionally buy one I had entirely forgotten about during one of my legendary shopping sprees, and although they make nice gifts, it's always annoying.

So I'm now starting to code a WAP front-end to my Delicious Library database, and 's DOM-XML functions make it childishly trivial. Here's a code snippet that demonstrates the basic technique (and glosses over the fact that the XML file can be pretty damn big if you have well over 300 entries, something I will be addressing later) to display a page crammed with cover images:

define( "XML_DATA", "Library Media Data.xml" );

if ( !
$dom = domxml_open_file( XML_DATA ) ) {
  echo
"Error while parsing the document " . XML_DATA . "\n";
  exit;
}

$root = $dom->document_element();
$nodes = $root->child_nodes();
$aImages = array( "Small Covers", "Medium Covers", "Large Covers" );
foreach(
$nodes as $node ) {
  if(
$node->tagname == "items" ) {
    
$items = $node->child_nodes();
    foreach(
$items as $item ) {
      if(
$item->tagname == "book" ) {
        
$szUUID = $item->get_attribute("uuid");
        foreach(
$aImages as $szPrefix )
          if(
file_exists( "Images/$szPrefix/$szUUID" ) ) {
            echo
"<img src=\"Images/$szPrefix/$szUUID\">";
            break;
// we only want one image per UUID
          
}
      }
    }
  }
}

Caveats

A few notes are in order:

  • I'm not using XSLT here. It's doable, but I wanted the sample to be readable and easy to understand (and modify) by folk without XSLT support on their , hence the DOM/nested loop approach.
  • I'm using a copy of my ~/Library/Application Support/Delicious Library directory, not the original. I made a copy inside the ~/Sites folder on my and am fiddling with it there - so adjust pathnames and permissions accordingly.
  • Note the way how I select the smallest image possible. This is by no means foolproof, but rather a test - so far, everything appears to have a Small Cover, but I'm not accounting for entries that don't; this is just a sample to show how it can be done.
  • The code, as written, spews out a lot of XML warnings due to lack of proper attribute checks. Again, it's just a concept piece. You have been warned.
  • This cries out for pre-parsing and caching any way you look at it - re-parsing the XML file on every request is suicidal, and the best approach is to walk the DOM tree, capture all the attributes you find interesting, and serialize() the results (it's faster and leaner to unserialize() the data). Additionally, images need proper Content-Type headers (I'm trusting browsers to do some byte sniffing and display the images properly just because the file is referenced inside an img tag, and that's pretty sloppy, even for me).

I'll be doing that over the next few days as time allows (after work and whatnot), but if you want to roll your own, hacking together a search form, some smart image resizing (based on the WAP User Agent profile) and minimal formatting shouldn't be too hard if you look at my webpipe code.

Enjoy.

A few days later...

...I finally picked this up again, and after only half an hour's worth of coding, I have something fit for my own use:

The ugly Submit buttons are one of the few quirks of the 's browser (they don't appear on the or on the other devices I have with me at the moment), but they're perfectly tolerable (in fact, the beauty of the 's browser is that it does the right thing with anything I throw at it, unlike, say, the ).

As to the application itself, it's a very thin wrapper around a slightly tweaked version of the class I published above (with a query result limit and a couple of extra attributes), and once I've used it a bit more, fixed a couple of layout bugs and figured out what I need from Delicious Library on the move besides checking whether or not I already own a book/DVD/whatever, I'll package it up and publish it somewhere.

But, again, it's child's play. Feel free to roll your own or drop me any suggestions you might have.

This page is referenced in: