def tweet_earthquake(self, earthquake_key): # Always call this method after adding to the DB. earthquake = db.get(earthquake_key) if earthquake.magnitude <= 0.0 and len(earthquake.location) > 0: cnf = config.Config() t_creds = cnf.twitter_credentials() t = Twitter(t_creds['username'], t_creds['password']) bitly_creds = cnf.bitly_credentials() url = "http://zemjotresi.appspot.com/zemjotres/%s" % earthquake_key short_url = t.shorten_url(url) if short_url['good']: short_url = short_url['url'] else: logging.error("Coulnd't shorten url (%s). Data: %s." % (url, short_url)) return False template_id = int(round(random.uniform(1, 2))) template_path = os.path.join(os.path.dirname(__file__), "../template/tweets/tweet_%s.txt" % template_id) tweet = template.render(template_path, {'earthquake': earthquake, 'short_url': short_url}) status = t.update_status(tweet) if status['good']: logging.info("Tweeted status_id: %s !" % status["id"]) elif status['good'] == False and status["error"] == 403: logging.error("Couldn't tweet status for (%s). Exceeded update limit, got message: %s!" % (earthquake_key, status['message'])) else: logging.error("Couldn't tweet status for (%s). Got error %s with data: %s!" % (earthquake_key, status['error'], status['data'])) else: logging.info("Not tweeting status for (%s)!" % earthquake_key)