Last active
December 26, 2015 16:42
-
-
Save onurmatik/a9497794779e0f8e6ca9 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
""" | |
- Takes a file which has one name on each line | |
- Searches for Twitter users; tries combinations for names longer than 2 words | |
- Returns the most probable username | |
""" | |
from twython import Twython | |
import itertools | |
twitter = Twython( | |
CONSUMER_KEY, | |
CONSUMER_SECRET, | |
ACCESS_TOKEN, | |
ACCESS_TOKEN_SECRET, | |
) | |
def get_twitter_user(name): | |
try: | |
result = twitter.search_users( | |
q=n, | |
count=1, | |
include_entities='false', | |
) | |
except TwythonRateLimitError as e: | |
time.sleep(int(e.retry_after) - int(time.time())) | |
return get_twitter_user(name) | |
else: | |
return result | |
f = open('names.txt', 'r') | |
for name in f: | |
if name: | |
combinations = [name] | |
names_split = name.split(' ') | |
if len(names_split) > 2: | |
combinations += [' '.join(n) for n in itertools.combinations(names_split, 2)] | |
for n in combinations: | |
result = get_twitter_user(n) | |
if result: | |
print result[0]['screen_name'] | |
break | |
if not result: | |
print '-' | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment