Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cody Bontecou
Cody Bontecou

Posted on

     

Generating Twitter Lists with Python

Generate Twitter Lists with Python

Generating Twitter Lists with Python and Tweepy

The finished project can be seenhere

Paul Graham is an influential man in the tech and startup space.

His Twitter activity keeps me entertained and informed, spanning from discussions aboutair quality significantly impacting life expectancy to interestingdiscussions he has with his kids.

The air pollution discussion this morning motivated me to want to see his feed and all of the interesting things a man like Paul sees. I started to follow the accounts he follows and after about 30 or so clicks I decided to investigate Twitter's API for a way to automate this.

This is where I discoveredTwitter Lists.

Twitter Lists are a great way to create custom feeds

Viewing a List timeline will show you a stream of Tweets from only the accounts on that List.

Twitter Lists are the perfect solution. It gives me the ability to create custom feeds.

If I feel like delving into Paul Grahams feed, it's a click away. Same with Elon Musk or any of the other influential users on Twitter.

Using Tweepy to automatically generate our Twitter Lists

Tweepy is an easy-to-use Python library for accessing the Twitter API.

Tweepy provides built in methods to easily interact with the Twitter API.

In this project, I use the following from Tweepy:

Authenticating with Twitter's API using Tweepy

This tutorial will assume you already have a Twitter developer account. If you do not, I went into detailhere on how to generate the proper authentication keys needed to access Twitter's API.

Tweepy provides the helper function.OAuthHandler that requires you to pass it yourconsumer key andconsumer secret initializing the auth object, you then must call its method.set_access_token() which requires youraccess token andaccess token secret given to you when creating your developer account andgenerating your app.

importtweepyauth=tweepy.OAuthHandler(os.getenv("consumer_key"),os.getenv("consumer_secret"))auth.set_access_token(os.getenv("access_token"),os.getenv("access_token_secret"))
Enter fullscreen modeExit fullscreen mode

Initializing Tweepy's API and Client objects

We can now utilize our auth variable to createAPI andClient objects using the following code:

api=tweepy.API(auth)client=tweepy.Client(bearer_token=os.getenv("bearer_token"),consumer_key=os.getenv("consumer_key"),consumer_secret=os.getenv("consumer_secret"),access_token=os.getenv("access_token"),access_token_secret=os.getenv("access_token_secret"),wait_on_rate_limit=True,)
Enter fullscreen modeExit fullscreen mode

With these objects, we can now access every Twitter endpoint.

Creating a List using Tweepy's API

Tweepy methods used:

  • .create_list(): Takes three parameters: name, mode, and description. Name and description are self-explanatory and mode can either be"public" or"private" to define the visibility status of the List.

The response from a successfully List creation returns the List data. I will be using this list later on, so I decided to extract it's id into the variablelist_id.

list_name="Paul Grahams's Feed"list_description="A list of everyone Paul Graham follows"twitter_list=api.create_list(name=list_name,description=list_description)list_id=twitter_list._json["id"]
Enter fullscreen modeExit fullscreen mode

Getting a User and the accounts they follow

Tweepy methods used:

By passingclient.get_user() a twitter handle - in this case, Paul Graham - I can get all of the public data Twitter provides regarding that user.

I then use that user's data withclient.get_users_following() alongside max_results. The argumentmax_results defines how many user objects Twitter will pass back. In this case, I used the max of 1000. The default is 100.

twitter_handle="paulg"user=client.get_user(username=twitter_handle)followers=client.get_users_following(id=user.data.id,max_results=1000)
Enter fullscreen modeExit fullscreen mode

Automatially adding users to the list

Tweepy methods used:

Now that we have the List we want to populate and the followers we want populate it with, we use theapi.add_list_members() method.

This method takes a list of up to 100 Twitter User ID's. The code snippet below chunks out follower data into sections of 100 Users and generates a list of ids to pass.

We are also passing this method thelist_id that was saved when we created the list.

foriinrange(0,len(followers.data),100):ids=[follower["id"]forfollowerinfollowers.data[i:i+100]]api.add_list_members(list_id=list_id,user_id=ids)
Enter fullscreen modeExit fullscreen mode

Things to keep in mind when working with Twitter's API

Twitter rate limits just about all of their endpoints.Here is Twitter's documentation on what the rate limits are. Keep this is mind when developing Twitter-based applications because you may find yourself with an odd error.

Thank you for reading! I hope you enjoyed it.

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
jessicagarson profile image
Jessica Garson
Jessica Garson is a Python programmer, educator, and artist. She currently works at Elastic as a Developer Advocate. In her spare time, she is on a never-ending quest for the perfect vegan snack.
  • Location
    Brooklyn, NY
  • Work
    Developer Advocate at Elastic
  • Joined

This is awesome!

CollapseExpand
 
codybontecou profile image
Cody Bontecou
is enjoying life as a digital nomad, building web apps one line at a time.
  • Location
    Kona, Hawaii
  • Education
    Bachelors of Science in Computer Science
  • Work
    Senior Fullstack Engineer
  • Joined

I'm glad you like it :)

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

is enjoying life as a digital nomad, building web apps one line at a time.
  • Location
    Kona, Hawaii
  • Education
    Bachelors of Science in Computer Science
  • Work
    Senior Fullstack Engineer
  • Joined

More fromCody Bontecou

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp