Friday, March 23, 2007

retagged!

I finally finished (mostly) converting all my 150 del.icio.us tags to new blogger categories. I say "mostly" because I ran into a few issues...

The actual content of a Blogger post is stored as character data (rather than as actual tags or xml) in the xml feed-- and the troubles I had with some of my posts enlightened me as to why this is: there's no way to guarantee that user input is going to be valid xml. If bloggers write their posts using the "compose" interface I guess the odds are better; I personally tend to avoid that because I don't like the styles and tags that Blogger uses by default, and as far as I know there's no way to customize this. But that opens up the door for human error. Another source of errors-- we were using a GreaseMonkey script to auto-generate our delicious tags, and for a while it looks one of those scripts was adding a stray </span> tag for no apparent reason (bad!). The other recurring problem I ran into was special characters that had been input as html named-entities rather than unicode/numeric ones. My fault, again.

There were about three posts that I had a weird error with-- I got a 400 response code back, which is a "bad request." The Zend code didn't seem to think there was anything wrong with my data, so I'm not sure exactly what the problem is. I'll have to look at those three posts more closely and see if I can figure out why this is happening.

I have some more ideas for scripts, and would like to post simple versions of an easy php back-up and reload scripts (these essential for anyone who wants to play with the Blogger API to do anything with their posts, just in case something goes wrong! I was SO glad I had made backups), because I think these could be done pretty concisely, could be useful, and might give other people ideas of their own.

Thanks to api.rboyd for the pointers on doing entry updates within the Zend framework. I was able to find some helpful documentation on the Zend_Feed_EntryAtom object once I knew exactly what I was looking for (although I'm not sure how reliable the url is...). It seems odd to me that the official Zend Framework site only includes examples, and not the full object documentation (not that I can find, anyway). Examples can only take you so far-- and it doesn't seem to take me nearly as far as I want to go.

Monday, March 19, 2007

Accessing blogger posts with PHP

The fact that Blogger uses the Google Data API opens up all kinds of possiblities. I was browsing the Blogger Data API documentation and noticed that there Zend has created a PHP client for Google Data. I'm pretty comfortable with PHP, and PHP 5 has a good DOM interface, so I thought I'd take a look.

Zend's framework code isn't so far along (they're still not up to their 1.0 release), but it was enough to get me started. I was able to create a script that went through all my Blogger posts in month-by-month chunks (my other blog has over 200 posts, spread out over 2 years) and saved a local copy of the XML feed for each post. I wanted to have a back up of them before I attempted any scripted changes, and it turns out that was a very good thing.

Next, I modified my script so it could update the posts. My goal: converting my del.icio.us tags to the new Blogger style labels. There are a lot of posts on my blog, and a lot of tags (it's a book-blog, and since we're tagging authors there are a lot of unique or infrequently used tags), so it would have been major work to retag all of the posts by hand. Here I discovered one of the limitations in Zend's implementation-- there's no update function for their Blogger/GData classes. However, it was easy enough to add a quick method that would do a PUT instead of a POST. The other snag I ran into: I was using the DOM to pull the del.icio.us tags out of my posts (which worked okay) and then remove the obsolete div of del.icio.us tags from the post entirely. What I discovered is that not all of my posts are well-formed XML (I think the most common culprits seemed to be unclosed <br> tags), and this resulted in some problems. I won't go into the details, but let's just say I was very glad I'd made a local backup of all my posts before I attempted anything.

I hope to clean up my script to make it work a little better (so that I can finish re-tagging my posts without destroying any of them!). If there's any interest, I might be willing to post the relevant code or upload a script somewhere so that others could benefit from it.

Sunday, March 18, 2007

randomized quotes

So, there was an old Blogger hack documented in the Blogger help about using Javascript to have a randomly changing description or quote for your blog. When I started upgrading my other blog and playing with the new Blogger feeds, it occurred to me I could improve on it.

This will take a little bit of setup to get going, but once it's done it will be much easier to add new quotes. Or, in my case, if you have a shared blog, the less technical people can still easily contribute quotes without having to know Javascript and understand where in their template the code needs to go.

The technical stuff, for anyone who's interested: it's an AJAX call to the Blogger single-post full entry feed, which uses the Javscript DOM to get a count of list items and display one at random.

How to get it working

Enter your information into the form below, and then add the widget to your blog. There's one thing you need to do before you're ready to add the widget. Create a new post for your quotes and put at least one quote in it now (you can add more quotes at any time). Make sure to format it as a bulleted list-- the script will look for list elements and ignore everything else, so feel free to add other text or whatever you like. Now (this is as tricky as it gets), figure out the post id # of your new post and enter it in the form below. When you are editing the post in Blogger, the end of the url should look like postID=######.

Blog: .blogspot.com
Quote post id #:
Title:
Note: If you don't want a title to display, you'll need to either leave it empty here or (better, if you're up to it, because then when you manage your template you don't have a nameless, unlabeled widget), after you add it use the "edit HMTL" option for your blogger layout, find the new widget (by title is easiest) in your template, and remove the code that displays the title.

A couple of caveats...

  • Don't expect your quotes to show up in the Blogger preview, since the preview page isn't hosted under your blogspot domain name and most browser don't allow AJAX calls across domains because it's insecure.
  • Your post content must be valid xml-- if you have the setting turned on to convert line breaks to <br>s, it may cause problems.
  • This widget is only designed to work for blogs hosted on blogspot.
  • This code has been tested in Firefox 1.5 and IE6; no guarantees it will work in other browsers.

Updated 2007-03-24: content length was limited by Javascript string size (which varies from browser to browser), and truncating the post content makes for invalid xml. Added a fix (storing the content in a hidden div) to get past this size limitation.

Friday, March 16, 2007

Blogger junkie

Ever since I switched my main blog over to the new no-longer-Beta blogger, I've been obsessed with it! I'm behaving like a true code junkie-- forgetting to eat, staying up way too late (much later than when I normally stay up too late), losing all track of time... I've had a blog for somewhat more than a year and a half now (with posts retroactive so it's over two years of content now), and I was using just about every Blogger hack I knew about, along with some of my own hacks. I've been hesitant to switch over to the new Blogger, because I knew all of my hacks would break-- but finally bit the bullet because I was working on a PHP script to pull out my Blogger posts using the Google Data API, and switching my blog over made it much easier. But I've been having way too much fun rebuilding my blog, replacing and updating my hacks as I can, and thinking of lots of crazy fun new things that are made possible by the new Blogger. Anyway, I thought maybe I would document some of my ideas and hacks, successes and failures, and whatever else over here on this little blog I created months ago as a test to try out the then-Beta Blogger. Maybe I'll come up with something cool that will interest others. Maybe I'll build a fun widget to share. Who knows what I might not come up with!