Delicious Diversion

Are you getting tired of waiting for Wil Shipley to release Delicious Library 2?

I hear that he is adding the ability to publish your library online. But why wait? Here’s a web server that you can inject into Delicious Library and start sharing your library today. It’s primitive, but you can easily customize it to your heart’s delight. It’s the same web server that I used in my screencast, and it is built using NuAnywhere, which ships with Nu and NuHTTP, a reusable web server component that I wrote to plug into Cocoa applications.

The server code is below. To use it, just follow the steps that I showed in the screencast: first use NuAnywhere to inject a console into Delicious Library, then use the console to load the file below. Of course you’ll need to have Nu (which includes NuAnywhere) and NuHTTP. If you don’t yet have Nu, you can get a binary installer here. You’ll need to get NuHTTP with git (but you really should be familiar with git by now), then build and install it with “nuke install”.

If you try this, let me know how it goes!

If you don’t have Delicious Library, you can download a trial version from Wil’s company site: Delicious Monster. All of this will work with the trial version, but your library will be limited to 25 or fewer items.

Here’s the code; in my screencast, I had it in a file named delicious-server.nu:
(load "NuHTTP:server")
(set document ((NSDocumentController sharedDocumentController) 
               currentDocument))
(set server ((NuHTTPController alloc) initWithPort:3000))
(server setHandlers:(set handlers (array)))

;; the main index page
(get "/" 
     (set TITLE "My Library")
     (set template <<-TEMPLATE
<div style="float:left; margin-right:10px">
<img src="/icon" height="60px" />
</div>
<h1>My Delicious Library</h1>
<br clear="all"/>
<ul>
<% (((document library) items) each: (do (item) %>
<li><a href="/item?id=<%= (item identifierString) %>">
<%= (item titleForSorting) %></a></li>
<% )) %>
</ul>
TEMPLATE)
     (eval (NuTemplate codeForString:template)))

;; view a specific item
(get /^\/item\?id=(.*)$/
     (set identifierString (@match groupAtIndex:1))
     (set items (((document library) items) select: 
                 (do (item) (eq (item identifierString) 
                                identifierString))))
     (set item (items objectAtIndex:0))
     (set template <<-TEMPLATE
<div style="float:left; margin-right:10px">
<img src="/icon" height="60px" />
</div>
<h1><a href="/">My Delicious Library</a></h1>
<br clear="all"/>
<div style="float:left; margin-right:10px">
<img src="/item/cover?id=<%= identifierString %>" height="200px"/>
</div>
<table>
<tr><td>Title</td><td><%= (item titleForSorting) %></td></tr>
<tr><td>Author</td><td><%= (item valueForKey:"author") %></td></tr>
</table>
TEMPLATE)
     (eval (NuTemplate codeForString:template)))

;; get the cover image for an item
(get /^\/item.cover\?id=(.*)$/
     (set identifierString (@match groupAtIndex:1))
     (set items (((document library) items) select:
                 (do (item) (eq (item identifierString) 
                                identifierString))))
     (set item (items objectAtIndex:0))
     ((response objectForKey:"headers") 
      setObject:"image/jpg" forKey:"Content-Type")
     (set bitmapImageRep 
          (NSBitmapImageRep imageRepWithData:(item plainCoverImageData)))
     (bitmapImageRep 
          representationUsingType:NSJPEGFileType 
          properties:NULL))

;; get a png of the application icon
(get "/icon" 
     ((response objectForKey:"headers") 
      setObject:"image/png" forKey:"Content-Type")
     (set bitmapImageRep 
          (NSBitmapImageRep imageRepWithData:
               (((NSApplication sharedApplication) applicationIconImage)
                TIFFRepresentation)))
     (bitmapImageRep 
          representationUsingType:NSPNGFileType 
          properties:NULL))

Comment on this post ↓

Leave a Comment (sign in with Twitter)