Twitter to blog script

Twitter logoBased on an example provided with the Twitter library for Python I cobbled together the following script to add my latest tweets to this site. It's called from a cron job that I run on an occasional basis. My script linkifies hashtags and @username tokens in tweets so that you can see search results or user in­for­ma­tion.

Why did I not use one of the WordPress widgets? Well writing scripts like this is fun, and some widgets don't seem to play too well with my Thesis theme. One thing to note is that getting the shell script setup under some cron con­fig­u­ra­tions can take a while if you aren't using it on a regular basis. It's operation is also different between Ubuntu Server and Joyent's Ac­cel­er­a­tor platform.

Up next: a similar script to process my latest bookmarks on Delicious.com.

Main script (tweets.py)

#!/usr/bin/python

import codecs, re, getopt, sys, twitter

TEMPLATE = """
<li>
  <span class="twitter-text">%s</span>
  <span class="twitter-relative-created-at"><a href="http://twitter.com/%s/statuses/%s">Posted %s</a></span>
</li>
"""

def Usage():
  print 'Usage: %s [options] twitterid' % __file__
  print
  print '  This script fetches a users latest twitter update and stores'
  print '  the result in a file as an XHTML fragment'
  print
  print '  Options:'
  print '    --help -h : print this help'
  print '    --output : the output file [default: stdout]'


def FetchTwitter(user, output):
  assert user
  statuses = twitter.Api().GetUserTimeline(user=user, count=7)

  xhtml = []
  for status in statuses:
      status.text = Linkify(status.text)
      xhtml.append(TEMPLATE % (status.text, status.user.screen_name, status.id, status.relative_created_at))

  if output:
    Save(''.join(xhtml), output)
  else:
    print ''.join(xhtml)

def Linkify(tweet):
    tweet = re.sub(r'(\A|\s)@(\w+)', r'\1@\2', tweet)
    return re.sub(r'(\A|\s)#(\w+)', r'\1#\2', tweet)

def Save(xhtml, output):
  out = codecs.open(output, mode='w', encoding='utf-8',
                    errors='xmlcharrefreplace')
  out.write(xhtml)
  out.close()

def main():
  try:
    opts, args = getopt.gnu_getopt(sys.argv[1:], 'ho', ['help', 'output='])
  except getopt.GetoptError:
    Usage()
    sys.exit(2)
  try:
    user = args[0]
  except:
    Usage()
    sys.exit(2)
  output = None
  for o, a in opts:
    if o in ("-h", "--help"):
      Usage()
      sys.exit(2)
    if o in ("-o", "--output"):
      output = a
  FetchTwitter(user, output)

if __name__ == "__main__":
  main()

Shell script executed as cron job (tweets.sh)

/usr/bin/python /path/to/tweets.py brianly --output /path/to/output/twittertimeline.htm

Tagged with cron, python, scripting and twitter.

Keeping files in sync with Dropbox

Dropbox logoWhen you use more than one computer on a daily basis, keeping files in sync between them is a constant problem. I'm familiar with tools like Subversion and Mercurial that make it relatively easy to keep code in sync between machines, but these require explicit actions that I often want to control. When it comes to photos, office documents, and other binary files that I don't want to actively manage these developer tools just get in my way. Thankfully a Y Combinator startup called Dropbox is trying to solve this problem and doing a pretty good job at it.

Dropbox is very simple to use. Just drop a file in a folder and it'll be uploaded to the central server. If your other computer is connected to Dropbox it'll be updated with the file almost im­me­di­ate­ly. It is worth noting that these updates are almost instant, even for large files. Whilst a sync tool like this is all very simple in theory, the execution is near flawless. I've tried out other tools in the past and none of them had seen the attention to detail that the Dropbox team made for their ap­pli­ca­tion. It get's better when you discover that Dropbox works just as well across Windows, Mac, and Linux.

I'm just about to shell out for one of their paid accounts so that I can keep my photos in sync between my Mac and Windows desktop. I'll post an update on my experience in a couple of months. I'm sure I'll try out some of the neat tricks detailed on their wiki.

Tagged with dropbox, sync and synchronization.