今年春天我構建了一個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
任何幫助表示讚賞:)
其實這不是問題。問題似乎是,因爲我正在尋找關於相對較少主題的推文(因爲它們在芬蘭語中),所以我不會爲連接獲得足夠快的推文。所以我什至不分析任何東西。我想,即使沒有不斷髮布的推文,我應該如何保持聯繫仍然存在,這一點已經發生了變化。 – Tumetsu 2014-10-01 13:34:43