Skip to content

Instantly share code, notes, and snippets.

@RouxRC
Created February 16, 2014 03:50
Show Gist options
  • Save RouxRC/9028951 to your computer and use it in GitHub Desktop.
Save RouxRC/9028951 to your computer and use it in GitHub Desktop.
Tester for python-twitter streams
# -*- coding: utf-8 -*-
# USAGE:
# First create a keys.py file setting your api keys: KEY, SECRET, OAUTH_TOKEN, OAUTH_SECRET
# python test_twitter_stream.py [block|noblock|timeout] [low|high|high][+] [regular|user]
import sys
from twitter import OAuth, TwitterStream
from keys import KEY, SECRET, OAUTH_TOKEN, OAUTH_SECRET
stream = 'regular'
if len(sys.argv) > 3:
stream = sys.argv[3].lower()
traffic = 'low'
if len(sys.argv) > 2:
traffic = sys.argv[2].lower()
mode = 'block'
if len(sys.argv) > 1:
mode = sys.argv[1].lower()
args = {'auth': OAuth(OAUTH_TOKEN,OAUTH_SECRET,KEY,SECRET)}
qargs = {'track': 'youpiiiiii'}
method = 'statuses/filter'
if mode == 'noblock':
args['block'] = False
if mode.startswith('timeout'):
args['timeout'] = int(mode.replace('timeout', ''))
if stream == 'user':
args['domain'] = 'userstream.twitter.com'
method = 'user'
if traffic.endswith('+'):
qargs['delimited'] = 'length'
if traffic.startswith('high'):
qargs['track'] = 'bieber,gaga'
ct = 0
for a in getattr(TwitterStream(**args), method)(**qargs):
if not a: continue
ct += 1
if isinstance(a, dict):
if "id_str" in a:
print ct, a['id_str'], a['text']
elif "friends" in a:
print ct, len(a['friends']), "friends"
else: print ct, a
else: print ct, a
@RouxRC
Copy link
Author

RouxRC commented Feb 16, 2014

Examples of test combinations:

python2.6 test_twitter_stream.py block low regular
python2.7 test_twitter_stream.py noblock low regular 
python3.2 test_twitter_stream.py noblock high user
python2.6 test_twitter_stream.py timeout20 low+ user
python2.6 test_twitter_stream.py timeout60 low+ user
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment