Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. #! /usr/bin/env python
  2.  
  3. # how to unfollow everyone who isn't following you
  4. # By Jamieson Becker (Public Domain/no copyright, do what you will)
  5.  
  6. # Easy instructions, even if you don't know Python
  7. #
  8. # 1. Install pip (apt-get install python-pip) and then
  9. # pip install tweepy, which is the python twitter client
  10. #
  11. # 2. create a new app in your account at dev.twitter.com
  12. # and then plug in your consumer and app keys below.
  13. # Trim all whitespace at beginning/end of your keys.
  14. #
  15. # 3. the twitter app needs to have permissions changed to
  16. # read-write, as apps are read-only by default.
  17. #
  18. # 4. Execute this script: python unfollow.py
  19.  
  20. import time
  21. import tweepy
  22. import sys
  23.  
  24. auth = tweepy.auth.OAuthHandler(
  25. consumer_key='foo',
  26. consumer_secret='bar')
  27. auth.set_access_token(
  28. 'foobaz',
  29. 'foobar')
  30.  
  31. api=tweepy.API(auth_handler=auth)
  32.  
  33. print "Loading followers.."
  34. follower_objects = [follower for follower in tweepy.Cursor(api.followers).items()]
  35.  
  36. print "Found %s followers, finding friends.." % len(followers)
  37. friend_objects = [friend for friend in tweepy.Cursor(api.friends).items()]
  38.  
  39. # create dictionaries based on id's for easy lookup
  40. friends = dict([(friend.id, friend) for friend in friend_objects])
  41. followers = dict([(follower.id, follower) for follower in follower_objects])
  42.  
  43. # find all your "non_friends" - people who don't follow you even though you follow them.
  44. non_friends = [friend for friend in friend_objects if friend.id not in followers]
  45.  
  46. # double check, since this could be a rather traumatic operation.
  47. print "Unfollowing %s non-following users.." % len(non_friends)
  48. print "This will take approximately %s minutes." % (len(non_friends)/60.0)
  49. answer = raw_input("Are you sure? [Y/n]").lower()
  50. if answer and answer[0] != "y":
  51. sys.exit(1)
  52.  
  53. # start the removal process. In the event of a failure (thanks, twitter!),
  54. # retry once after five seconds. An error on same record again is
  55. # probably more serious issue, so abort with error
  56.  
  57. for nf in non_friends:
  58. print "Unfollowing " + str(nf.id).rjust(10)
  59. try:
  60. nf.unfollow()
  61. except:
  62. print " .. failed, sleeping for 5 seconds and then trying again."
  63. time.sleep(5)
  64. nf.unfollow()
  65. print " .. completed, sleeping for 1 second."
  66. time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement