-
Notifications
You must be signed in to change notification settings - Fork 1
/
tournamenter.py
148 lines (106 loc) · 3.93 KB
/
tournamenter.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python
import requests
import json
class Tournamenter(object):
teams = {}
url = ''
password = ''
session = None
def __init__(self, url, password=''):
self.url = url
self.password = password
self.session = requests.Session()
if password != '':
if not (self.login(password)):
raise ValueError("Wrong password or URL")
def login(self, password):
r = self.session.post(self.url + '/login', {'password': password})
return False if 'Wrong Password' in r.content else True
def add_team(self, name, country):
r = self.session.post(self.url + '/teams',
json.dumps({
"name": name,
"country": country}
),
headers={'content-type': 'application/json'})
if r.status_code != 201:
r.content
return r.json()
def find_group_id(self, name):
r = self.session.post(self.url + '/groups/find',
json.dumps({
"name": name
}),
headers={'content-type': 'application/json'})
out = r.json()
if len(out) == 0:
return None
return out[0]['id']
def load_teams(self):
r = self.session.get(self.url + '/teams')
for item in r.json():
self.teams[item['name']] = item
def team_info(self, name):
if not (name in self.teams):
self.load_teams()
return self.teams[name]
def add_match(self, teamA, teamB, day, field, time, group,
state="scheduled"):
if type(teamA) != int:
teamA = self.team_info(teamA)['id']
if type(teamB) != int:
teamB = self.team_info(teamB)['id']
if type(group) != int:
group = self.find_group_id(group)
r = self.session.post(self.url + '/matches', json.dumps({
'teamAId': teamA,
'teamBId': teamB,
'groupId': group,
'day': day,
'field': field,
'hour': time,
'state': state
}), headers={'content-type': 'application/json'})
if r.status_code != 201:
return r.content
return r.json()
def find_match_id(self, teamA, teamB, field, day):
if type(teamA) != int:
teamA = self.team_info(teamA)['id']
if type(teamB) != int:
teamB = self.team_info(teamB)['id']
r = self.session.post(self.url + '/matches/find', json.dumps({
'teamAId': teamA,
'teamBId': teamB,
'field': field,
'day': day
}), headers={'content-type': 'application/json'})
out = r.json()
if len(out) == 0:
return None
return r.json()[0]['id']
def update_match(self, teamA, teamB, field, day, teamAScore, teamBScore,
state="scheduled"):
if type(teamA) != int:
teamA = self.team_info(teamA)['id']
if type(teamB) != int:
teamB = self.team_info(teamB)['id']
id = self.find_match_id(teamA, teamB, field, day)
r = self.session.post(self.url + '/matches/update', json.dumps({
'teamAId': teamA,
'teamBId': teamB,
'field': field,
'day': day,
'teamAScore': teamAScore,
'teamBScore': teamBScore,
'id': id,
'state': state
}), headers={'content-type': 'application/json'})
if r.status_code != 200:
return r.content
return r.json()
if __name__ == "__main__":
t = Tournamenter('http://localhost:1337', '12345')
t.add_match("Brazil", "Argentina", "1", "A", "12:00", 'testing group')
print(t.find_match_id("Brazil", "Argentina", "A", 1))
t.update_match("Brazil", "Argentina", "A", 1, 5, 4)