当前位置: 首页>>代码示例>>Python>>正文


Python Stream.sample方法代码示例

本文整理汇总了Python中tweepy.streaming.Stream.sample方法的典型用法代码示例。如果您正苦于以下问题:Python Stream.sample方法的具体用法?Python Stream.sample怎么用?Python Stream.sample使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tweepy.streaming.Stream的用法示例。


在下文中一共展示了Stream.sample方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TweepyStreamTests

# 需要导入模块: from tweepy.streaming import Stream [as 别名]
# 或者: from tweepy.streaming.Stream import sample [as 别名]
class TweepyStreamTests(unittest.TestCase):
    def setUp(self):
        self.auth = create_auth()
        self.listener = MockStreamListener(self)
        self.stream = Stream(self.auth, self.listener, timeout=3.0)

    def tearDown(self):
        self.stream.disconnect()

    def test_userstream(self):
        # Generate random tweet which should show up in the stream.
        def on_connect():
            API(self.auth).update_status(mock_tweet())

        self.listener.connect_cb = on_connect
        self.listener.status_stop_count = 1
        self.stream.userstream()
        self.assertEqual(self.listener.status_count, 1)

    def test_sample(self):
        self.listener.status_stop_count = 10
        self.stream.sample()
        self.assertEquals(self.listener.status_count,
                          self.listener.status_stop_count)

    def test_filter_track(self):
        self.listener.status_stop_count = 5
        phrases = ['twitter']
        self.stream.filter(track=phrases)
        self.assertEquals(self.listener.status_count,
                          self.listener.status_stop_count)
开发者ID:dowens,项目名称:tweepy,代码行数:33,代码来源:test_streaming.py

示例2: TweepyStreamTests

# 需要导入模块: from tweepy.streaming import Stream [as 别名]
# 或者: from tweepy.streaming.Stream import sample [as 别名]
class TweepyStreamTests(unittest.TestCase):
    def setUp(self):
        self.auth = create_auth()
        self.listener = MockStreamListener(self)
        self.stream = Stream(self.auth, self.listener, timeout=3.0)

    def tearDown(self):
        self.stream.disconnect()

    def test_userstream(self):
        # Generate random tweet which should show up in the stream.
        def on_connect():
            API(self.auth).update_status(mock_tweet())

        self.listener.connect_cb = on_connect
        self.listener.status_stop_count = 1
        self.stream.userstream()
        self.assertEqual(self.listener.status_count, 1)

    def test_userstream_with_params(self):
        # Generate random tweet which should show up in the stream.
        def on_connect():
            API(self.auth).update_status(mock_tweet())

        self.listener.connect_cb = on_connect
        self.listener.status_stop_count = 1
        self.stream.userstream(_with='user', replies='all', stall_warnings=True)
        self.assertEqual(self.listener.status_count, 1)

    def test_sample(self):
        self.listener.status_stop_count = 10
        self.stream.sample()
        self.assertEquals(self.listener.status_count,
                          self.listener.status_stop_count)

    def test_filter_track(self):
        self.listener.status_stop_count = 5
        phrases = ['twitter']
        self.stream.filter(track=phrases)
        self.assertEquals(self.listener.status_count,
                          self.listener.status_stop_count)

    def test_track_encoding(self):
        s = Stream(None, None)
        s._start = lambda async: None
        s.filter(track=[u'Caf\xe9'])

        # Should be UTF-8 encoded
        self.assertEqual(u'Caf\xe9'.encode('utf8'), s.parameters['track'])

    def test_follow_encoding(self):
        s = Stream(None, None)
        s._start = lambda async: None
        s.filter(follow=[u'Caf\xe9'])

        # Should be UTF-8 encoded
        self.assertEqual(u'Caf\xe9'.encode('utf8'), s.parameters['follow'])
开发者ID:bakagh,项目名称:tweepy,代码行数:59,代码来源:test_streaming.py

示例3: run

# 需要导入模块: from tweepy.streaming import Stream [as 别名]
# 或者: from tweepy.streaming.Stream import sample [as 别名]
def run():
    # Authenticates with Twitter
    print('Auth')
    auth = authenticate()
    api = connect_to_api(auth)

    print('Handling')
    listener = TweepyListener(api)
    listener.add_handler(handler=PrintTweetHandler())

    print('Streaming')
    stream = Stream(auth=auth, listener=listener)

    stream.sample()
开发者ID:anhhuy1605,项目名称:test_rtd,代码行数:16,代码来源:tweepy_integrations.py

示例4: TweepyStreamTests

# 需要导入模块: from tweepy.streaming import Stream [as 别名]
# 或者: from tweepy.streaming.Stream import sample [as 别名]
class TweepyStreamTests(unittest.TestCase):
    def setUp(self):
        self.auth = create_auth()
        self.listener = MockStreamListener(self)
        self.stream = Stream(self.auth, self.listener, timeout=3.0)

    def tearDown(self):
        self.stream.disconnect()

    def test_userstream(self):
        # Generate random tweet which should show up in the stream.
        def on_connect():
            API(self.auth).update_status(mock_tweet())

        self.listener.connect_cb = on_connect
        self.listener.status_stop_count = 1
        self.stream.userstream()
        self.assertEqual(self.listener.status_count, 1)

    def test_sample(self):
        self.listener.status_stop_count = 10
        self.stream.sample()
        self.assertEquals(self.listener.status_count,
                          self.listener.status_stop_count)

    def test_filter_track(self):
        self.listener.status_stop_count = 5
        phrases = ['twitter']
        self.stream.filter(track=phrases)
        self.assertEquals(self.listener.status_count,
                          self.listener.status_stop_count)

    def test_on_data(self):        
        test_wrong_data = [
            '{"disc', # this is actual data read from twitter
            '600',    # this is actual data read from twitter
            '41\n',   # this is actual data read from twitter
            'obviously non-json',
            '"json but not dict"',
			'{"json dict":"but not a twitter message"}'
        ]        
        for raw_data in test_wrong_data: 
            # should log errors but not raise / not return False
            self.assertEquals(self.listener.on_data(raw_data), None)
            self.assertEquals(self.listener.status_count, 0)
