Repository: Mintplex-Labs/anything-llm
File: collector/scripts/twitter.py (Python)

Code before:
"""
Tweepy implementation of twitter reader. Requires the 4 twitter keys to operate.
"""

import tweepy
import os, time
import pandas as pd
import json
from .utils import tokenize, ada_v2_cost

def twitter():
    #get user and number of tweets to read
    username = input("user timeline to read from (blank to ignore): ")
    searchQuery = input("Search term, or leave blank to get user tweets (blank to ignore): ")
    tweetCount = input("Gather the last number of tweets: ")
    
    #read your API kes to call the api.
    consumer_key = os.environ["TW_CONSUMER_KEY"]
    consumer_secret = os.environ["TW_CONSUMER_SECRET"]
    access_token = os.environ["TW_ACCESS_TOKEN"]
    access_token_secret = os.environ["TW_ACCESS_TOKEN_SECRET"]

    # Pass in our twitter API authentication key
    auth = tweepy.OAuth1UserHandler(
        consumer_key, consumer_secret, access_token, access_token_secret
    )

    # Instantiate the tweepy API
    api = tweepy.API(auth, wait_on_rate_limit=True)

    try:
        if (searchQuery == ''):
            tweets = api.user_timeline(screen_name=username, tweet_mode = 'extended', count=tweetCount)
        else:
            tweets = api.search_tweets(q=searchQuery, tweet_mode = 'extended', count=tweetCount)

        # Pulling Some attributes from the tweet
        attributes_container = [
            [tweet.id, tweet.user.screen_name, tweet.created_at, tweet.favorite_count, tweet.source, tweet.full_text]
            for tweet in tweets
        ]
        

Review comment (security):
```suggestion
    #read your API keys to call the api.
```

We should check the existence of the keys before invocation and throw an error if any are missing - right now we just try to do the OAuth and hope for the best - if anything the `auth` and `api` should be inside of the try-catch block as well so at least that gets handled.

Code after:
"""
Tweepy implementation of twitter reader. Requires the 4 twitter keys to operate.
"""

import tweepy
import os, time
import pandas as pd
import json
from .utils import tokenize, ada_v2_cost

def twitter():
    #get user and number of tweets to read
    username = input("user timeline to read from (blank to ignore): ")
    searchQuery = input("Search term, or leave blank to get user tweets (blank to ignore): ")
    tweetCount = input("Gather the last number of tweets: ")
    
    # Read your API keys to call the API.
    consumer_key = os.environ.get("TW_CONSUMER_KEY")
    consumer_secret = os.environ.get("TW_CONSUMER_SECRET")
    access_token = os.environ.get("TW_ACCESS_TOKEN")
    access_token_secret = os.environ.get("TW_ACCESS_TOKEN_SECRET")

    # Check if any of the required environment variables is missing.
    if not consumer_key or not consumer_secret or not access_token or not access_token_secret:
        raise EnvironmentError("One of the twitter API environment variables are missing.")

    # Pass in our twitter API authentication key
    auth = tweepy.OAuth1UserHandler(
        consumer_key, consumer_secret, access_token, access_token_secret
    )

    # Instantiate the tweepy API
    api = tweepy.API(auth, wait_on_rate_limit=True)

    try:
        if (searchQuery == ''):
            tweets = api.user_timeline(screen_name=username, tweet_mode = 'extended', count=tweetCount)
        else:
            tweets = api.search_tweets(q=searchQuery, tweet_mode = 'extended', count=tweetCount)

        # Pulling Some attributes from the tweet
        attributes_container = [