<commit_before>import json
import urlparse

from jfr_playoff.remote import RemoteUrl as p_remote
from jfr_playoff.tournamentinfo import TournamentInfoClient

FLAG_CDN_URL = 'https://cdn.tournamentcalculator.com/flags/'


class TCJsonTournamentInfo(TournamentInfoClient):
    def get_results_link(self, suffix):
        return urlparse.urljoin(self.settings['link'], suffix)

    def is_finished(self):
        settings_json = json.loads(
            p_remote.fetch_raw(self.get_results_link('settings.json')))
        return (not settings_json['LiveResults']) \
            and (settings_json['LastPlayedRound'] > 0) \
            and (settings_json['LastPlayedSession'] > 0)

    def get_tournament_results(self):
        results = []
        results_json = json.loads(
            p_remote.fetch_raw(self.get_results_link('results.json')))
        for result in results_json['Results']:
            participant = result['Participant']
            flag_url = None
            flag = participant['_flag']
            if flag is not None:
                flag_url = self.get_results_link(
                    flag['CustomFlagPath']
                    if flag['IsCustom']
                    else '%s/%s.png' % (FLAG_CDN_URL, flag['CountryNameCode']))
            results.append((
                result['ParticipantGroup'], result['Place'],
                participant['_name'], participant['_shortName'],
                flag_url))
        return [list(r[2:]) + [None] for r in sorted(results)]

    def get_exceptions(self, method):
        return (TypeError, IndexError, KeyError, IOError, ValueError)
<commit_msg>Logging added to TC JSON tournament results<commit_after>import json
import urlparse

from jfr_playoff.logger import PlayoffLogger
from jfr_playoff.remote import RemoteUrl as p_remote
from jfr_playoff.tournamentinfo import TournamentInfoClient

FLAG_CDN_URL = 'https://cdn.tournamentcalculator.com/flags/'


class TCJsonTournamentInfo(TournamentInfoClient):
    def get_exceptions(self, method):
        return (TypeError, IndexError, KeyError, IOError, ValueError)

    def get_results_link(self, suffix):
        link = urlparse.urljoin(self.settings['link'], suffix)
        PlayoffLogger.get('tcjson').info(
            'generating tournament-specific link from leaderboard link %s: %s -> %s',
            self.settings['link'], suffix, link)
        return link

    def is_finished(self):
        settings_json = json.loads(
            p_remote.fetch_raw(self.get_results_link('settings.json')))
        live_results = settings_json['LiveResults']
        last_round = settings_json['LastPlayedRound']
        last_session = settings_json['LastPlayedSession']
        finished = (not live_results) \
            and (last_round > 0) and (last_session > 0)
        PlayoffLogger.get('jfrhtml').info(
            'tournament settings (live = %s, last_round = %d, last_session = %d) indicate finished: %s',
            live_results, last_round, last_session, finished)
        return finished

    def get_tournament_results(self):
        results = []
        results_json = json.loads(
            p_remote.fetch_raw(self.get_results_link('results.json')))
        for result in results_json['Results']:
            participant = result['Participant']
            flag_url = None
            flag = participant['_flag']
            if flag is not None:
                flag_url = self.get_results_link(
                    flag['CustomFlagPath']
                    if flag['IsCustom']
                    else '%s/%s.png' % (FLAG_CDN_URL, flag['CountryNameCode']))
            results.append((
                result['ParticipantGroup'], result['Place'],
                participant['_name'], participant['_shortName'],
                flag_url))
        PlayoffLogger.get('tcjson').info(
            'tournament results fetched: %s' % results)
        return [list(r[2:]) + [None] for r in sorted(results)]

{'fraction_non_alphanumeric': 0.10808748728382503, 'fraction_numerical': 0.001525940996948118, 'mean_word_length': 2.634935304990758, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4722182', 'n_tokens_mistral': 1126, 'n_tokens_neox': 1084, 'n_words': 235}