Guest User

Untitled

a guest
Jan 12th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. def get_user_followers(username):
  2. """
  3. Retrieves data on all followers of targetted account.
  4. WARNING: There may be hundreds of thousands or millions of followers
  5. Be sure you want this data before using this function!
  6. """
  7. import pdb; pdb.set_trace()
  8. api = oauth_authenticate()
  9. datestamp = str(datetime.datetime.now().strftime("%Y-%m-%d"))
  10. target = api.get_user(screen_name = username)
  11. target_id = target.id
  12. next_cursor = -1
  13. index = 0
  14. followersdata = {}
  15. while next_cursor:
  16. try:
  17. for x in limit_handled(tweepy.Cursor(api.followers, screen_name = username).items()):
  18. followersdata[index] = {}
  19. followersdata[index]['screen_name'] = x._json['screen_name']
  20. followersdata[index]['id_str'] = x._json['id_str']
  21. followersdata[index]['name'] = x._json['name']
  22. followersdata[index]['description'] = x._json['description']
  23. followersdata[index]['targeted_user_id'] = target_id
  24. index = index + 1
  25. next_cursor = x._json["next_cursor"]
  26. except (TweepError, ConnectionError) as exc:
  27. print(exc)
  28. print("Waiting 15 minutes for next API window to open")
  29. print("Current Time is:", time.strftime("%I:%M:%S"))
  30. time.sleep(60*15)
  31. del api
  32. api = oauth_authenticate()
  33. continue
  34. followersDF = pd.DataFrame.from_dict(followersdata, orient = "index")
  35. followersDF.to_csv("%s-%s followers list.csv" % (username, datestamp),
  36. index = False, encoding = 'latin-1')
Add Comment
Please, Sign In to add comment