Note that there are some explanatory texts on larger screens.

plurals
  1. POTweepy public stream filter by a changing variable
    text
    copied!<p>I was working with the Tweepy library for python to access the public twitter stream and ran into a problem where once the stream is running, it doesn't stop. Now, that makes sense for what it does, but I wanted it to start filtering with an empty list of user IDs and after a while, userIDs are added to the list after someone tweets a particular track word, so that once they tweet a word, the tracker starts tracking all their tweets. The problem is that once the stream is started with the initial filter options, changing the variables doesn't affect the filter; it just keeps on using the initial arguments.</p> <pre><code>userIDs = [] trackWords = ["#Obama"] def stream(): s = Stream(auth, StreamListener()) s.filter(follow = userIDs, track = trackWords) </code></pre> <p>I was able to get around this earlier by recalling the stream definition again after a new keyword is added, but I have multiple streams searching and I put them in separate threads so they can all run simultaneously. I can't figure out how to refresh the threads, so trying to refresh the filter without recalling the definition seems easier.</p> <p>I'm fairly new to programming, so maybe this is a fundamental concept I don't know yet, but hopefully there's an easy trick to get it to refresh.</p> <p>Here's all my relevant code if that helps anyone. The above was just a quick thing to help show what I'm talking about:</p> <pre><code>userIDs = [] userNames = [] account = ['@DMS_423'] publicKeyWords = ['the','be','to','of','and','are','is','were','was'] class AStreamListener(StreamListener): def on_status(self, status): if status.author.screen_name not in userNames: userNames.append(str(status.author.screen_name)) userIDs.append(str(api.get_user(str(status.author.screen_name)).id)) print status.author.screen_name, "has joined the game." def uStream(): s = Stream(auth, StreamListener()) s.filter(follow = userIDs) def pStream(): ps = PStream(pAuth, PStreamListener()) ps.filter(track = publicKeyWords) def aStream(): adds = Stream(auth, AStreamListener()) adds.filter(track = account) t1 = Thread(target = aStream) t2 = Thread(target = uStream) t3 = Thread(target = pStream) def run(): t1.start() t2.start() t3.start() run() </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload