2013-06-21 78 views
5

我正在使用twython(用於python的twitter API庫)連接到流API,但我似乎只獲得可能通過單詞過濾的公共Twitter流。沒有辦法獲得經過身份驗證的用戶時間表或@mentions的實時流嗎?通過twitter stream API 1.1獲取提及和DM嗎? (使用twython)

我一直在循環瀏覽REST API的延遲調用以獲得這些提及,但Twitter並不喜歡我提出如此多的請求。

Twython文檔對我的幫助不大,官方的twitter文檔也不是。

如果還有另一個python庫比twython更適合流式傳輸(對於Twitter API v1.1)。我會很感激這個建議......謝謝。

回答

3

在我的研究開始時,我認爲python-twitter用於Python的twitter庫。但最後,看起來好像更受歡迎,並支持Twitter流。

這有點棘手,流媒體API和REST API對於直接消息是不相等的。這個小示例腳本演示如何使用用戶流獲得直接的信息:

import twitter # if this module does not 
       # contain OAuth or stream, 
       # check if sixohsix' twitter 
       # module is used! 
auth = twitter.OAuth(
    consumer_key='...', 
    consumer_secret='...', 
    token='...', 
    token_secret='...' 
) 

stream = twitter.stream.TwitterStream(auth=auth, domain='userstream.twitter.com') 

for msg in stream.user(): 
    if 'direct_message' in msg: 
     print msg['direct_message']['text'] 

這個腳本會打印所有新郵件 - 不啓動腳本之前已經收到的人。

0

無法傳輸直接消息。

但是,有一種方法可以流式傳輸用戶時間表。檢查出的文檔在Twitter上的位置:https://dev.twitter.com/docs/streaming-apis/streams/user

from twython import TwythonStreamer 


class MyStreamer(TwythonStreamer): 
    def on_success(self, data): 
     if 'text' in data: 
      print data['text'].encode('utf-8') 
     # Want to disconnect after the first result? 
     # self.disconnect() 

    def on_error(self, status_code, data): 
     print status_code, data 

# Requires Authentication as of Twitter API v1.1 
stream = MyStreamer(APP_KEY, APP_SECRET, 
        OAUTH_TOKEN, OAUTH_TOKEN_SECRET) 

stream.user() 

待到requestshttps://github.com/kennethreitz/requests)發佈了新的版本,但是,從你的追隨者鳴叫將在後面一個職位。儘管這應該儘快修復! :)

+2

這似乎是錯誤的。直接消息流在這裏提到:https://dev.twitter.com/docs/streaming-apis/streams/user#Direct_messages – lumbric

+0

哦,我很抱歉;我從來沒有看到這個文檔。 –

+0

更新或刪除您的答案以反映DM流。 – Sheharyar

相關問題