Run with...
funtimes = FollowerGetter("openlab", cks)
funtimes.get_followers()
TO DO: This works now. :)
consumer_key = "use" | |
consumer_secret = "your" | |
access_token = "own" | |
access_token_secret = "values" |
import tweepy | |
import secrets as cks | |
import time | |
import csv | |
class FollowerGetter: | |
def __init__(self, username, authfile): | |
self.username = username | |
self.authfile = authfile | |
self.followers = [] | |
def authenticate(self): | |
cks = self.authfile | |
auth = tweepy.OAuthHandler(cks.consumer_key, cks.consumer_secret) | |
auth.set_access_token(cks.access_token, cks.access_token_secret) | |
api = tweepy.API(auth, wait_on_rate_limit=True, | |
wait_on_rate_limit_notify=True) | |
return api | |
def get_followers(self): | |
self.authenticate() | |
print ("getting followers") | |
bunch = self.followers | |
username = self.username | |
try: | |
for page in tweepy.Cursor(api.followers, screen_name=username, | |
count=200).pages(): | |
bunch.extend(page) | |
print ("patience!") | |
except tweepy.error.RateLimitError: | |
print ("Waiting a minute ...") | |
time.sleep(60) # Sleep for 60sec | |
self.write_csv(bunch) | |
def write_csv(self, resultset): | |
filename = "followers-" + self.username + ".csv" | |
with open(filename, 'w', newline='') as fp: | |
writer = csv.writer(fp, delimiter=',') | |
writer.writerow(["id", "screen_name", "name", "verified", | |
"description", "blocked_by"]) | |
for user in resultset: | |
writer.writerow([user.id, user.screen_name, user.name, | |
user.verified, user.description, | |
user.blocked_by]) |