Skip to content

Instantly share code, notes, and snippets.

@swinton
Created September 21, 2012 19:45
Show Gist options
  • Select an option

  • Save swinton/3763471 to your computer and use it in GitHub Desktop.

Select an option

Save swinton/3763471 to your computer and use it in GitHub Desktop.
Connect to Twitter, track a hashtag, read out the tweets!
#!/usr/bin/env python
"""Connect to Twitter, track a hashtag, read out the tweets!"""
import getpass
import json
import random
import sys
import subprocess
import multiprocessing
import requests
queue = multiprocessing.Queue()
def say(voices='alex,victoria,albert,zarvox'):
voices = voices.split(',')
while True:
line = queue.get()
print "Saying:", line
subprocess.call(['say', '-v', random.choice(voices), line])
def talky_tweety(track, username, password):
"""Connect to Twitter, track a hashtag, read out the tweets!"""
try:
process = multiprocessing.Process(target=say)
process.start()
r = requests.post('https://stream.twitter.com/1.1/statuses/filter.json',
data={'track': track}, auth=(username, password), prefetch=False)
for line in r.iter_lines():
if line: # filter out keep-alive new lines
tweet = json.loads(line)
if "text" in tweet:
queue.put(tweet["text"])
except KeyboardInterrupt:
process.terminate()
process.join()
if __name__ == "__main__":
track = sys.argv[1]
username = raw_input("Username: ")
password = getpass.getpass("Password: ")
talky_tweety(track, username, password)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment