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:

      #!/usr/bin/python2.4
      #
      # Copyright (c) Michael Still 2007, released under the terms of the GNU GPL v2
      
      """blather.py -- 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/stillhq.com/blather/'
      
      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 = datetime.datetime.now().strftime('%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)
            p_file.close()
          else:
            data['upto'] = 0
      
          # Append this message to the pickle
          data[data['upto'] + 1] = '>b<%s>/b<: %s>br/<' \
                                   %(datetime.datetime.now().strftime('%H:%M'), line)
          data['upto'] += 1
      
          # Save our state
          p_file = open(p_filename, 'w')
          cPickle.dump(data, p_file)
          p_file.close()
      
          # And then churn out some HTML
          b_filename = '%s%s.blog' %(_prefix, filename)
          b_file = open(b_filename, 'w')
          b_file.write('Blathering for %s\n'
                       % datetime.datetime.now().strftime('%A, %d %B %Y'))
          for i in range(1, data['upto'] + 1):
            b_file.write('%s\n' % data[i])
      
          b_file.write('\n[btags: ]')
          b_file.close()
      
          return "Done"
      
        return 'Huh?'
      
      def Cleanup():
        """Cleanup -- you're about to be unloaded"""
        return
      
      # 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: mbot: new hotness in Google Talk bots; mbot: new hotness in Google Talk bots; A MythTV Jabber bot; Tridge demonstrates BitKeeper hack; Renaming mbot to gtalkbot; gtalkbot 1.1

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