import configparser


def get_api_config(filename):
    """
    Attempt to pull in twitter app API key and secret. If the key 
    and secret don't exist prompt for them.

    Arguments:
    filename -- name of the config file to try and parse
    
    Returns:
    config_api_store -- contains the twitter API key and secret
    """

    config_api_store = {}
    config_twiter_api = configparser.ConfigParser()
    config_twiter_api.read(filename)
    # Try and find the API key and secret in the config file
    try:
        config_api_store["CONSUMER_KEY"] = config_twiter_api['DEFAULT']['CONSUMER_KEY']
        config_api_store["CONSUMER_SECRET"] = config_twiter_api['DEFAULT']['CONSUMER_SECRET']
    # If we can't find them, prompt for them and write them in to the configuration file
    except KeyError:
        print("Visit https://apps.twitter.com/ to create an application and aquire these values (API key and API secret)")
        config_api_store["CONSUMER_KEY"] = input("Please enter a valid twitter app API key: ")
        config_api_store["CONSUMER_SECRET"] = input("Please enter a valid twitter app API secret: ")
        api_config_file = configparser.ConfigParser()
        api_config_file['DEFAULT'] = {'CONSUMER_KEY': config_api_store["CONSUMER_KEY"], 'CONSUMER_SECRET': config_api_store["CONSUMER_SECRET"]}
        with open(filename, 'w') as configfile:
            api_config_file.write(configfile)
    return config_api_store