Created
May 26, 2009 00:51
-
-
Save davej/117830 to your computer and use it in GitHub Desktop.
Keeps your followers and friends in sync
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
This script will follow all those who follow you and unfollow those who | |
unfollow you. Basically it keeps your followers and friends in sync. | |
You will need to get a consumer key and consumer secret token to use this | |
script, you can do so by registering a twitter application at http://twitter.com/apps | |
@requirements: Python 2.5+, Tweepy (https://github.com/joshthecoder/tweepy) | |
@author: Dave Jeffery | |
""" | |
import tweepy | |
CONSUMER_KEY = 'xxx' | |
CONSUMER_SECRET = 'xxx' | |
def oauth_login(consumer_key, consumer_secret): | |
"""Authenticate with twitter using OAuth""" | |
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | |
auth_url = auth.get_authorization_url() | |
verify_code = raw_input("Authenticate at %s and then enter you verification code here > " % auth_url) | |
auth.get_access_token(verify_code) | |
return tweepy.API(auth) | |
def get_diff(list1,list2): | |
"""Outputs objects which are in list1 but not list 2""" | |
return list(set(list1).difference(set(list2))) | |
def follow_users(api, follow_list): | |
"""Follows users in follow_list (list of twitter ids)""" | |
for user in follow_list: | |
try: | |
api.create_friendship(user) | |
except: | |
print "error on user: %s" % api.get_user(user).screen_name | |
print "Done\n\n" | |
def unfollow_users(api, unfollow_list): | |
"""Unfollows users in unfollow_list (list of twitter ids)""" | |
for user in unfollow_list: | |
try: | |
api.destroy_friendship(user) | |
except: | |
print "error on user: %s" % api.get_user(user).screen_name | |
print "Done" | |
def friend_sync(api): | |
"""Do the syncing, cha cha cha!""" | |
friend_ids = [] | |
follower_ids = [] | |
print "Fetching Followers..." | |
for follower in tweepy.Cursor(api.followers).items(): | |
follower_ids.append(follower.id) | |
print "Fetching Friends..." | |
for friend in tweepy.Cursor(api.friends).items(): | |
friend_ids.append(friend.id) | |
follow_list = get_diff(follower_ids, friend_ids) | |
unfollow_list = get_diff(friend_ids, follower_ids) | |
# Follow people | |
print "You are about to FOLLOW %s people." % len(follow_list) | |
print "Does this sound ok? There is no undo! Type yes to carry out this action." | |
do_follow = raw_input("> ") | |
if do_follow.lower() == 'yes': | |
print "If errors appear below it may mean that the user has a private profile, if so a follow request will be sent." | |
follow_users(api, follow_list) | |
# Un-follow people | |
print "You are about to UN-FOLLOW %s people." % len(unfollow_list) | |
print "Does this sound ok? There is no undo! Type yes to carry out this action." | |
do_unfollow = raw_input("> ") | |
if do_unfollow.lower() == 'yes': | |
unfollow_users(api, unfollow_list) | |
if __name__ == "__main__": | |
api = oauth_login(CONSUMER_KEY, CONSUMER_SECRET) | |
print "Authenticated as: %s" % api.me().screen_name | |
friend_sync(api) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment