user971956
user971956

Reputation: 3208

How to track more than a single term in Tweepy?

I am using Tweepy and would like to track two separate terms, 'wordA' and 'wordB' (meaning each tweet will contain either) but I also want to store their results in separate structures. Is it possible to have two separate stream listeners on the same auth object? any code examples demonstrating how to do this would be appreciated. Thanks

Upvotes: 0

Views: 968

Answers (1)

Toby
Toby

Reputation: 131

You're only allowed a single stream per user, so you'll have to split them out after the data has been received.

I tend to do this in something like the following:

import tweepy
from tweepy.utils import import_simplejson
json = import_simplejson()
tracklist1=[wordA, wordAA]
tracklist2=[wordB, wordBB]

class CustomStreamListener(tweepy.StreamListener):

    def on_data(self, data):
        if 'in_reply_to_status_id' in data:
            temp=json.loads(data)
            words = [word.lower().strip('!,.:?"') for word in temp['text'].split()]
            if set(words) & set(tracklist1):
                print 'match A'
            elif set(words) & set(tracklist):
                print 'match B'
            else:
                print 'no match found'

Works well enough for me, and the use of lists for tracklist1 and tracklist2 allow you to build a more complex search for each topic you're after. You'll always get some that don't match as twitter matches against user names as well as text on the streaming API.

To do this properly you'll probably want to filter out everything that isn't alphanumerics instead of just stripping the most common punctuation as I've done in the example above.

Upvotes: 1

Related Questions