Giter Site home page Giter Site logo

A way to stop the stream about twitterapi HOT 9 CLOSED

geduldig avatar geduldig commented on August 23, 2024
A way to stop the stream

from twitterapi.

Comments (9)

geduldig avatar geduldig commented on August 23, 2024

If you use r's iterator I don't see the necessity of a stop function. Maybe you have something else in mind. Can you show me a code example of how you would use r.stop() if there were such a method?

from twitterapi.

geduldig avatar geduldig commented on August 23, 2024
r = api.request('statuses/filter', {'track':TRACK_TERM})
for item in r.get_iterator():
    if stop(item):
        break
    process_tweet(item)

def stop(item):
    if 'code' in item and item['code'] is 88:
        print "Rate limit exceeded."
        return True
    else:
        return False

from twitterapi.

filipjohansson avatar filipjohansson commented on August 23, 2024

The reason I want to stop the api.request is because I want to be able to change the search term on the fly, maybe I'm trying to solve that problem the wrong way?

from twitterapi.

geduldig avatar geduldig commented on August 23, 2024

It really depends on what you want to do. For example, you could get a tweet and change the request and get another tweet this way:

r = api.request('statuses/filter', {'track':TRACK_TERM1})
tweet = next(r.get_iterator())
print tweet['text']
r = api.request('statuses/filter', {'track':TRACK_TERM2})
tweet = next(r.get_iterator())
print tweet['text']

You can see from this example, which uses next, and the previous example, which uses a for loop, there is no starting and stopping of the stream. The request method returns an object which represents the stream you requested. The iterator lets you grab one item from that stream at a time. You can grab as many items as you like, and you can stop grabbing items whenever you like.

I hope that clarifies something.

from twitterapi.

filipjohansson avatar filipjohansson commented on August 23, 2024

If I understand your example it allows me to search for many things at the same time?
What I would like to do is to stop tracking the old one and start tracking the new one. Hard coding the amount of times I can change the search term is not an option either. Best would be if I could just replace the old search term with the new one.

from twitterapi.

geduldig avatar geduldig commented on August 23, 2024

I think you are misunderstanding. The last example does not do a simultaneous search. It does exactly what you said:

- tracks one term
- replaces the the term with a new one
- tracks a second term

from twitterapi.

filipjohansson avatar filipjohansson commented on August 23, 2024

Yes, I am pretty sure there is something here I do not understand.

My code first looks like this:

r = api.request('statuses/filter', {'track': track_term})
for item in r:
    print(item['text'] if 'text' in item else item)

That works fine. But later in the script I change the value of track_term and replace r. Like this:

track_term = 'pizza'
r = api.request('statuses/filter', {'track': track_term})

The old for loop still outputs the results from the first track_term.

from twitterapi.

geduldig avatar geduldig commented on August 23, 2024

That sounds right, because the for loop never ends. And, as you said in your first post, you need a way to stop the stream. Maybe what you do not realize is that even if you could call r.stop() it would not be effective for you. You probably would call it just before you wanted to set the new search term; however, your program would still be in the for loop and never reach r.stop(). This is why there is no point in having a stop method.

What I suggest is this. First, ask yourself what is the condition you want the for loop to stop. You have not specified that in this thread. Second, use that condition in the stop function example I provided in my second post.

from twitterapi.

filipjohansson avatar filipjohansson commented on August 23, 2024

It feels like I am looking at this in the wrong way. What you say makes sense, but since I do not have that much experience with Python I am having a hard time implementing it.
I will close this thread now but I am more than happy with all the help you have given me. Thank you very much and keep up the good work!

from twitterapi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.