My application is to retweet a hashtag using Tweepy. Retweeting the hashtag works, I am having trouble getting errors 2 and 3 to work. Errors 1. Your own id (done) 2. If tweet has already been RTd 3. If tweet to RT comes from protected source
Accessing api.retweet(doTweet) within the StdOutListener does not allow it to fall through to on_error(). How else can I do this? I am Python noob.
class StdOutListener(tweepy.StreamListener):
def on_data(self, data):
all_data = json.loads(data)
username = all_data["user"]["screen_name"]
doTweet = all_data["id"]
if username != our_own_id:
#make sure you haven't already retweeted
#make sure tweets aren't protected
print(username) # just so we know it's working
api.retweet(doTweet)
return True
def on_error(self, status_code):
print('error')
read_error = json.loads(status_code)
print('Got an error with status code: ' + str(read_error))
return True # To continue listening
def on_timeout(self):
print('Timeout...')
return True # To continue listening
try:
if __name__ == '__main__':
listener = StdOutListener()
stream = tweepy.Stream(auth, listener)
stream.filter(track=['#love'])
except KeyboardInterrupt:
sys.exit()