Question

Here is my code... It works for getting the individual tweets, not retweets. Does anyone have any idea? :(

import tweepy

CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_TOKEN = ''
ACCESS_TOKEN_SECRET = ''


def OAuth():
    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

class CustomStreamListener(tweepy.StreamListener):
    def on_status(self, status):
        try:
                geo = status.geo.get("coordinates")
                lat = geo[0]
                lon = geo[1]
                text = status.text
                print status.id ,status.author.screen_name, text, status.created_at, status.retweeted, status.retweet_count
        except Exception, e:
            #sys.stdout.write("-")
                print "StreamListener Error: %s" %e

    def on_error(self, status_code):
        print("status_code %d")%(status_code)
        return False #Kill the Stream

    def on_timeout(self):
        print("On_timeout")
        return False #Kill the Stream

def main():
    # Call method to establish the connection to twitter
    status_wrapper = TextWrapper(width=60, initial_indent='    ', subsequent_indent='    ')
    auth = OAuth()
    api = tweepy.API(auth)
    api.verify_credentials()
    dateTime = time.strftime("%Y/%m/%d %H:%M:%S")
    text = "Tweet this msg from my app at %s" %dateTime
    api.update_status(text)
    profile = api.get_user('xxxxxxxx')
    user = profile.screen_name
    id = profile.id
    print("%s : %s") %(user, id)
    while True:
            try:
                streaming = tweepy.streaming.Stream(auth, CustomStreamListener(), timeout=3000000000)
                streaming.filter(follow=[id])
            except Exception, e :
                print e

main()

Then, I tested to retweet one's message and got this error...

StreamListener Error: 'NoneType' object has no attribute 'get'

In part of normal tweet, it done...

367259704327544832 xxxxxxxx Test sending a msg 2013-08-13 12:21:51  False 0
Was it helpful?

Solution

I found out the cause of this error already, have to thanks to Taylor Singletary. It's because I try to get the coordinate from retweet msg that not support geo-tag. If I removed these 3 lines from the code, it'd work well

geo = status.geo.get("coordinates")
lat = geo[0]
lon = geo[1]

Btw, I'm still curious about retweet_count that always be 0 as another tickets.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top