मैं अपने ट्विटर खाते से प्राप्त सीधे संदेश स्ट्रीम करने के लिए निम्नलिखित कोड का उपयोग कर रहा हूँ -:स्ट्रीमिंग ट्विटर सीधे संदेश
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()
मैं अपने नाम के रूप में रूप में अच्छी तरह मुद्रित प्राप्त करने में सक्षम हूँ "कनेक्शन स्थापित!" संदेश।
लेकिन जब भी मैं अपने दोस्त की प्रोफ़ाइल से अपनी प्रोफ़ाइल पर सीधा संदेश भेजता हूं, तो कोई विधि नहीं कहा जाता है।
हालांकि, जब भी मैं अपनी प्रोफ़ाइल से कुछ ट्वीट करता हूं, तो यह प्रोग्राम द्वारा सही तरीके से मुद्रित होता है।
मुझे और मेरे दोस्त दोनों ट्विटर पर एक-दूसरे का अनुसरण करते हैं, इसलिए प्रत्यक्ष संदेश अनुमतियों में कोई समस्या नहीं होनी चाहिए।
ऐसा करने का सही तरीका क्या है?
इसके अलावा, अगर यह ट्वेपी में भी नहीं किया जा सकता है, तो मैं किसी भी अन्य पायथन पुस्तकालय का उपयोग करने के लिए तैयार हूं।
मैं Tweepy 3.3.0 और अजगर 3.4 का उपयोग कर रहा विंडोज 7