Tweepy: Data flow in X minutes?

I use tweepy to use a general stream of tweets for keywords. It is quite simple and described in several places:

http://runnable.com/Us9rrMiTWf9bAAW3/how-to-stream-data-from-twitter-with-tweepy-for-python

http://adilmoujahid.com/posts/2014/07/twitter-analytics/

Copying the code directly from the second link:

#Import the necessary methods from tweepy library
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream

#Variables that contains the user credentials to access Twitter API 
access_token = "ENTER YOUR ACCESS TOKEN"
access_token_secret = "ENTER YOUR ACCESS TOKEN SECRET"
consumer_key = "ENTER YOUR API KEY"
consumer_secret = "ENTER YOUR API SECRET"


#This is a basic listener that just prints received tweets to stdout.
class StdOutListener(StreamListener):

    def on_data(self, data):
        print data
        return True

    def on_error(self, status):
        print status


if __name__ == '__main__':

    #This handles Twitter authetification and the connection to Twitter Streaming API
    l = StdOutListener()
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    stream = Stream(auth, l)

    #This line filter Twitter Streams to capture data by the keywords: 'python', 'javascript', 'ruby'
    stream.filter(track=['python', 'javascript', 'ruby'])

What I cannot understand is how can I pass this data into a python variable? Instead of typing on the screen ... I work on an ipython laptop and want to capture a stream in some variable fooafter streaming for a minute or so. Also, how do I get a thread to timeout? Thus, it works indefinitely.

Connected:

Using tweepy to access Twitter Streaming API

+3
1

, @Adil Moujahid , 3 . , :

a) , , . . . :

stream_all = Stream(auth, l)

, -, :

stream_SFO = stream_all.filter(locations=[-122.75,36.8,-121.75,37.8])  

, .

(b) :

tweet_iter = stream_SFO.filter(track=['python', 'javascript', 'ruby']) 

(c) :

with open('file_name.json', 'w') as f:
        json.dump(tweet_iter,f,indent=1)

. , . , .

, .

+2

Source: https://habr.com/ru/post/1614287/


All Articles