Guest User

Untitled

a guest
Oct 12th, 2011
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 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.  
  7. # Easy instructions even if you don't know Python
  8. #
  9. # 1. remember to pip or easy_install tweepy
  10. #
  11. # 2. edit the script below and plug in your keys. Don't leave
  12. # whitespace (i.e., spaces) at beginning or end of any keys
  13. #
  14. # 3. you need to create a new app in your account at dev.twitter.com
  15. # and then plug in the new consumer and app keys below.
  16. #
  17. # 4. the app needs to be set to read-write, but apps are read-only
  18. # by default. that's all there is to it.
  19. #
  20. # 5. Execute this script by either python unfollow.py OR
  21. # chmod +x unfollow.py and then execute it: ./unfollow.py
  22.  
  23. import time
  24. import tweepy
  25. import sys
  26.  
  27. auth = tweepy.auth.OAuthHandler(
  28. consumer_key='foo',
  29. consumer_secret='bar')
  30. auth.set_access_token(
  31. 'foobaz',
  32. 'foobar')
  33.  
  34. # the following dictionaries etc aren't strictly needed for this
  35. # but useful for your own more in-depth apps.
  36.  
  37. api=tweepy.API(auth_handler=auth)
  38.  
  39. print "Loading followers.."
  40. followers = []
  41. for follower in tweepy.Cursor(api.followers).items():
  42. followers.append(follower)
  43.  
  44. print "Found %s followers, finding friends.." % len(followers)
  45. friends = []
  46. for friend in tweepy.Cursor(api.friends).items():
  47. friends.append(friend)
  48.  
  49. # creating dictionaries based on id's is handy too
  50.  
  51. friend_dict = {}
  52. for friend in friends:
  53. friend_dict[friend.id] = friend
  54.  
  55. follower_dict = {}
  56. for follower in followers:
  57. follower_dict[follower.id] = follower
  58.  
  59. # now we find all your "non_friends" - people who don't follow you
  60. # even though you follow them.
  61.  
  62. non_friends = [friend for friend in friends if friend.id not in follower_dict]
  63.  
  64. # double check, since this could be a rather traumatic operation.
  65.  
  66. print "Unfollowing %s non-following users.." % len(non_friends)
  67. print "This will take approximately %s minutes." % (len(non_friends)/60.0)
  68. answer = raw_input("Are you sure? [Y/n]").lower()
  69. if answer and answer[0] != "y":
  70. sys.exit(1)
  71.  
  72. # actually start the removal process. In the event of a failure (thanks,
  73. # twitter), retry once after five seconds. An error on same record again
  74. # is probably more serious issue, exit and error occurs
  75.  
  76. for nf in non_friends:
  77. print "Unfollowing " + str(nf.id).rjust(10)
  78. try:
  79. nf.unfollow()
  80. except:
  81. print " .. failed, sleeping for 5 seconds and then trying again."
  82. time.sleep(5)
  83. nf.unfollow()
  84. print " .. completed, sleeping for 1 second."
  85. time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment