Blather, an open source Twitter work-alike for Blosxom and Google Talk

    This post is brought to you by jet lag (I got up at 5am and couldn't get back to sleep), music (the Black Eyed Peas!), morning coffee, and my confusion about Twitter.

    Another thing I don't understand about Twitter is given it's simplicity why it hasn't been implemented by three billion sites by now. To that end, I give you blather, a gtalkbot module that implements Twitter-like features for blosxom (the blog engine I use for this site).

    Here's the code:

      # Copyright (c) Michael Still 2007, released under the terms of the GNU GPL v2
      """ -- a very simple replacement for Twitter that works with blosxom
      import cPickle
      import datetime
      import os
      # This is the location to write the blog files to, you'll need to customise it
      _prefix = '/data/'
      def Verbs():
        """Verbs -- return the verbs which this module supports"""
        return ['b']
      def Status():
        """Status -- suggest a message to display as the status string for the bot"""
        return 'Blathering away'
      def Help(verb):
        """Help -- display help for a verb"""
        if verb == 'b':
          return 'Use this to have whatever you say blogged in the blather category'
        return ''
      def Command(verb, line):
        """Command -- execute a given verb with these arguments"""
        if verb == 'b':
          data = {}
          filename ='%Y%m%d')
          print 'Blathering %s into %s' %(line, filename)
          # Load the pickle for this day (if it exists)
          p_filename = '%s%s.pkl' %(_prefix, filename)
          if os.path.exists(p_filename):
            p_file = open(p_filename, 'r')
            data = cPickle.load(p_file)
            data['upto'] = 0
          # Append this message to the pickle
          data[data['upto'] + 1] = '>b<%s>/b<: %s>br/<' \
                                   %('%H:%M'), line)
          data['upto'] += 1
          # Save our state
          p_file = open(p_filename, 'w')
          cPickle.dump(data, p_file)
          # And then churn out some HTML
          b_filename = '' %(_prefix, filename)
          b_file = open(b_filename, 'w')
          b_file.write('Blathering for %s\n'
                       %'%A, %d %B %Y'))
          for i in range(1, data['upto'] + 1):
            b_file.write('%s\n' % data[i])
          b_file.write('\n[btags: ]')
          return "Done"
        return 'Huh?'
      def Cleanup():
        """Cleanup -- you're about to be unloaded"""
      # Do initialisation here
      print u'blather_bot is loading'

    This took about 15 minutes to write using gtalkbot (which does basically all the heavy lifting). Now I can IM messages to my home machine, it will generate a blog post per day of the IM messages, and they then get pushed to the live site regularly. Cool, huh?

    Tags for this post: blather source code google talk gtalk bot
    Related posts: A MythTV Jabber bot; mbot: new hotness in Google Talk bots; mbot: new hotness in Google Talk bots; gtalkbot 1.2; Renaming mbot to gtalkbot; MythTV talk at Google

posted at: 07:07 | path: /blather | permanent link to this entry