本文整理匯總了Python中twitter.TwitterStream方法的典型用法代碼示例。如果您正苦於以下問題:Python twitter.TwitterStream方法的具體用法?Python twitter.TwitterStream怎麽用?Python twitter.TwitterStream使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類twitter
的用法示例。
在下文中一共展示了twitter.TwitterStream方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: run_sample
# 需要導入模塊: import twitter [as 別名]
# 或者: from twitter import TwitterStream [as 別名]
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()
示例2: streamAPI
# 需要導入模塊: import twitter [as 別名]
# 或者: from twitter import TwitterStream [as 別名]
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) )
示例3: stream_data
# 需要導入模塊: import twitter [as 別名]
# 或者: from twitter import TwitterStream [as 別名]
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)