2014-09-30 132 views
1

今年春天我構建了一個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 

任何幫助表示讚賞:)

回答

0

最終問題是由稍微舊的Twitter庫引起的。通過pip更新它,腳本似乎可以像以前一樣工作。

0

如果你的分析時間過長,Twitter將掛斷你。 也許您的程序不夠快,無法實時處理採樣器Feed? 您的程序在MongoDB中需要多長時間進行分析和存儲?

考慮只是傾銷一些記錄進行分析,並從磁盤而不是實時處理它們。

+0

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