Duplicating a twitter feed
Are you an infovore? Curious what other infovores’ information diet looks like? The simple python code in this post allows you to automatically create a twitter list of all accounts that a particular person of interest follows, such as @tylercowen.
Running this code requires that you have twitter API access, or you can comment an account at the bottom and I can create that list for you.
We need the tweepy package for interacting with the Twitter API, and I use the yaml package to load my API information from a file titled “api-config.yaml”.
import tweepy
import yaml
config_file = 'api-config.yaml'
with open(config_file, "r") as f:
config = yaml.load(f, Loader=yaml.FullLoader)
print('Config loaded from '+config_file)
Config loaded from api-config.yaml
Using the loaded configuration information we authenticate with the API.
auth = tweepy.OAuthHandler(config["api_key"],config["api_secrets"])
auth.set_access_token(config["access_token"],config["access_secret"])
api = tweepy.API(auth, wait_on_rate_limit = True)
client = tweepy.Client(
bearer_token=config["bearer_token"],
consumer_key=config["api_key"],
consumer_secret=config["api_secrets"],
access_token=config["access_token"],
access_token_secret=config["access_secret"],
wait_on_rate_limit=True,
)
if api.verify_credentials() == False:
print("The user credentials are invalid.")
else:
print("The user credentials are valid.")
The user credentials are valid.
We enter our screen name and the target’s screen name.
your_screen_name = 'YourUsernameHere' # Your own account username
target_screen_name = 'tylercowen' # The username you want to build a list from
list_name = "@"+target_screen_name+"'s Feed"
list_description = "A list of everyone @" + target_screen_name + \
" follows. This list may not be complete yet due to Twitter's API rate limits." +\
" For an explanation of the code that generated this list, \or to request a" + \
" list, see jbreffle.github.io"
print("List is named: ", list_name)
print("List description is: ", list_description)
List is named: @tylercowen's Feed
List description is: A list of everyone @tylercowen follows. This list may not be complete yet due to Twitter's API rate limits. For an explanation of the code that generated this list, or to request a list, see jbreffle.github.io
Iterate through your own lists and check if the list already exists. Create it if it doesn’t.
user = api.get_user(screen_name=your_screen_name)
user_lists = client.get_owned_lists(user.id)
list_exists_flag = False
for ith_list in range(len(user_lists.data)):
listName = user_lists.data[ith_list]
# print("ith_list Name:",listName)
if listName.name == list_name:
list_id = listName.id
if list_exists_flag==False:
list_exists_flag = True
elif list_exists_flag==True:
print("Error: List with same name already exists twice")
if not list_exists_flag:
twitter_list = api.create_list(name=list_name, description=list_description)
list_id = twitter_list._json["id"]
else:
print(" List already exists, ID: "+list_id)
List already exists, ID: 1622739317704515584
Get the list of people the target follows.
user = client.get_user(username=target_screen_name)
following = client.get_users_following(id=user.data.id, max_results=1000)
print('@'+target_screen_name, 'has', len(following.data), 'friends')
@tylercowen has 484 friends
Get the current members of the list.
list_members = []
for page in tweepy.Cursor(api.get_list_members, list_id = list_id).items():
list_members.append(page)
list_members_ids = []
for i in range(len(list_members)):
list_members_ids.append(list_members[i].id)
print("List currently has,", len(list_members_ids), " members")
List currently has 17 members
Increment through the list of everyone the target follows and add them to the list if they aren’t already in it. The Twitter API severaly limits the number of people you can add to the list per day to an unspecified and varying number, so the loop exits once the limit is reached.
loop_break_flag=False
for i in range(len(followers.data)):
if following.data[i].id in list_members_ids:
pass
# print(followers.data[i].username, "Already in list")
else:
if not api.get_user(user_id=following.data[i].id).protected:
try:
print(following.data[i].username, "Not in list, adding...")
api.add_list_member(list_id=list_id, user_id=following.data[i].id)
except:
print("Rate limit reached when trying to add", \
following.data[i].username, "to list")
loop_break_flag=True
if loop_break_flag:
break
list_members = []
for page in tweepy.Cursor(api.get_list_members, list_id = list_id).items():
list_members.append(page)
print("List now has,", len(list_members), " members")
List currently has 17 members