0

What its wrong?. I only can "catch" 140 characters of Twitter. I'cant do it for myself and i don't have experience at all. Thank's! This is the code for Python. Chears, Mariana.

    import tweepy

    consumer_key = ""
    consumer_secret = ""
    access_token = ""
    access_token_secret = ""

    class MyStreamListener(tweepy.StreamListener):
    def on_connect(self):
    print("I'am connected")
    def on_status(self, status):
    print(status.text)
    def on_error(self, status_code):
    print("Error", status_code)

    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)

    myStreamListener = MyStreamListener()
    myStream = tweepy.Stream(auth=api.auth, listener=myStreamListener, tweet_mode="extended")

    myStream.filter(locations=[])

1 Answers1

0

You are doing it correct by letting tweepy get the extended tweets by using:

myStream = Stream(my_auth, my_listener, tweet_mode='extended')

But the 280 character tweet is hidden in the json blob under the key "full_text"

So I just did a scan of possible json paths that end with full_text and I got this list:

  • ['retweeted_status', 'quoted_status', 'extended_tweet', 'full_text']
  • ['quoted_status', 'extended_tweet', 'full_text']
  • ['retweeted_status', 'extended_tweet', 'full_text']
  • ['extended_tweet', 'full_text']

So just a quick and dirty way of doing this:

class StdOutListener(StreamListener):
    def on_data(self, data):
        parsed = json.loads(data)
        if "full_text" in data:
            try:
                print(parsed['retweeted_status']['quoted_status']['extended_tweet']['full_text'])
            except Exception:
                pass
            try:
                print(parsed['quoted_status']['extended_tweet']['full_text'])
            except Exception:
                pass
            try:
                print(parsed['retweeted_status']['extended_tweet']['full_text'])
            except Exception:
                pass
            
            try:
                print(parsed['extended_tweet']['full_text'])
            except Exception:
                pass

        else:
            print(parsed['text'])

        print("="*50)
        return True

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

if __name__ == '__main__':
    my_listener = StdOutListener()
    my_auth = OAuthHandler(consumer_key, consumer_secret)
    my_auth.set_access_token(access_token, access_token_secret)
    stream = Stream(my_auth, my_listener, tweet_mode='extended')
    stream.filter(track=['food'])

Credits to the json path builder here

Geoffrey Cai
  • 159
  • 7