0

I have a set number of accounts that I need to monitor and I am trying to create a script in Python using Tweepy that can scrape all Tweets from these accounts in the past hour and compile them into a list.

The code runs without error, but it says that none of the accounts have tweeted even though I know this not to be true.

Below is the code that I have used. I have used the correct bearer_token and account names in my script and have tried to get ChatGPT to figure out the problem already but to no avail (it just keeps asking me to make sure the accounts have actually tweeted).

I have also tried changing the timeframe to significantly larger than 3600 to check if it was a timezone issue but it still doesn't seem to be picking up any tweets.

The code runs without error so I am very confused at what's going wrong.

Would be very grateful if anyone can spot what's going wrong with the code, thank you!

import requests
import json
import time

# Twitter API v2 endpoint to get tweets from a user's profile
profile_endpoint = "https://api.twitter.com/2/users/{}/tweets"

# Twitter API v2 endpoint to get tweets from a user's timeline
timeline_endpoint = "https://api.twitter.com/2/users/{}/tweets?tweet.fields=created_at&max_results=100"

# List of Twitter usernames to monitor (without duplicates)
usernames = list(set(["Account A", "Account B", "Account C"]))

# Twitter API v2 Bearer token
bearer_token = "bearer_token"

# Loop through each username
for username in usernames:
    # Construct the API endpoints for the user's profile and timeline
    profile_url = profile_endpoint.format(username)
    timeline_url = timeline_endpoint.format(username)

    # Set the headers and parameters for the API request
    headers = {"Authorization": f"Bearer {bearer_token}"}
    params = {"max_results": 100, "tweet.fields": "created_at"}

    # Send the API request and get the response for the user's profile
    response = requests.get(profile_url, headers=headers, params=params)

    # Parse the JSON response for the user's profile
    try:
        data = json.loads(response.text)["data"]
    except KeyError:
        data = []

    # Send the API request and get the response for the user's timeline
    response = requests.get(timeline_url, headers=headers, params=params)

    # Parse the JSON response for the user's timeline
    try:
        data += json.loads(response.text)["data"]
    except KeyError:
        pass

    # Print the total number of tweets found for the user
    print(f"Total tweets for {username}: {len(data)}")

    # Loop through each tweet
    for tweet in data:
        # Print the tweet text and created_at timestamp for debugging purposes
        print(f"{username}: {tweet['text']}")
        print(f"Created at: {tweet['created_at']}")

        # Check if the tweet was posted in the last hour
        created_at = time.strptime(tweet["created_at"], "%Y-%m-%dT%H:%M:%S.%fZ")
        current_time = time.gmtime()
        time_difference = time.mktime(current_time) - time.mktime(created_at)
        if time_difference < 3600:
            print("Tweet posted within the last hour.")
        else:
            print("Tweet not posted within the last hour.")

0 Answers0