def get_followers(user_id): """Takes a Twitter user ID as input, and returns that user's followers in the form of a list of Twitter IDs.""" base_url = 'https://api.twitter.com/1.1/followers/ids.json' params = {'user_id': user_id, 'count': 5000} all_followers = [] while True: id_info = twitter_get(base_url, params, 60) if id_info is None: return None # something went wrong elif id_info['next_cursor'] == 0: return all_followers else: all_followers.extend(id_info['ids']) params['cursor'] = id_info['next_cursor_str'] print '{0} followers retrieved so far.'.format(len(all_followers))
def get_screen_name_to_id_map(screen_names): """Takes a list of Twitter screen names as input, and returns a dictionary mapping lower-case screen names to IDs.""" base_url = 'https://api.twitter.com/1.1/users/lookup.json' screen_name_to_id_map = {} chunk_size = 100 # API call accepts 100 users at a time for i in xrange(0, len(screen_names), chunk_size): params = {'screen_name': ','.join(screen_names[i:i + chunk_size])} users = twitter_get(base_url, params, 5) if users is None: return None # something went wrong else: for user in users: screen_name_to_id_map[user['screen_name'].lower()] = user['id_str'] return screen_name_to_id_map
def get_screen_name_to_id_map(screen_names): """Takes a list of Twitter screen names as input, and returns a dictionary mapping lower-case screen names to IDs.""" base_url = 'https://api.twitter.com/1.1/users/lookup.json' screen_name_to_id_map = {} chunk_size = 100 # API call accepts 100 users at a time for i in xrange(0, len(screen_names), chunk_size): params = {'screen_name': ','.join(screen_names[i:i + chunk_size])} users = twitter_get(base_url, params, 5) if users is None: return None # something went wrong else: for user in users: screen_name_to_id_map[ user['screen_name'].lower()] = user['id_str'] return screen_name_to_id_map
def __get_tweets(params): """Takes a Twitter user id (a string) or screen name as input (screen_name takes precedent) and returns all of that user's tweets, going back as far as the API will let you.""" base_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json' params['count'] = 200 all_tweets = [] while True: chunk_of_tweets = twitter_get(base_url, params, 5) if chunk_of_tweets is None: return None # something went wrong elif len(chunk_of_tweets) == 0: return all_tweets else: all_tweets.extend(chunk_of_tweets) min_id_found = min([tweet['id'] for tweet in chunk_of_tweets]) params['max_id'] = str(min_id_found - 1) # we want tweets with IDs lower than min_id_found print '{0} tweets retrieved so far.'.format(len(all_tweets))
def __get_tweets(params): """Takes a Twitter user id (a string) or screen name as input (screen_name takes precedent) and returns all of that user's tweets, going back as far as the API will let you.""" base_url = "https://api.twitter.com/1.1/statuses/user_timeline.json" params["count"] = 200 all_tweets = [] while True: chunk_of_tweets = twitter_get(base_url, params, 5) if chunk_of_tweets is None: return None # something went wrong elif len(chunk_of_tweets) == 0: return all_tweets else: all_tweets.extend(chunk_of_tweets) min_id_found = min([tweet["id"] for tweet in chunk_of_tweets]) params["max_id"] = str(min_id_found - 1) # we want tweets with IDs lower than min_id_found print "{0} tweets retrieved so far.".format(len(all_tweets))