Python twitter.TwitterStream() Examples

The following are 3 code examples of twitter.TwitterStream(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module twitter , or try the search function .
Example #1
Source File: twitter_tester.py    From bazarr with GNU General Public License v3.0 6 votes vote down vote up
def run_sample(self):
        """
        Listen to live data from Twitter, and pass on the fully-formed tweets
        to `check_ftfy`. This requires the `twitter` Python package as a
        dependency.
        """
        from twitter import TwitterStream
        from ftfy.streamtester.oauth import get_auth
        twitter_stream = TwitterStream(auth=get_auth())
        iterator = twitter_stream.statuses.sample()
        for tweet in iterator:
            if 'text' in tweet:
                self.check_ftfy(tweet['text'])
                if 'user' in tweet:
                    lang = tweet['user'].get('lang', 'NONE')
                    self.lines_by_lang[lang].append(tweet['text'])
                if self.count % 10000 == 100:
                    self.save_files() 
Example #2
Source File: StreamAPI.py    From Data-Mining-on-Social-Media with MIT License 5 votes vote down vote up
def streamAPI():
    try:
#         q = 'uga'
#        locations = "-83.40,33.93,-83.37,33.94 "
        locations = "-84.56,33.62,-84.20,33.91"
        
#         print >> sys.stderr,'Filtering the public timeline for track = "%s"' %(q)
        
        
        
        twitter_stream = twitter.TwitterStream(auth = twitter_api.auth)
#        print twitter_stream
        
        stream = twitter_stream.statuses.filter(locations = locations)
#        print stream

        InsertStatus(stream)

        


        
    except:
        exc_type, exc_value, exc_traceback = sys.exc_info()
        lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
        print ''.join('!! ' + line for line in lines)  # Log it or whatever here
        logger.info(''.join('!! ' + line for line in lines) ) 
Example #3
Source File: Stream.py    From twitterDataMining with GNU General Public License v3.0 4 votes vote down vote up
def stream_data(self, track=None, follow=None, locations=None, save_to_db=False,
                    collection_name='stream'):
        """
            https://dev.twitter.com/streaming/reference/post/statuses/filter
            The default access level allows up to 400 track keywords, 5,000 follow userids and 25 0.1-360 degree location boxes.

        :param track: str    ;
        :param follow:list str ;
        :param locations: str ;
        :param save_to_db:
        :param collection_name:
        :return: None
        """

        def location_bounding_box(_locations):
            t = _locations.split(',')
            res = ''
            for i in xrange(0, len(t), 2):
                x, y = str(float(t[i]) + 1), str(float(t[i + 1]) + 1)
                res += t[i] + ',' + t[i + 1] + ',' + x + ',' + y + ','
            return res

        kwg = {'language': 'en'}

        if not track and not follow and not locations:
            kwg['track'] = 'twitter'

        if track:
            kwg['track'] = track

        if follow:
            kwg['follow'] = follow

        if locations:
            kwg['locations'] = location_bounding_box(locations)

        print kwg

        twitter_stream = twitter.TwitterStream(auth=self.twitter_api.auth)
        stream = twitter_stream.statuses.filter(**kwg)

        for i, tweet in enumerate(stream):
            if not i % 200 and 'text' in tweet: print i, datetime.datetime.now(), ' ', tweet["text"]
            tweet = dict(tweet)
            if 'id' in tweet:
                self.tweets.append(tweet)

                if self.get_data:
                    self.get_data = False
                    self.conn.send(self.tweets)
                    self.tweets = []

                if save_to_db:
                    self.save_tweets_to_mongodb(tweet, colname=collection_name)