简体   繁体   English

Twitter流API返回挂断

[英]Twitter streaming API returns hangup

I built a Python app this spring which was meant to keep running monitoring public stream and collect tweets about certain topics. 我在今年春天构建了一个Python应用程序,旨在继续监视公共流并收集有关某些主题的推文。 The code worked fine but now when I wanted to continue it, I keep getting {hangup: True} response if the stream doesn't get tweets almost constantly. 该代码运行良好,但是现在当我想继续使用它时,如果流没有得到几乎连续的推文,我将不断得到{hangup:True}响应。 I haven't been able to figure out what has changed and what I should do to fix the problem. 我一直无法弄清楚发生了什么变化以及应该如何解决该问题。

I'm using sixohsix's Twitter library: https://github.com/sixohsix/twitter And below is my code for monitoring the stream: 我正在使用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

Any help is appreciated :) 任何帮助表示赞赏:)

If your analysis takes too long, Twitter will hangup on you. 如果您的分析时间太长,Twitter会挂断您。 Maybe your program isn't fast enough to process the sampler feed in realtime? 也许您的程序不够快,无法实时处理采样器提要? How long does your program need for analysis and storage in MongoDB? 您的程序需要多长时间在MongoDB中进行分析和存储?

Consider just dumping a number of records for analysis, and processing them from disk instead of live. 考虑仅转储许多记录以进行分析,然后从磁盘而不是实时处理它们。

In the end the problem was cause by slightly old twitter library. 最后,问题是由较旧的Twitter库引起的。 By updating it via pip the script seems to work as in the past. 通过pip更新脚本似乎可以像过去一样工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM