Search

Mapping blog visits with XQuery

Posterous logs visits but doesn't time or geo-code them.  I was wondering how to do this and this is what I came up with. 

I create the blog item but don't post it to Twitter.  I create a URL  to a script on the eXist server with the blog item as a parameter. I shorten the URI with bit.ly, and post this link to Twitter.

The script on the eXist server extracts data from the request and logs it to a local XML file. It then constructs the posterous URI for the item and does a redirect.

A script converts the log  file to entries kml, using the wonderful ipinfodb.com to do the IP address geo-coding. Another script converts the log to a feed for a Simile Timeline.

This approach will only log visits via the bit.ly URI rather than direct to posterous so its not ideal but the approach is generic.

The logger (but for access controls) looks like this:

  
let $log :=doc( "/db/.../Bloglog.xml")/log
 let $item := request:get-parameter("item","")
 let $logrecord := element logrecord {
            attribute dateTime {current-dateTime()},
            attribute referer {tokenize(request:get-header("X-Forwarded-For"),",")[1]},
            $item
        }


let $update :=update insert $logrecord into $log
 return
   response:redirect-to(xs:anyURI(concat("http://kitwallace.posterous.com/",$item)))

At present I'm not caching the ip address geo-coding so I will link the display scripts in a later post. Thanks for calling, your visit has been noted!

OK, here goes...