流式传输 Twitter 直接消息 [英] Streaming Twitter direct messages

查看:32
本文介绍了流式传输 Twitter 直接消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码来流式传输我的 Twitter 帐户收到的直接消息 -:

I am using the following code to stream direct messages received by my Twitter account -:

from tweepy import Stream
from tweepy import OAuthHandler
from tweepy import API

from tweepy.streaming import StreamListener

# These values are appropriately filled in the code
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""

class StdOutListener( StreamListener ):

    def __init__( self ):
        self.tweetCount = 0

    def on_connect( self ):
        print("Connection established!!")

    def on_disconnect( self, notice ):
        print("Connection lost!! : ", notice)

    def on_data( self, status ):
        print("Entered on_data()")
        print(status, flush = True)
        return True

    def on_direct_message( self, status ):
        print("Entered on_direct_message()")
        try:
            print(status, flush = True)
            return True
        except BaseException as e:
            print("Failed on_direct_message()", str(e))

    def on_error( self, status ):
        print(status)

def main():

    try:
        auth = OAuthHandler(consumer_key, consumer_secret)
        auth.secure = True
        auth.set_access_token(access_token, access_token_secret)

        api = API(auth)

        # If the authentication was successful, you should
        # see the name of the account print out
        print(api.me().name)

        stream = Stream(auth, StdOutListener())

        stream.userstream()

    except BaseException as e:
        print("Error in main()", e)

if __name__ == '__main__':
    main()

我能够打印我的名字以及连接已建立!"留言.
但是,每当我从朋友的个人资料向我自己的个人资料发送直接消息时,都不会调用任何方法.
虽然,每当我从我的个人资料中发推文时,它都会被程序正确打印.

I am able to get my name printed as well as the "Connection established!" message.
But whenever I send a direct message to my own profile from my friend's profile, no method is called.
Although, whenever I tweet something from my profile, it is printed correctly by the program.

我和我的朋友都在 Twitter 上互相关注,所以直接消息权限应该没有任何问题.

Me and my friend both follow each other on Twitter, so there shouldn't be any problems with the direct message permissions.

正确的做法是什么?

此外,如果根本无法在 Tweepy 中完成,我准备使用任何其他 Python 库.

Also, if it cannot be done in Tweepy at all, I am ready to use any other Python library.

我在 Windows 7 上使用 Tweepy 3.3.0 和 Python 3.4.

I am using Tweepy 3.3.0 and Python 3.4 on Windows 7.

推荐答案

问题中提供的代码确实是正确的.
问题是我在将应用程序权限更改为读取、写入和发送消息"后忘记重新生成访问令牌和密码.

The code provided in the question is indeed correct.
The problem was that I had forgotten to regenerate the access token and secret after changing my application permissions to "Read, write and direct messages".

注意:直接消息通过 on_data() 方法而不是 on_direct_message() 方法到达.

Note: The direct messages arrive in the on_data() method rather than the on_direct_message() method.

这篇关于流式传输 Twitter 直接消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