summaryrefslogtreecommitdiff
path: root/jfr_playoff/data/tournament/tcjson.py
blob: 8d7cd34cd86114dda08c2c328e6c3bfe03beaad2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import json
import urlparse

from jfr_playoff.logger import PlayoffLogger
from jfr_playoff.remote import RemoteUrl as p_remote
from jfr_playoff.data.tournament import TournamentInfoClient

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


class TCJsonTournamentInfo(TournamentInfoClient):
    @property
    def priority(self):
        return 20

    def is_capable(self):
        return 'link' in self.settings

    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('tournament.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('tournament.tcjson').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')))
        participant_groups = []
        for result in results_json['Results']:
            group = result['ParticipantGroup']
            if group is not None:
                if group not in participant_groups:
                    participant_groups.append(group)
                group_id = participant_groups.index(group) + 1
            else:
                group_id = 999999
            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((
                group_id, result['Place'],
                participant['_name'], participant['_shortName'],
                flag_url))
        PlayoffLogger.get('tournament.tcjson').info(
            'tournament results fetched: %s' % results)
        return [list(r[2:]) + [None] for r in sorted(results)]