18
loading...
This website collects cookies to deliver better user experience
The finished project can be seen here
Viewing a List timeline will show you a stream of Tweets from only the accounts on that List.
Tweepy is an easy-to-use Python library for accessing the Twitter API.
This tutorial will assume you already have a Twitter developer account. If you do not, I went into detail here on how to generate the proper authentication keys needed to access Twitter's API.
.OAuthHandler
that requires you to pass it your consumer key and consumer secret initializing the auth object, you then must call its method .set_access_token()
which requires your access token and access token secret given to you when creating your developer account and generating your app.import tweepy
auth = tweepy.OAuthHandler(os.getenv("consumer_key"), os.getenv("consumer_secret"))
auth.set_access_token(os.getenv("access_token"), os.getenv("access_token_secret"))
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,
)
"public"
or "private"
to define the visibility status of the List.list_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"]
client.get_user()
a twitter handle - in this case, Paul Graham - I can get all of the public data Twitter provides regarding that user.client.get_users_following()
alongside max_results. The argument max_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)
api.add_list_members()
method.list_id
that was saved when we created the list.for i in range(0, len(followers.data), 100):
ids = [follower["id"] for follower in followers.data[i : i + 100]]
api.add_list_members(list_id=list_id, user_id=ids)