0

I created this bot with tweepy and python, basically i can retweet an like the most recent tweets that contain a certain keyword. I want to get the status of a tweet that has that keyword so that i know if i already retweeted it or not.

import time

import tweepy
import config

# Search/ Like/ Retweet


def get_client():
    client = tweepy.Client(bearer_token=config.BEARER_TOKEN,
                           consumer_key=config.CONSUMER_KEY,
                           consumer_secret=config.CONSUMER_SECRET,
                           access_token=config.ACCESS_TOKEN,
                           access_token_secret=config.ACCESS_TOKEN_SECRET, )
    return client


def search_tweets(query):
    client = get_client()

    tweets = client.search_recent_tweets(query=query, max_results=20)

    tweet_data = tweets.data

    results = []

    if tweet_data is not None and len(tweet_data) > 0:
        for tweet in tweet_data:
            obj = {'id': tweet.id, 'text': tweet.text}
            results.append(obj)
    else:
        return 'There are no tweets with that keyword!'

    return results


client = get_client()

tweets = search_tweets('#vinu')

for tweet in tweets:
    client.retweet(tweet["id"])
    client.like(tweet['id'])
    time.sleep(2)

This is the code. I want to create an if statement to check with api v2 if i already retweeted it , and if so , to continue to the next item in the loop. I know that i can use api.get_status with api v1 , but i dont find how to do it with v2. please help me out.

 if tweet_data is not None and len(tweet_data) > 0:
        for tweet in tweet_data:
            status = tweepy.api(client.access_token).get_status(tweet.id)
            if status.retweeted:
                continue
            else:
                obj = {'id': tweet.id, 'text': tweet.text}
                results.append(obj)
    else:
        return ''

    return results

This should work in v1 , please help me do the same thing in v2. Thanks!

bytro
  • 61
  • 6

1 Answers1

1

For Tweepy API v2 you can use the get_retweeters() method for each tweet, then compare your user id with the retrieved retweeters' id's.

if tweet_data is not None and len(tweet_data) > 0:
        for tweet in tweet_data:
            status = client.get_retweeters(tweet.id, max_results=3)
            for stat in status.data:
                if stat.id == YOUR_ID_HERE:
                    continue
                else:
                    obj = {'id': tweet.id, 'text': tweet.text}
                    results.append(obj)
    else:
        return 'There are no tweets with that keyword!'

    return results

You can change the max_results to whatever limit you'd like as long as you're managing your rates correctly. If you're getting None results try changing the tweet.id to a static id that has retweets and test just that one, this is due to many tweets not having any retweets.

You can find an easy way to find your twitter id using your twitter username here

bytro
  • 61
  • 6