Created
September 21, 2012 19:45
-
-
Save swinton/3763471 to your computer and use it in GitHub Desktop.
Connect to Twitter, track a hashtag, read out the tweets!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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