Skip to content

Instantly share code, notes, and snippets.

@hackingbutlegal
Created July 1, 2019 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hackingbutlegal/49ee4157dca8ef647393f3b949dddc91 to your computer and use it in GitHub Desktop.
Save hackingbutlegal/49ee4157dca8ef647393f3b949dddc91 to your computer and use it in GitHub Desktop.
Bulk Add Twitter Users to a List
#!/usr/bin/env python3
import tweepy
import pandas as pd
# Config
consumer_key = 'xxx'
consumer_secret = 'xxx'
access_token = 'xxx'
access_token_secret = 'xxx'
list_name = 'blockList'
# login in twitter api
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
# Create the list
twitter_list = api.create_list(list_name)
me = api.me()
df = pd.read_csv('./users.csv')
for index, twitter_account in enumerate(df['Twitter']):
print('\rAdding user: {0}/{1}'.format(index, df.shape[0]), end='')
try:
user = api.get_user(twitter_account)
a = api.add_list_member(user_id=user.id_str, slug=list_name, owner_screen_name=me.screen_name)
except tweepy.error.TweepError as e:
print('\nError: {0}'.format(twitter_account))
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment