The tweepy Package

The tweepy package provides some useful tweeting capabilities. :bird: :bird:

Reference:

Setup

Create a Twitter Account. Then while logged in to Twitter, visit the Twitter Application Management Console and click "Create New App" to create a new Twitter Application. You might have to first sign up for a Twitter developer account, and click the link in a confirmation email.

After creating a new application, click on the "Keys and Access Tokens" tab, and note the application's "Consumer Key" and "Consumer Secret". Scroll down and generate a new Access Token and note its "Access Token" and "Access Token Secret" values. Store these four values as Environment Variables in a ".env" file in your project's root directory, like:

# the ".env" file...

TWITTER_API_KEY="______________"
TWITTER_API_SECRET="______________"
TWITTER_ACCESS_TOKEN="______________"
TWITTER_ACCESS_TOKEN_SECRET="______________"

Installation

Install tweepy, if necessary:

pip install tweepy

Usage

List your recent tweets:

import os

from dotenv import load_dotenv
import tweepy

load_dotenv()

consumer_key = os.environ.get("TWITTER_API_KEY")
consumer_secret = os.environ.get("TWITTER_API_SECRET")
access_token = os.environ.get("TWITTER_ACCESS_TOKEN")
access_token_secret = os.environ.get("TWITTER_ACCESS_TOKEN_SECRET")

# AUTHENTICATE

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

# INITIALIZE API CLIENT

client = tweepy.API(auth)

# ISSUE REQUESTS

user = client.me() # get information about the currently authenticated user

tweets = client.user_timeline() # get a list of tweets posted by the currently authenticated user

# PARSE RESPONSES

print("---------------------------------------------------------------")
print(f"RECENT TWEETS BY @{user.screen_name} ({user.followers_count} FOLLOWERS / {user.friends_count} FOLLOWING):")
print("---------------------------------------------------------------")

for tweet in tweets:
    created_on = tweet.created_at.strftime("%Y-%m-%d")
    print(" + ", tweet.id_str, created_on, tweet.text)

Last updated