2015-06-29 7 views
12

मैं अपने ट्विटर खाते से प्राप्त सीधे संदेश स्ट्रीम करने के लिए निम्नलिखित कोड का उपयोग कर रहा हूँ -:स्ट्रीमिंग ट्विटर सीधे संदेश

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

उत्तर

6

पर सवाल में प्रदान किए गए कोड वास्तव में सही है।
समस्या यह थी कि मैं "पर अपनी एप्लिकेशन अनुमतियों को बदलने के बाद पहुंच टोकन और रहस्य को पुन: उत्पन्न करना भूल गया था संदेश पढ़ें, लिखें और निर्देशित करें"।

नोट: सीधे संदेश के बजाय on_data() विधि on_direct_message() विधि में पहुंचें।

0

एक और समाधान on_data विधि को ओवरराइड नहीं करना है। इस तरह मैंने अपने मामले में काम करने के लिए अपना कोड उदाहरण प्राप्त किया।

tweepy.streaming.StreamListener वस्तु की on_data विधि सभी json डेटा हैंडलिंग और अन्य तरीकों में से फोन, on_direct_message की तरह है, कि तुम और मैं उम्मीद कर रहे थे शामिल हैं।

0

यहाँ 2017

from twitter import * 
import os 

import oauth 

#Create a new Twitter app first: https://apps.twitter.com/app/new 


APP_KEY,APP_SECRET = 'H3kQtN5PQgRiA0ocRCCjqjt2P', '51UaJFdEally81B7ZXjGHkDoDKTYy430yd1Cb0St5Hb1BVcDfE' 
# OAUTH_TOKEN, OAUTH_TOKEN_SECRET = '149655407-TyUPMYjQ8VyLNY5p7jq0aMy8PjtFtd7zkIpDh3ZA', 'IUVpiDpoVmdO75UaHOTinAv5TOsAQmddttNENh9ofYuWO' 


MY_TWITTER_CREDS = os.path.expanduser('my_app_credentials') 
if not os.path.exists(MY_TWITTER_CREDS): 
    oauth_dance("crypto sentiments", APP_KEY, APP_SECRET, 
       MY_TWITTER_CREDS) 

oauth_token, oauth_secret = read_token_file(MY_TWITTER_CREDS) 

auth = OAuth(
    consumer_key=APP_KEY, 
    consumer_secret=APP_SECRET, 
    token=oauth_token, 
    token_secret=oauth_secret 
) 

twitter_userstream = TwitterStream(auth=auth, domain='userstream.twitter.com') 
for msg in twitter_userstream.user(): 
    if 'direct_message' in msg: 
     print (msg['direct_message']['text']) 
में कोड का एक काम संस्करण है
संबंधित मुद्दे