def __init__(self, config):
        self.config = config

        logger.debug("Creating API instnce")
        api_config = pyroyale.Configuration()
        api_config.api_key['authorization'] = config['api']['api_key']
        if config['api']['proxy']:
            api_config.proxy = config['api']['proxy']
        if config['api']['proxy_headers']:
            api_config.proxy_headers = config['api']['proxy_headers']

        self.clans = pyroyale.ClansApi(pyroyale.ApiClient(api_config))
        self.players = pyroyale.PlayersApi(pyroyale.ApiClient(api_config))
def get_data_from_api(config):  # pragma: no coverage
    # get API instance
    configuration = pyroyale.Configuration()
    configuration.api_key['authorization'] = config['api']['api_key']
    if config['api']['proxy']:
        configuration.proxy = config['api']['proxy']
    if config['api']['proxy_headers']:
        configuration.proxy_headers = config['api']['proxy_headers']

    api = pyroyale.ClansApi(pyroyale.ApiClient(configuration))

    try:
        # Get clan data and war log from API.
        clan = api.get_clan(config['api']['clan_id'])
        warlog = api.get_clan_war_log(config['api']['clan_id'])
        current_war = api.get_current_war(config['api']['clan_id'])

        print('- clan: {} ({})'.format(clan.name, clan.tag))

        return (clan, warlog, current_war)
    except pyroyale.ApiException as e:
        if e.body:
            body = json.loads(e.body)
            if body['reason'] == 'accessDenied':
                logger.error(
                    'developer.clashroyale.com claims that your API key is invalid. Please make sure you are setting up crtools with a valid key.'
                )
            elif body['reason'] == 'accessDenied.invalidIp':
                logger.error('developer.clashroyale.com says: {}'.format(
                    body['message']))
            else:
                logger.error('error: {}'.format(body))
        else:
            logger.error('error: {}'.format(e))
    except pyroyale.OpenApiException as e:
        logger.error('error: {}'.format(e))

    # If we've gotten here, something has gone wrong. We need to abort the application.
    exit(0)
Example #3
0
 def setUp(self):
     # create an instance of the API class
     self.api = pyroyale.PlayersApi(
         pyroyale.ApiClient(config.getConfiguration()))
     pass
def build_dashboard(config):  # pragma: no coverage
    """Compile and render clan dashboard."""

    logger.debug('crtools version v{}'.format(__version__))
    #logger.debug('pyroyale version v{}'.format(pyroyale.__version__))
    logger.debug(config)

    # Create temporary directory. All file writes, until the very end,
    # will happen in this directory, so that no matter what we do, it
    # won't hose existing stuff.
    tempdir = tempfile.mkdtemp(config['paths']['temp_dir_name'])

    # get API instance
    configuration = pyroyale.Configuration()
    configuration.api_key['authorization'] = config['api']['api_key']
    api = pyroyale.ClansApi(pyroyale.ApiClient(configuration))

    # Putting everything in a `try`...`finally` to ensure `tempdir` is removed
    # when we're done. We don't want to pollute the user's disk.
    try:
        output_path = os.path.expanduser(config['paths']['out'])

        # Get clan data and war log from API.
        clan = api.get_clan(config['api']['clan_id']).to_dict()
        warlog = api.get_clan_war_log(config['api']['clan_id']).to_dict()
        current_war = api.get_current_war(config['api']['clan_id']).to_dict()

        # process data from API
        current_war_processed = process_current_war(config, current_war)
        clan_processed = process_clan(config, clan, current_war)
        member_history = history.get_member_history(
            clan['member_list'], io.get_previous_history(output_path),
            current_war)
        members_processed = process_members(config, clan, warlog, current_war,
                                            member_history)
        recent_wars = process_recent_wars(config, warlog)
        former_members = process_absent_members(config,
                                                member_history['members'])

        io.parse_templates(
            config, member_history, tempdir, clan_processed, members_processed,
            former_members, current_war_processed, recent_wars,
            get_suggestions(config, members_processed, clan_processed),
            get_scoring_rules(config))

        if (config['crtools']['debug'] == True):
            # archive outputs of API for debugging
            io.dump_debug_logs(
                tempdir, {
                    'clan': clan,
                    'warlog': warlog,
                    'currentwar': current_war,
                    'clan-processed': clan_processed,
                    'members-processed': members_processed,
                    'currentwar-processed': current_war_processed,
                    'recentwars-processed': recent_wars
                })

        # if fankit is previously downloaded, it will copy fankit. Otherwise,
        # if fankit is enabled, it will download it.
        fankit.get_fankit(tempdir, output_path, config['paths']['use_fankit'])

        io.copy_static_assets(tempdir, config['paths']['clan_logo'],
                              config['paths']['favicon'])

        io.move_temp_to_output_dir(tempdir, output_path)

    except ApiException as e:
        logger.error('error: {}'.format(e))

    finally:
        # Ensure that temporary directory gets deleted no matter what
        shutil.rmtree(tempdir)