PyTwitFace: Twitter/Facebook mashup with Python

April 13, 2008 in Design & Programming, Technology |

PyTwitFace

I’ve recently started using Twitter, but its “What are you doing?” functionality does have some overlap with the Facebook “Status“. It’d be nice to join these two things together, so that an update in one place would automatically update the other.

The current solutions to this (both the official Twitter Facebook app, and the unofficial Twittersync app) allow you to import your Twitter updates into Facebook. This is great for some people, but I’d rather not force all my none-geeky Facebook friends to put up with my geeky Twittering.

A better solution for me would be syncing from Facebook to Twitter. Facebook publishes an RSS feed of your status updates, and Twitterfeed can monitor feeds and post updates to Twitter. Perfect.. except it seems to be a bit flaky (I was getting double posts to Twitter of all my Facebook updates). It’s also lacking some customisation options which I’d like.

So.. if all else fails, do it yourself. Below is PyTwitFace, a Python script which grabs your RSS feed from Facebook, checks whether your most recent update has been posted to Twitter, and if not, posts it.

You will need:

This script doesn’t do any caching. It checks your recent Twitter updates to see if your Facebook status has already been imported. Twitter only publishes your twenty most recent Twitter updates via its API. So, if you update your Facebook status, then post 20 updates to Twitter, your status will be copied over again (duplicated). This isn’t a problem for me, but it may be for you.

The script does a bit of pre-processing of your Facebook status before posting it to Twitter. It chops off the first word (which is your name), and then checks whether the the next word is in a list of verbs (mine includes “is” and “has”, but you can add your own). It then appends “(from Facebook)” to the message. This means that a Facebook status of:

Jamie is trying out PyTwitFace.

will appear on Twitter as..

Trying out PyTwitFace (from Facebook)

Here’s the code. Don’t forget to have a look at it, and put in your Twitter username and password and your Facebook feed URL.

  1. import twitter, feedparser, socket
  2.  
  3. #
  4. # SETTINGS
  5. #
  6.  
  7. twitteruser = 'username'
  8. twitterpassword = 'password'
  9. facebook_feed_address = "Your full Facebook status feed URL"
  10.  
  11. # Will be appended to facebook status
  12. to_append = "(from Facebook)"
  13.  
  14. # These words will be removed from the
  15. # beginning of your status if present
  16. verbs = ['is', 'has']
  17.  
  18.  
  19.  
  20. # Make sure the script doesn't hang if it can't get through to
  21. # Twitter or Facebook (ie network is down).
  22. timeout = 60 # seconds
  23. socket.setdefaulttimeout(timeout)
  24.  
  25. #
  26. # FACEBOOK STUFF
  27. #
  28.  
  29. # Get most recent facebook status.
  30. feed = feedparser.parse(facebook_feed_address)
  31. most_recent_facebook = feed.entries[0].title
  32.  
  33. # Convert facebook status string into a list of words
  34. most_recent_facebook_words = most_recent_facebook.split()
  35.  
  36. # Get rid of name from start of string.
  37. del most_recent_facebook_words[0]
  38.  
  39. # If next word is in verb list, remove it.
  40. if most_recent_facebook_words[0] in verbs:
  41. del most_recent_facebook_words[0]
  42.  
  43. # Capitalise first word.
  44. most_recent_facebook_words[0] = most_recent_facebook_words[0].title()
  45.  
  46. # Append selected string.
  47. most_recent_facebook_words.append(to_append)
  48.  
  49. # Convert back into a string
  50. most_recent_facebook = " ".join(most_recent_facebook_words)
  51.  
  52. #
  53. # TWITTER STUFF
  54. #
  55.  
  56. # Set up API
  57. twitterapi = twitter.Api(username=twitteruser, password=twitterpassword)
  58.  
  59. # Get list of statuses
  60. twitter_statuses = twitterapi.GetUserTimeline(twitteruser)
  61. twitter_statuses_text = [s.text for s in twitter_statuses]
  62.  
  63. # Check if your current Facebook status
  64. # has already been posted to Twitter
  65. if not most_recent_facebook in twitter_statuses_text:
  66. # If not, post it!
  67. twitterapi.PostUpdate(most_recent_facebook)
  68.  
  69.  

You can either run this manually when you update your Facebook status (which may slightly defeat the object), or put it on a server and set up a cron job, or (like me) run it automatically every so often on your own computer. I’m doing this using launchd in Mac OS X. See my Physical Gmail Notifier post for instructions of how to do this.

A final note: while I was writing this post, I came across Twitterbook, which does a similar thing with PHP. It does seem to require manually visiting the script’s URL to work though, which isn’t ideal. Try it out if you’re more of a PHP person.

1 Comment »

RSS feed for comments on this post. TrackBack URI

  1. Excellent work, I’ll install that when I’ve got time. I studied at Sussex, I hope you’re enjoying it :)

    Comment by Mike G — July 31, 2008 #

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Powered by WordPress with modified Pool theme design by Borja Fernandez.
Entries and comments feeds. Valid XHTML and CSS. ^Top^