casmlab / stack

The BITS Lab STACK tool for social media collection and analysis.
http://bits.ischool.syr.edu/
MIT License
1 stars 0 forks source link

Get Followers #3

Open casmlab-bot opened 7 years ago

casmlab-bot commented 7 years ago

Need script to collect follower lists for each tracked account; periodic updates but not on demand

jhemsley commented 7 years ago

This could help as a starting point. It just collects all followers and deals with the rate limits. Note that the rate limits mean that it can take a very long time to get a full list. For example, it took more than a week to get Trumps followers in 2015.

import sys import time import json import tweepy from tweepy.error import TweepError

http://docs.tweepy.org/en/latest/api.html

https://dev.twitter.com/rest/reference/get/followers/list

consumer_key ='xxx' consumer_secret='xxx' access_token='xxx' access_secret='xxx'

def main(handle):

Authenticate with Twitter

auth = tweepy.auth.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)

if api.verify_credentials:
    print "Successfully authenticated with Twitter. Now collecting followers for: %s" % handle
else:
    print "Failed to authenticate with Twitter. Please try again."
    sys.exit(1)

with open('%s-followers.txt' % handle, 'a') as outfile:
    followers = tweepy.Cursor(api.followers, screen_name=handle, count=200).items()
    writing = True
    f_count = 1

    while writing:
        try:
            f = next(followers)
            print '#%d: %s' % (f_count, f.screen_name)
            outfile.write(f.screen_name + '\n')
            f_count += 1

        except TweepError as e:
            print 'Received timeout. Sleeping for 15 minutes.'
            time.sleep(15*60)

        except StopIteration as e:
            print 'Follower collection completed.'
            writing = False

if name == 'main': main(sys.argv[1])