def tweet_string(message, log, media=None):
check_twitter_config()
logging.captureWarnings(True)
old_level = log.getEffectiveLevel()
log.setLevel(logging.ERROR)
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
retries = 0
while retries < 5:
log.setLevel(logging.ERROR)
try:
if media:
photo = open(media, 'rb')
media_ids = twitter.upload_media(media=photo)
twitter.update_status(status=message.encode('utf-8').strip(), media_ids=media_ids['media_id'])
else:
twitter.update_status(status=message.encode('utf-8').strip())
break
except TwythonAuthError, e:
log.setLevel(old_level)
log.exception(" Problem trying to tweet string")
twitter_auth_issue(e)
return
except:
log.setLevel(old_level)
log.exception(" Problem trying to tweet string")
retries += 1
s = random.randrange(5, 10 * retries)
log.debug(" sleeping %d seconds for retry", s)
time.sleep(s)
log.setLevel(old_level)
if retries == 5:
log.error("Couldn't tweet string: %s with media: %s", message, media)
评论列表
文章目录