Created
March 13, 2015 17:49
-
-
Save gion-pop/886b2c05cc9b33a8c054 to your computer and use it in GitHub Desktop.
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
# "TwitterAPI" library on PyPI | |
from TwitterAPI import ( | |
TwitterAPI, | |
TwitterRequestError, | |
TwitterConnectionError, | |
) | |
from auth_info import ( | |
consumer_key, | |
consumer_secret, | |
access_token_key, | |
access_token_secret, | |
) | |
api = TwitterAPI( | |
consumer_key, | |
consumer_secret, | |
access_token_key, | |
access_token_secret | |
) | |
rayban_urls = ['http://goo.gl/pVssDM'] | |
def get_next_user(): | |
iterator = api.request( | |
'statuses/filter', | |
{'track': 'goo gl pVssDM'} | |
).get_iterator() | |
try: | |
for tweet in iterator: | |
if 'text' in tweet: | |
for url in tweet['entities']['urls']: | |
if url.get('expanded_url', None) in rayban_urls: | |
yield tweet['user']['screen_name'] | |
elif 'disconnect' in tweet: | |
event = tweet['disconnect'] | |
if event['code'] in [2,5,6,7]: | |
# something needs to be fixed before re-connecting | |
raise Exception(event['reason']) | |
else: | |
# temporary interruption, re-try request | |
break | |
except TwitterRequestError as e: | |
if e.status_code < 500: | |
# something needs to be fixed before re-connecting | |
raise | |
else: | |
# temporary interruption, re-try request | |
pass | |
except TwitterConnectionError: | |
# temporary interruption, re-try request | |
pass | |
if __name__ == '__main__': | |
for screen_name in get_next_user(): | |
print(screen_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment