Question

I'm very new to twitter api, please help me understand the difference between two things. As far as I understand I can get real-time tweets by using tweepy for example :

hashtag = ['justinbieber']
class CustomStreamListener(tweepy.StreamListener):

    def on_status(self, status):
        try:
            data = status.__getstate__()
            print data
            output.write("%s\n "% data)
        except Exception, e:
            print >> sys.stderr, 'Encountered Exception:', e
            pass


    def on_error(self, status_code):
        print >> sys.stderr, 'Encountered error with status code:', status_code
        return True # Don't kill the stream

    def on_timeout(self):
        print >> sys.stderr, 'Timeout...'
        return True # Don't kill the stream

class Twitter():

    def __init__(self):

        consumer_key=
        consumer_secret=
        access_key = 
        access_secret = 

        self.auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
        self.auth.set_access_token(access_key, access_secret)
        self.api = tweepy.API(self.auth)


    def start(self):
        l = CustomStreamListener()
        stream = tweepy.streaming.Stream(self.auth,l, secure=True)
        stream.filter(follow=None, track=hashtag)

if __name__ == "__main__":
    Twitter().start()

But what exactly I'm getting if I use python-twitter's api.GetSearch()? For example:

def t_auth(self):
    consumer_key=
    consumer_secret=
    access_key = 
    access_secret = 

    self.api = twitter.Api(consumer_key, consumer_secret ,access_key, access_secret)
    self.api.VerifyCredentials()

    return self.api

self.tweets = []
self.tweets.extend(self.api.GetSearch(self.hashtag, per_page=10))

Imagine that I put last line in an infinite while loop, will I get the same result as in the first example? What's the difference between those two?

Was it helpful?

Solution

Here's my insight.

The first example with tweepy stream is a use case of twitter streaming API. The second example using python-twitter is a use case of twitter search API.

So, I understand this question as: Should I use twitter regular search API or Streaming API?

It depends, but, long story short, if you want to see the real real-time picture - you should use streaming.

I don't have enough experience to explain you props and cons of both approaches, so I'll just refer you:

Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top