A.S.J
A.S.J

Reputation: 637

Twitter Search API using regex to filter tweets: "no tweets found"

I'm currently trying to make a twitter bot that is supposed to reply to one tweet, which it filters using regex, and reply to it. The relevant code looks as follows:

questionRegex = re.compile(regex here)
def searchWeatherRequest(weatherReport) :
    for tweet in tweepy.Cursor(api.search,
                                q=questionRegex,
                                lang="en",
                                since=today).items(1):
        try:
            tweetId = tweet.user.id
            username = tweet.user.screen_name
            print ('\Tweet by: @' + username)
            tweet.retweet()
            api.update_status("@" + username + "Today's weather" + weatherReport)
            print (tweet.text)
        except tweepy.TweepError as e:
            print (e.reason)
        except StopIteration:
            break

    time.sleep(3600)

But whenever I run the code, I receive the message "no tweets found" (even after posting a tweet that would match the regex, so I know that it's not just because there are simply no tweets that would match it). I also tried filtering the tweets in steps (first, I filter tweets using just one word, and then I filter those tweets using regex) but this did not work either. Does anyone know what I'm doing wrong. I read multiple articles and questions about this but none of the solutions seemed to work. I read one question you couldn't filter tweets using regex but other answers suggested otherwise. Is it true that you simply can't use regex, or am I encountering a simple coding error?

Upvotes: 0

Views: 816

Answers (1)

Generic Snake
Generic Snake

Reputation: 575

Unfortunately regexes won't work here. This is because the q= is expecting a string to come through and thus won't interperet the regex you're passing, instead I believe it'd either just error or take the re.compile(regex here) as a string itself, which of course, isn't likely to turn up many - if any - results.

So it looks like your current method isn't going to work. A workaround could be using Twitter's standard operators. You could build up strings using filter operations that when passed to the Cursor, essentially act the same way as your regex did. Keep in mind though that there are character limits and overly complicated queries may also be rejected. You can find details on that in the search tweets docs.

Another option would be to have a fairly general search, and then use your regex from there to filter the results. The answerer to a fairly similar question as yours shares some articles here.

Hope that helps and gets you on the right path.

Upvotes: 2

Related Questions