Showing posts with label blogger. Show all posts
Showing posts with label blogger. Show all posts

Monday, October 30, 2006

Retrieving Your BloggerBeta Blog ID

Some people are wondering how to programatically get the blogID that the Google Data API page keeps referring to. I'm not sure if this is the best way or not, but I've had success using the following method in Python:

def getBlogID(uri):
    import httplib2, re
    con = httplib2.Http()
    response, content = con.request(uri, 'GET')
    match = re.search('blogID=(\d*)', content)
    if match:
        return match.group(1)
    else:
        print "BlogID retrieval failed."

It's quite simple actually, it takes your blog's URL as a parameter (as in http://whatever.blogspot.com), and sends an empty GET request to it. The response returned contains a string that matches the regular expression blogID=(\d*). That is, the string "blogID=" followed by a bunch of numbers. That bunch of numbers is the blogID. The function shown above extracts that number with a regular expression, and returns it.

Sunday, October 22, 2006

Vim-Blogger Featuring Markdown

Yes I'm still playing with this vim-blogger plugin. Now it has the ability to allow you to compose your post using John Gruber's Markdown syntax instead of plain ol' ugly html :). To accomplish this task, I am using a Python module for the conversion to HTML when posted.

Also cool is the fact that when a post is retrieved from your blog using this plugin, it is converted back to Markdown syntax using html2text. I think this is pretty cool. Here is a screenshot of me composing this post:

Vim-BloggerBeta with Markdown

As you can see, I have a Vim syntax file for Markdown files too. I've modified it a bit to include vim-blogger specific things like the @@LABELS@@ line at the bottom etc. I do plan on making a release of this plugin at some point soon. It's too buggy just yet though. When I do, it will be composed of multiple files including the markdown and html2text Python modules, the syntax files, an ftplugin directory, and a regular (small) blogger.vim plugin file.

Using Markdown syntax has both advantages and disadvantages. The biggest advantage is the fact that it is easier to read and write when compared to HTML code. Also, it is rapidly becoming a popular method of writing structured text. A disadvantage is that the syntax still has some weaknesses. For example as of now there is no way to specify the size of an embedded image and there's no way to make it float right or left in your text. This can be done with embedded HTML, but then it would get messed up during the conversion back to Markdown in the context of this Vim plugin. For now though, it's fun to play with :).

Saturday, October 14, 2006

See the Vim-BloggerBeta Plugin in Action

I made a funky flash screencast of my vim-blogger plugin in action for all to see :). The screencast demonstrates saving a draft post, retrieving a list of all posts on the blog, editing a saved post, publishing a post, adding labels to a post, and finally deleting a post. The swf file ended up being quite large, but hopefully managable, and since Blogger doesn't allow hosting such files (yet?), I have to host it on my slow home server for now. Let me know if it is unbearably slow.

I do plan on releasing the plugin some day soon so all Vim/Blogger-beta users can make use of it, but first it needs some serious testing. The last thing I want to happen is for me to make it available too early, then have somebody pooch their data because of it. That and the thing is a bloody mess. I wrote it while discovering how to use the Blogger GData API, and the code shows it. Once it's in a safe state that I don't mind putting my name on, I'll make it public ;p.

Until then, enjoy the screencast, and leave comments or suggestions if you have any!

Friday, October 13, 2006

Success! Posting to Blogger-beta Using Vim

If you see this, then I've finally (at least partially) figured out how to use the Blogger-beta GData API from Python. And to make posting a little bit quicker/easier for me, I've stuck that Python code into a Vim plugin.

To be honest, I'm not entirely sure what I was doing wrong in my previous attempts. I've changed several things, then changed some back. The resulting code really doesn't look much different to me than it did before, but there are a few very subtle changes. I suspect my ignorance when it comes to HTTP and XML protocols played a large part in my frustrations.

Anyways, here is a working version of the Python code that works for posting a blog entry. You will obviously need to fill in your BLOGID, GMAIL_ADDRESS, and GMAIL_PASSWORD appropriately for it to work for you:

#!/usr/bin/env python

import httplib2, re

account = "GMAIL_ADDRESS"
password = "GMAIL_PASSWORD"
blogid = "BLOGID"

def authenticate(h):
    auth_uri = 'https://www.google.com/accounts/ClientLogin'
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    myrequest = "Email=%s&Passwd=%s&service=blogger&service=TestCompany-TestApp-0.0" % (account, password)
    response, content = h.request(auth_uri, 'POST', body=myrequest, headers=headers)
    if response['status'] == '200':
        return re.search('Auth=(\S*)', content).group(1)
    else:
        return None

entry = """<?xml version="1.0" ?>
    <entry xmlns='http://www.w3.org/2005/Atom'>
      <title type='text'>Test Post</title>
      <content type='xhtml'>
        <div xmlns="http://www.w3.org/1999/xhtml">
        If you are reading this, then it worked!
        </div>
      </content>
      <author>
        <name>TestUser</name>
      </author>
    </entry>
    """

h = httplib2.Http()
uri = 'http://www.blogger.com/feeds/%s/posts/full' % blogid

# Get the Auth token from ClientLogin
auth = authenticate(h)
if auth:
    headers = {'Content-Type': 'application/atom+xml', 'Authorization': 'GoogleLogin auth=%s' % auth.strip()}
    response, content = h.request(uri, 'POST', body=entry, headers=headers)

    # blindly follow redirects
    while response['status'] == '302':
        response, content = h.request(response['location'], 'POST', body=entry, headers=headers)

    if response['status'] == '201':
        print "Entry successfully posted."
    else:
        print "Post failed: %s" % response['status']
else:
    print "Authorization failed."

It's pretty rough, I realize, but I think it's good enough to get anyone started if you are interested. I hope someone will find this useful.

Now to see what other functionality I can perform :).

Oh, I almost forgot. If you want the Vim plugin I'm using you can get it here. To install it, drop it in ~/.vimrc/plugins/. To use it, open vim, type your post's subject on the first line, the post body below that, and type :BlogPost when you are done.

Wednesday, October 11, 2006

Blogger Template Customization

Today I decided to customize my Blogger template a little bit to make it differ from the typical cookie cutter blogs on here. I'm not an expert on CSS and HTML by any means, but I think I'm pleased with the results so far. Aside from the shot of my ugly mug I stuck up there at the top of the page, I think it's fairly easy on the eyes. Of course, I've only viewed it with Firefox, so if you see any errors or problems with the page layout at all, please let me know.

By the looks of it, one pretty much has complete control over the look and feel of their Blogger-beta website. I could touch and edit pretty much every aspect of the page from what I could see. Like I said, however, I'm no expert so I didn't want to delve too deeply into things for fear of messing them up beyond repair.