gpt4 book ai didi

python - 流式传输 Twitter 直接消息

转载 作者:太空狗 更新时间:2023-10-29 17:49:48 26 4
gpt4 key购买 nike

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

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()

我能够打印出我的名字以及“已建立连接!”消息。
但是每当我从我 friend 的个人资料向我自己的个人资料发送直接消息时,都不会调用任何方法。
虽然,每当我从我的个人资料中发推文时,程序都会正确打印出来。

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

正确的做法是什么?

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

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

最佳答案

题中提供的代码确实正确。
问题是我在将我的应用程序权限更改为“读取、写入和直接消息”后忘记重新生成访问 token 和密码。

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

关于python - 流式传输 Twitter 直接消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31116213/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com