Created
August 20, 2012 16:25
-
-
Save swinton/3405525 to your computer and use it in GitHub Desktop.
Example of fetching followers of multiple Twitter accounts recursively, using twitterspawn.
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 | |
""" | |
Example of fetching followers of multiple Twitter accounts recursively, using twitterspawn. | |
""" | |
import atexit | |
import json | |
import twitterspawn | |
followers = {} | |
def savefollowers(): | |
""" | |
Save data on exit | |
""" | |
fp = open("followers.json", "wb") | |
json.dump(followers, fp) | |
fp.close() | |
def callback(response, worker): | |
""" | |
Handle Twitter API responses | |
""" | |
try: | |
has_next_cursor = hasattr(response, "json") and "next_cursor" in response.json and response.json["next_cursor"] | |
has_ids = hasattr(response, "json") and "ids" in response.json and response.json["ids"] | |
except: | |
has_next_cursor = False | |
has_ids = False | |
if has_next_cursor: | |
# Add request to get the next page of followers too | |
params = response.request.params | |
params.update(dict(cursor=response.json["next_cursor_str"])) | |
twitterspawn.add_request("https://api.twitter.com/1/followers/ids.json", | |
dict(params=params), | |
callback) | |
if has_ids: | |
# Process followers | |
followers.setdefault(response.request.params["screen_name"], []) | |
followers[response.request.params["screen_name"]].extend(response.json["ids"]) | |
def main(): | |
""" | |
Configure and launch twitterspawn | |
""" | |
# Requests... | |
twitterspawn.add_request("https://api.twitter.com/1/followers/ids.json", | |
dict(params=dict(screen_name="barackobama")), | |
callback) | |
twitterspawn.add_request("https://api.twitter.com/1/followers/ids.json", | |
dict(params=dict(screen_name="justinbieber")), | |
callback) | |
twitterspawn.add_request("https://api.twitter.com/1/followers/ids.json", | |
dict(params=dict(screen_name="ashtonkutcher")), | |
callback) | |
twitterspawn.add_request("https://api.twitter.com/1/followers/ids.json", | |
dict(params=dict(screen_name="ladygaga")), | |
callback) | |
# Workers... | |
twitterspawn.add_worker(access_token="YOUR_FIRST_ACCESS_TOKEN", | |
access_token_secret="YOUR_FIRST_ACCESS_TOKEN_SECRET", | |
consumer_key="YOUR_FIRST_CONSUMER_KEY", | |
consumer_secret="YOUR_FIRST_CONSUMER_SECRET", | |
header_auth=True) | |
twitterspawn.add_worker(access_token="YOUR_NEXT_ACCESS_TOKEN", | |
access_token_secret="YOUR_NEXT_ACCESS_TOKEN_SECRET", | |
consumer_key="YOUR_NEXT_CONSUMER_KEY", | |
consumer_secret="YOUR_NEXT_CONSUMER_SECRET", | |
header_auth=True) | |
# | |
# MOAR WORKERS HERE... | |
# | |
twitterspawn.add_worker(access_token="YOUR_LAST_ACCESS_TOKEN", | |
access_token_secret="YOUR_LAST_ACCESS_TOKEN_SECRET", | |
consumer_key="YOUR_LAST_CONSUMER_KEY", | |
consumer_secret="YOUR_LAST_CONSUMER_SECRET", | |
header_auth=True) | |
# Let's go!!! | |
twitterspawn.go() | |
if __name__ == "__main__": | |
atexit.register(savefollowers) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment