Created
February 16, 2014 03:50
-
-
Save RouxRC/9028951 to your computer and use it in GitHub Desktop.
Tester for python-twitter streams
This file contains 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
# -*- 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Examples of test combinations: