Skip to content

Instantly share code, notes, and snippets.

@swinton
Created January 6, 2010 17:00
Show Gist options
  • Save swinton/83cc7bfda15ec859c662 to your computer and use it in GitHub Desktop.
Save swinton/83cc7bfda15ec859c662 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
Have OS X read tweets aloud to you.
"""
# Inspired by this gist: http://gist.github.com/267147
# Uses the Twitter streaming API, and TweetStream (http://bitbucket.org/runeh/tweetstream/src/)
import getopt, getpass, socket, subprocess, sys, tweetstream
# 'default_bufsize = 0' trick via http://blog.persistent.info/2009/08/twitter-streaming-api-from-python.html
socket._fileobject.default_bufsize = 0
def say(s):
subprocess.call(['say', str(s)])
def speak_tweets(user, passwd, follow):
try:
stream = tweetstream.FollowStream(user, passwd, follow)
for tweet in stream:
say('%s says: %s' % (tweet['user']['name'], tweet['text']))
except KeyboardInterrupt:
sys.exit(0)
def usage():
sys.stdout.write("tweet_speak.py -u <user> -p <password> -f <comma-seperated list of twitter user ids to follow>\n")
def main(argv):
try:
opts, args = getopt.getopt(argv, "u:p:f:", ["user=", "password=", "follow="])
except getopt.GetoptError:
usage()
sys.exit(2)
user = None
passwd = None
follow = None
for o, a in opts:
if o in ("-u", "--user"):
user = a
elif o in ("-p", "--password"):
passwd = a
elif o in ("-f", "--follow"):
follow = a.split(",")
if user == None or follow == None:
usage()
sys.exit(2)
if passwd == None:
passwd = getpass.getpass()
speak_tweets(user, passwd, follow)
if __name__ == '__main__':
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment