Esempio n. 1
0
def _load_credentials():
  """Loads saved credentials.
  If available and valid, a new Tweepy api object will be returned.
  """
  tweepy_config_path = os.path.join(global_config.get_settings_dir(), tweepy_config_name)
  try:
    tweepy_config = ConfigParser.RawConfigParser()
    tweepy_config.read(tweepy_config_path)
    if (tweepy_config.has_section("Credentials")):
      k = tweepy_config.get("Credentials", "access_key")
      s = tweepy_config.get("Credentials", "access_secret")
      if (k and s):
        tweepy_auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
        tweepy_auth.set_access_token(k, s)

        temp_api = tweepy.API(tweepy_auth)
        if (temp_api.verify_credentials()):
          return temp_api

  except (tweepy.TweepError) as err:
    logging.error(str(err.reason))

  except (Exception) as err:
    logging.error("Could not parse %s: %s" % (tweepy_config_path, str(err)))

  return None
Esempio n. 2
0
def _save_credentials(tweepy_api):
  """Saves Tweepy credentials."""
  tweepy_config_path = os.path.join(global_config.get_settings_dir(), tweepy_config_name)
  try:
    tweepy_config = ConfigParser.RawConfigParser()
    tweepy_config.add_section("Credentials")
    tweepy_config.set("Credentials", "access_key", tweepy_api.auth.access_token.key)
    tweepy_config.set("Credentials", "access_secret", tweepy_api.auth.access_token.secret)
    with open(tweepy_config_path, "wb") as f: tweepy_config.write(f)
    return True

  except (Exception) as err:
    logging.error("Could not write %s: %s" % (tweepy_config_path, str(err)))

  return False