Skip to content

Instantly share code, notes, and snippets.

@basilleaf
Last active March 3, 2018 19:13
Show Gist options
  • Save basilleaf/83ef1cad57a926d637d45ec0a369cdd3 to your computer and use it in GitHub Desktop.
Save basilleaf/83ef1cad57a926d637d45ec0a369cdd3 to your computer and use it in GitHub Desktop.
twitter maintenance
"""
snippet for twitter bots, follow back all followers, unfollow unfollowers (with exceptions)
"""
import tweepy
from secrets import CONSUMER_KEY, CONSUMER_SECRET, ACCESS_KEY, ACCESS_SECRET
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
followers = []
for block in tweepy.Cursor(api.followers_ids, SCREEN_NAME).items():
followers += [block]
friends = []
for block in tweepy.Cursor(api.friends_ids, SCREEN_NAME).items():
friends += [block]
to_follow = list(set(followers).difference(friends))
# do follow backs
for f in to_follow:
try:
api.create_friendship(f)
print('followed: ' + api.get_user(f).screen_name)
except tweepy.error.TweepError, e:
if 'unable to follow more' in e.message[0]['message']:
print(e.message[0]['message'])
break
if 'already requested' in e.message[0]['message']:
pass
# unfollow unfollowers
exceptions = ['Kaleidopix']
for f in friends:
if f not in followers:
if api.get_user(f).screen_name not in exceptions:
print "Unfollowing {0}".format(api.get_user(f).screen_name)
api.destroy_friendship(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment