Skip to content

Instantly share code, notes, and snippets.

@hiway
Created November 12, 2012 04:41
Show Gist options
  • Save hiway/4057558 to your computer and use it in GitHub Desktop.
Save hiway/4057558 to your computer and use it in GitHub Desktop.
Utility to trim down your twitter following by moving users to lists and unfollowing automatically.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
try:
import tweepy
except:
raise Exception('Install tweepy library first! (pip install tweepy)')
try:
from twitter_auth_settings import *
except:
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_KEY = ''
ACCESS_SECRET = ''
if not ACCESS_KEY:
raise Exception('Set up credentials from dev.twitter.com '
'in the script.')
if len(sys.argv) > 2:
slug = sys.argv[1]
usernames = sys.argv[2:]
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
me = api.me()
for username in usernames:
try:
user = api.get_user('%s' % username)
except:
print 'User %s not found!' % username
continue
if api.add_list_member(slug=slug, id=user.id):
# if user was successfully added to list,
# check if we're following the user:
if api.exists_friendship(me.id, user.id):
# Unfollow
api.destroy_friendship(user.id)
print "Now following %s via %s" % (user.screen_name, slug)
else:
print ("Usage: follow_via_list.py [list-slug] "
"[username1, username2, ...]")
@hiway
Copy link
Author

hiway commented Nov 12, 2012

Usage notes:

  • Create lists in twitter before you pass a 'slug' to this utility.
  • Install 'tweepy' library via pip.
  • You'll need twitter API keys, visit dev.twitter.com, create an app, also create user access keys - and use them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment