import tweepy
import sys
creds = json.loads(open('credential.json').read())
tw_consumer_key = creds['tw_consumer_key']
tw_consumer_secret = creds['tw_consumer_secret']
tw_access_token = creds['tw_access_token']
tw_access_token_secret = creds['tw_access_token_secret']
try:
auth = tweepy.OAuthHandler(tw_consumer_key,tw_consumer_secret)
auth.set_access_token(tw_access_token,tw_access_token_secret)
api = tweepy.API(auth)
except Exception:
service = None
api = None
# Query terms
Q = "Better"
class CustomStreamListener(tweepy.StreamListener):
def on_status(self,status):
try:
print "%s\n%s\n%s\n%s\n\n" % (status.text,status.author.screen_name,status.created_at,status.source,)
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 LiveStream(webapp2.RequestHandler):
def get(self):
streaming_api = tweepy.streaming.Stream(auth,CustomStreamListener(),timeout=60)
self.response.out.write(streaming_api.filter(track=Q))
可能由GAE引起的不允许套接字,我不知道如何应用查询术语来获取特定的过滤流式推文.这部分代码的目的是获取指定关键字的实时流.如果有其他方法,请指导.
最佳答案
在App Engine上,httplib连接(和urllib)将使用Google URL提取服务. URL Fetch service表示其他服务器(不是app引擎服务实例)执行请求并将响应返回给app引擎实例,而不是进程本身打开套接字.
原文链接:https://www.f2er.com/python/439459.html我相信你注意到在GAE上运行的httplib变体没有提供用于设置超时的sock属性.但基本问题是无法获得流,在响应完成之前,您不会将任何结果返回到应用程序引擎中.我还没有测试它是如何失败的,但我希望你会从URL服务中获得DeadlineExceededError,因为Twitter不会关闭流响应.