Twitter流媒体API返回挂断
问题描述:
今年春天我构建了一个Python应用程序,旨在持续监视公共流并收集关于特定主题的推文。代码运行良好,但现在当我想继续运行时,如果流不会几乎不断地收到推文,我会一直收到{hangup:True}响应。我一直无法弄清楚发生了什么变化以及我应该如何解决问题。Twitter流媒体API返回挂断
我使用sixohsix的Twitter库:https://github.com/sixohsix/twitter 而下面是我的监控流代码:
q = 'huuhkajat' # Comma-separated list of terms
print sys.stderr, 'Filtering the public timeline for track="%s"' % (q,)
api = authTwitter.getApi()
mongodb = DatabaseHandler()
analyze = Analysis(mongodb.getDictionary())
# Reference the self.auth parameter
twitter_stream = twitter.TwitterStream(auth=api.auth) # See https://dev.twitter.com/docs/streaming-apis
stream = twitter_stream.statuses.filter(track=q)
try:
i = 0
for tweet in stream:
print tweet
if 'lang' in tweet.keys() and tweet['lang'] == 'fi':
print tweet['text'] + " " + tweet['lang']
print tweet['place']
analyze.analyseTweet(tweet)
if i % 1 == 0:
print
analyze.printStatistics()
entry = {'positive' : analyze.getPositivePercent(), 'negative' : analyze.getNegativePercent(), 'neutral' : analyze.getNeutralPercent(), '_id' : 'sentimentPercentages', 'totalCount' : analyze.getCount(), 'latestTweet' : tweet, 'query' : q}
mongodb.saveToDb(entry, mongodb.statisticCollection)
mongodb.storeToDb(tweet, q) #tallentaa collectioniin jonka nimi on hakusana
print
i += 1
except twitter.TwitterHTTPError, e:
f = open('streamErrors.log','w')
f.write(e.message+'\n')
f.close()
print "ERROR " + e.message
任何帮助表示赞赏:)
答
最终问题是由稍微旧的Twitter库引起的。通过pip更新它,脚本似乎可以像以前一样工作。
答
如果你的分析时间过长,Twitter将挂断你。 也许您的程序不够快,无法实时处理采样器Feed? 您的程序在MongoDB中需要多长时间进行分析和存储?
考虑只是倾销一些记录进行分析,并从磁盘而不是实时处理它们。
其实这不是问题。问题似乎是,因为我正在寻找关于相对较少主题的推文(因为它们在芬兰语中),所以我不会为连接获得足够快的推文。所以我什至不分析任何东西。我想,即使没有不断发布的推文,我应该如何保持联系仍然存在,这一点已经发生了变化。 – Tumetsu 2014-10-01 13:34:43