twitter api credentials ize twitter initialize tweepy initialize a list to hold all the tweepy tweets make initial request for most recent tweets 200 is the maximum allowed count save most recent tweets save the id of the oldest tweet less one keep grabbing tweets until there are no tweets left to grab all subsequent requests use the maxid param to prevent duplicates save most recent tweets update the id of the oldest tweet less one transform the tweepy tweets into a 2d array that will populate the csv write the csv pass in the username of the account you want to download twitter api credentials ize twitter initialize tweepy initialize a list to hold all the tweepy tweets make initial request for most recent tweets 200 is the maximum allowed count save most recent tweets save the id of the oldest tweet less one keep grabbing tweets until there are no tweets left to grab all subsequent requests use the max_id param to prevent duplicates save most recent tweets update the id of the oldest tweet less one transform the tweepy tweets into a 2d array that will populate the csv write the csv pass in the username of the account you want to download
import csv

import tweepy


consumer_key = ""
consumer_secret = ""
access_key = ""
access_secret = ""


def get_all_tweets(screen_name: str) -> None:
    
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_key, access_secret)
    api = tweepy.API(auth)

    
    alltweets = []

    
    new_tweets = api.user_timeline(screen_name=screen_name, count=200)

    
    alltweets.extend(new_tweets)

    
    oldest = alltweets[-1].id - 1

    
    while len(new_tweets) > 0:
        print(f"getting tweets before {oldest}")

        
        new_tweets = api.user_timeline(
            screen_name=screen_name, count=200, max_id=oldest
        )

        
        alltweets.extend(new_tweets)

        
        oldest = alltweets[-1].id - 1

        print(f"...{len(alltweets)} tweets downloaded so far")

    
    outtweets = [[tweet.id_str, tweet.created_at, tweet.text] for tweet in alltweets]

    
    with open(f"new_{screen_name}_tweets.csv", "w") as f:
        writer = csv.writer(f)
        writer.writerow(["id", "created_at", "text"])
        writer.writerows(outtweets)


if __name__ == "__main__":
    
    get_all_tweets("FirePing32")