Я использую следующий код для потоковой передачи прямых сообщений, полученных мой счет Twitter -:Streaming 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()
Я могу получить мое имя напечатанное, а также «Соединение установлено!» сообщение.
Но всякий раз, когда я отправляю прямое сообщение в свой профиль из профиля моего друга, метод не вызывается.
Хотя всякий раз, когда я чирикаю что-то из своего профиля, он правильно печатается программой.
Я и мой друг оба следуют друг за другом в Twitter, поэтому не должно быть никаких проблем с правами прямого сообщения.
Каков правильный способ?
Кроме того, если это невозможно сделать в Tweepy вообще, я готов использовать любую другую библиотеку Python.
Я использую Tweepy 3.3.0 и Python 3.4 на Windows 7.