-
Notifications
You must be signed in to change notification settings - Fork 4
/
loaders.py
124 lines (108 loc) · 5.11 KB
/
loaders.py
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from tournament import Tournament
from tournament_objects import Match, Team, Game
from errors import TournamentProviderFail
from urllib import request
from urllib.error import HTTPError, URLError
from http import HTTPStatus
import processors
import requests
import json
import pprint
import random
import string
def poll_challonge(tournament_id, API_KEY):
matches_url = f"https://api.challonge.com/v1/tournaments/{tournament_id}/matches.json?api_key={API_KEY}"
teams_url = f"https://api.challonge.com/v1/tournaments/{tournament_id}/participants.json?api_key={API_KEY}"
try:
matches_page = request.urlopen(matches_url)
teams_page = request.urlopen(teams_url)
except HTTPError:
raise TournamentProviderFail("access", "challonge", tournament_id)
response_matches = matches_page.read()
response_participants = teams_page.read()
try:
response = {
"matches": json.loads(response_matches.decode()),
"participants": json.loads(response_participants.decode())
}
except json.JSONDecodeError:
raise TournamentProviderFail("parse", "challonge", tournament_id)
return response
def poll_faceit(tournament_id):
teams_url = f"https://api.faceit.com/championships/v1/championship/{tournament_id}/subscription"
groups_url = f"https://api.faceit.com/championships/v1/championship/{tournament_id}"
teams_response = requests.get(teams_url)
groups_response = requests.get(groups_url)
if teams_response.status_code == 400:
raise TournamentProviderFail("retrieve", "FACEIT", tournament_id, teams_response.text)
if groups_response.status_code == 400:
raise TournamentProviderFail("retrieve", "FACEIT", tournament_id, groups_response.text)
if not teams_response.ok or not groups_response.ok:
raise TournamentProviderFail("access", "FACEIT", tournament_id)
try:
teams = teams_response.json()
groups = groups_response.json()
except json.JSONDecodeError:
raise TournamentProviderFail("parse", "FACEIT", tournament_id)
if not teams or not groups:
raise TournamentProviderFail("parse", "FACEIT", tournament_id)
tournament = { "teams": teams, "groups": groups}
tricodes = []
teams = {}
for team in tournament["teams"]["payload"]["items"]:
current_team = team["team"]
new_team = Team()
new_team.id = current_team["id"]
new_team.name = current_team["name"]
new_team.tricode = processors.determine_tricode(current_team["name"], tricodes)
tricodes.append(new_team.tricode)
teams[new_team.id] = new_team
match_list = []
group_config = tournament["groups"]["payload"]["groups"]
for group in list(group_config.keys()):
group_url = f"https://api.faceit.com/championships/v1/championship/{tournament_id}/group/{group}/bracket"
response = requests.get(group_url)
if response.status_code == 400:
error_message = response.json()["errors"][0]["message"]
raise TournamentProviderFail("retrieve", "FACEIT", tournament_id, error_message)
elif not response.ok:
raise TournamentProviderFail("access", "FACEIT", tournament_id)
try:
group_json = response.json()
except json.JSONDecodeError:
raise TournamentProviderFail("parse", "FACEIT", tournament_id)
for match in group_json["payload"]["matches"].values():
new_match = Match()
new_match.best_of = match.get("bestOf")
new_match.id = match["id"]
new_match.teams = [0,0]
if match["status"] == "finished":
new_match.finished = True
new_match.scores = [0,0]
if match["status"] == "ongoing":
new_match.in_progress = True
for team in match["factions"]:
i = team["number"] - 1
if team.get("entity"):
new_match.teams[i] = team["entity"].get("id")
if teams.get(new_match.teams[i]) is None:
new_team = Team()
new_team.id = team["entity"].get("id")
if new_team.id != "bye":
new_team.name = team["entity"].get("name")
new_team.tricode = processors.determine_tricode(new_team.name, tricodes)
tricodes.append(new_team.tricode)
if new_team.name != "bye":
teams[new_team.id] = new_team
else:
new_match.teams[i] = "666"
if new_match.finished:
new_match.scores[i] = team["score"]
if team.get("winner"):
if team["winner"]:
new_match.winner = i
if not (new_match.finished and "bye" in new_match.teams):
match_list.append(new_match)
matches = sorted(match_list, key=lambda x: (x.finished, x.in_progress), reverse=True)
tournament = {"matches": matches, "teams": teams}
return tournament