whatspop - Kunal Anand

Posting to Blogger using ColdFusion

3/23/2006

Ever wanted to build your own solution for posting things to your Blogger blog automatically? I am going to outline a way of doing that rather trivially using ColdFusion and the Atom API. Even if you are not posting to Blogger, you can adapt this code for any system that uses the Atom API, which is the ultimate point of building a standardized API in the first place. As a reference, I encourage you to read/skim Google's documentation for using the Atom API (which is programming language agnostic). Anyways, let's begin:

<cfset host = "www.blogger.com" />
<cfset username = "your_blogger_username" />
<cfset password = "your_blogger_password" />
<cfset password_digest = Trim(ToBase64(username & ":" & password)) />
<cfset authorization_type = "BASIC " & password_digest />

Everything above should be pretty standard. The only awkward thing is that the password digest is an encoded element (concatenated with the variable "authorization_type") that will be used when we post our data using CFHTTP. I could have saved a line and a computation, but I decided to go with readability. Besides, computer cycles are cheap.

Now we need to set the entry's time. We cannot use a simple "now()" as the API requires a formatting string. Note, if your server is in a different time-zone, you may need to offset the hours otherwise your posts will not be synchronized with your local time. I will assume that you know how to add and subtract hours/minutes. I recommend using this UDF if you cannot be bothered to do some computation.

<cfset issued_date = dateFormat(now(), "yyyy-mm-dd") & "T" & 
 timeFormat(now(), "HH:mm:ss") & "Z" />

Still with me? Now comes the fun part - crafting the post. I suggest starting off with something low-fidelity, like good old "hello, world!" Since Blogger uses the Atom API, it requires us to send/edit content using the Atom specification, which is just XML markup. The only required thing about this entire process is to abide by the markup. Note, I am specific with my indentations.

Now we can write the XML. Note, check your line break settings in Blogger. That will affect how you structure your entries (hard returns or not).

<cfsavecontent variable="blogger_post">      
  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  <entry xmlns="http://purl.org/atom/ns#">
    <title mode="escaped" type="text/plain">Hello World!</title>
    <issued><cfoutput>#issued_date#</cfoutput></issued>
    <generator url="www.example.com">Your Site Name</generator>
    <content type="application/xhtml+xml">
      <div xmlns="http://www.w3.org/1999/xhtml">Posting from ColdFusion.</div>
    </content>
  </entry>
</cfsavecontent>

The last step is to post this data back to Blogger. Some people find this part of Google's documentation a bit cryptic. Not to worry, it is actually really simple - you just need to include the right post parameters. Note, you need to change the string of 9's in the URL below to your blog ID (really different than your user ID). You can find out your blog ID through the this Blogger Help entry. A quick tangent: if you are a web developer and unfamiliar with the concepts of HTTP posts and gets, I suggest you read this article by the W3.

<cfhttp method="post" url="https://www.blogger.com/atom/99999999" 
 resolveurl="true" timeout="600">
  <cfhttpparam name="Content-type" value="application/xml" type="header" />
  <cfhttpparam name="Host" value="#Host#" type="header" />
  <cfhttpparam name="Authorization" value="#AuthType#" type="header" />
  <cfhttpparam value="#trim(blogger_post)#" type="body" />
</cfhttp>

Guess what - that is all you have to do! In case you want to see any output of the post, here are two commands that you can use:

<cfoutput>#cfhttp.FileContent#</cfoutput>
<cfdump var="#XMLParse(cfhttp.FileContent)#" />

Before you roll this script out publicly, I would recommend adding a couple catch/try blocks. If you want to give users the chance to post things to their own Blogger blogs, make sure that you test the web forms locally. The last thing you want is an error to expose a user password or a post going awry. If you are going to string this process at the end of an existing one (i.e. grabbing a user's del.icio.us links and posting them to the blog), you might want to invoke a transaction to rollback if things go unplanned during runtime.

There are lots of applications that you can use this code with. Blogger just happens to be one of them. Feel free to abstract any of this into any component, user-defined function, or your API. As always, let me know if you find an easier way to do this process or happen to build anything neat with it. I would be cool with developing a full blown component (handling posts, edits, deletions) for Blogger if there is enough collaboration/interest.

Visualizing my del.icio.us tags

3/20/2006

I wrote a Python script to help me visualize my tag intersections. As you can see, computing the weights are a bit kinky, but it works.



The purpose for doing this was to help me see if I was even using multiple tags for my links. As it turns out, I have about 70 intersecting nodes (tags), 20 that are isolated (which are not included in the above visualization). What is neat is that I have approximately 200 edges (intersections). The point? As predicted, I am not much of a combination tagger - instead, I am old-fashioned and use a tag to represent a folder. I would love to compare my del.icio.us usage with others. Mail me an XML of all your links and I'll run it through my script.

6.5 billion people since Saturday

3/08/2006

Via MNBC:

At 7:16 p.m. ET on Saturday, the population here on this good Earth hit 6.5 billion people, according to projections.
It's time to re-write those elementary history books again. The chart in this article indicates that there will be 7 billion humans roaming around the Earth a little after 2010 and 9 billion people by 2050. I doubt that any country has the infrastructure in place to support respective growth. Note, these estimates were made in 2002 and might not reflect any recent acceleration over the past 4 years.

Keeping your recipients happy

3/07/2006

Evan Williams has an interesting idea:

It should be both easy and socially acceptable to turn your inbox off from non-critical communications from time to time. So maybe anyone but those in your closest-contact list get a polite message that you're a bit too busy to get back to them right now. And those messages don't go in the same big list of things you need to pay attention to.
Being disconnected sounds good to me. I still think that Gmail should offer a feature that lets your recipients know how much unread mail you have sitting inside your inbox. Such an ability would help prevent users from building expectations about getting an instant reply. An implementation might be to invoke Blogger-like tags in your signature.

Aurora

3/06/2006

Ever wanted to know what an aurora looks like from outer space?



I have no idea where I got this stellar image from. All I know is that the file has been sitting unannotated on my desktop since January 2006.

About I am currently a Senior Engineer at MySpace. Feel free to check out my personal collective.

Archives
April 2005
May 2005
June 2005
July 2005
August 2005
September 2005
October 2005
November 2005
December 2005
January 2006
March 2006
April 2006
May 2006
June 2006
July 2006
August 2006
September 2006
October 2006
November 2006
January 2007
February 2007
April 2007
November 2007
December 2007
January 2008
March 2008
April 2008
May 2008
June 2008

Subscribe to my feed