Search code examples
pythonapitexttwittertweepy

Filter tweets by text with user_timeline in Tweepy


I am trying to filter the results I get from a certain account I get by a keyword with "api.user_timeline", so basically I want the program to ignore the tweets from that account that don't have that keyword in their tweet but I can't seem to find a solution for it.

import time

for tweet in tweepy.Cursor(api.user_timeline, since='2017-12-27',
                           screen_name='example').items(2):
    try:
        if not tweet.retweeted:
            tweet.retweet()
            m = "@example This is an example."
            t = api.update_status(status=m, in_reply_to_status_id=tweet.id)
            print("Example, Working...")
            time.sleep(5)
    except tweepy.TweepError as e:
        print(e.reason)
        time.sleep(5)
        break
    except StopIteration:
        break

Would it be: (?)

if not tweet.retweeted:
    if tweet.text == "keyword":
       doThis()

I really have no idea, so any help is highly appreciated!


Solution

  • if not tweet.retweeted:
        if "keyword" in tweet.text:
            doThis()
    

    You probably will also want to make this case-insensitive with string.lower().