开发者ID:mandelcode,项目名称:tweepy,代码行数:47,代码来源:test_streaming.py

示例5: StreamsTestsPlain

# 需要导入模块: from tweepy.streaming import Stream [as 别名]
# 或者: from tweepy.streaming.Stream import sample [as 别名]
class StreamsTestsPlain(object):
    
    def __init__(self):
        self._stream = None
        self._oauth = None
        self._oauth_api = None
        
        self._stream_init()
        self._oauth_init()
        
    def _oauth_init(self):
        self._oauth = OAuthHandler(social_keys.TWITTER_CONSUMER_KEY, social_keys.TWITTER_CONSUMER_SECRET)
        self._oauth.set_access_token(social_keys.TWITTER_APP_ACCESS_TOKEN,social_keys.TWITTER_APP_ACCESS_TOKEN_SECRET)
        self._oauth_api = API(self._oauth)
    
    def _stream_init(self):
        api1 = API()
        
        headers = {}
        headers["Accept-Encoding"] = "deflate, gzip"
        stream_auth = BasicAuthHandler(social_keys.TWITTER_HTTP_AUTH_U,social_keys.TWITTER_HTTP_AUTH_P)
        
        l = TestStreamListener(api=api1)
        self._stream = Stream(auth=stream_auth,listener=l,secure=True,headers=headers)
    
    def sample(self):
        self._stream.sample()
        
    def filter_follow(self):
        follow_names = ['hnfirehose','StockTwits','YahooFinance','Street_Insider','TheStreet','SquawkCNBC','CNBC','AP_PersonalFin','themotleyfool','MarketWatch','Reuters_Biz']
        follow_usr_objs = self._oauth_api.lookup_users(screen_names=follow_names)
        follow_ids = []
        
        for follow_usr in follow_usr_objs:
            follow_ids.append(follow_usr.id)
        
        print follow_ids
        
        self._stream.filter(follow=follow_ids)
    
    def filter(self):
        self._stream.filter(track=["@newsdotme","@twixmit","@app_engine"])
开发者ID:twixmit,项目名称:twixmit,代码行数:44,代码来源:streamstest.py

示例6: TweepyStreamBackoffTests

# 需要导入模块: from tweepy.streaming import Stream [as 别名]
# 或者: from tweepy.streaming.Stream import sample [as 别名]
class TweepyStreamBackoffTests(unittest.TestCase):
    def setUp(self):
        #bad auth causes twitter to return 401 errors
        self.auth = OAuthHandler("bad-key", "bad-secret")
        self.auth.set_access_token("bad-token", "bad-token-secret")
        self.listener = MockStreamListener(self)
        self.stream = Stream(self.auth, self.listener)

    def tearDown(self):
        self.stream.disconnect()

    def test_exp_backoff(self):
        self.stream = Stream(self.auth, self.listener, timeout=3.0,
                             retry_count=1, retry_time=1.0, retry_time_cap=100.0)
        self.stream.sample()
        # 1 retry, should be 4x the retry_time
        self.assertEqual(self.stream.retry_time, 4.0)

    def test_exp_backoff_cap(self):
        self.stream = Stream(self.auth, self.listener, timeout=3.0,
                             retry_count=1, retry_time=1.0, retry_time_cap=3.0)
        self.stream.sample()
        # 1 retry, but 4x the retry_time exceeds the cap, so should be capped
        self.assertEqual(self.stream.retry_time, 3.0)

    mock_resp = MagicMock()
    mock_resp.return_value.status = 420

    @patch(getresponse_location, mock_resp)
    def test_420(self):
        self.stream = Stream(self.auth, self.listener, timeout=3.0, retry_count=0,
                             retry_time=1.0, retry_420=1.5, retry_time_cap=20.0)
        self.stream.sample()
        # no retries, but error 420, should be double the retry_420, not double the retry_time
        self.assertEqual(self.stream.retry_time, 3.0)
开发者ID:GoNexia,项目名称:tweepy,代码行数:37,代码来源:test_streaming.py

示例7: classify_tweets

# 需要导入模块: from tweepy.streaming import Stream [as 别名]
# 或者: from tweepy.streaming.Stream import sample [as 别名]
def classify_tweets(label):
    stream = Stream(oauth(), TweetAnalyzer(label), secure=True)
    stream.sample()
开发者ID:Epictetus,项目名称:jubatus-example,代码行数:5,代码来源:classify-twitter-stream.py

示例8: on_error

# 需要导入模块: from tweepy.streaming import Stream [as 别名]
# 或者: from tweepy.streaming.Stream import sample [as 别名]
    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 

if __name__ == '__main__':

    #con = Connection('localhost')
    #db = con.tweettest
    #col = db.foo

    auth = get_oauth()
    stream = Stream(auth, AbstractedlyListener(), secure=True)
    stream.timeout = None
    # stream.filter(track=["twitter"])
    # stream.filter(languages=['ja'], track=['ja'])
    while True:
        try: 
            stream.sample()
        except Exception:
            time.sleep(20)
            print "sleep 20 sec"
            stream = Stream(auth, AbstractedlyListener(), secure=True)



开发者ID:tom--bo,项目名称:customized_twitter,代码行数:28,代码来源:twist.py


注:本文中的tweepy.streaming.Stream.sample方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。