/
decoders.py
255 lines (191 loc) · 7.32 KB
/
decoders.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import collections
from typing import Union, List, Dict
from model import Player, Fixture, Result, TeamForm, Standing, Competition, HeadToHead, LeagueTable, Team
def to_competition(json_dict: dict) -> Competition:
"""
Converts json dictionary into Competition object
:param json_dict:
:return:
"""
if json_dict is None:
return None
result = Competition(json_dict['id'], json_dict['caption'], json_dict['year'])
return result
def to_competitions(json_dict: dict) -> List[Competition]:
"""
Converts json dictionary to list of Competition objects
:param json_dict:
:return:
"""
if json_dict is None:
return None
result = [to_competition(competition_json) for competition_json in json_dict]
return result
def to_team(json_dict: dict) -> Team:
"""
Converts json dictionary to Team object
:param json_dict:
:return: Team
"""
if json_dict is None:
return None
team_link = json_dict['_links']['self']['href']
team_id = team_link[team_link.rfind('/') + 1:]
result = Team(team_id, json_dict['name'], json_dict['shortName'], json_dict['squadMarketValue'],
json_dict['crestUrl'])
return result
def to_player(json_dict: dict) -> Player:
"""
Converts json dictionary to Player object
:param json_dict:
:return:
"""
if json_dict is None:
return None
result = Player(json_dict['name'], json_dict['position'], json_dict['jerseyNumber'], json_dict['dateOfBirth'],
json_dict['nationality'], json_dict['contractUntil'], json_dict['marketValue'])
return result
def to_fixture(json_dict: dict) -> Fixture:
"""
Converts json dictionary to Fixture object
:param json_dict:
:return:
"""
if json_dict is None:
return None
# Get fixture
fixture_url = json_dict['_links']['self']['href']
fixture_id = fixture_url[fixture_url.rfind('/') + 1:]
# Get competition id
competition_url = json_dict['_links']['competition']['href']
competition_id = competition_url[competition_url.rfind('/') + 1:]
# Get home team data
home_team_url = json_dict['_links']['homeTeam']['href']
home_team_id = home_team_url[home_team_url.rfind('/') + 1:]
# Get away team data
away_team_url = json_dict['_links']['awayTeam']['href']
away_team_id = away_team_url[away_team_url.rfind('/') + 1:]
# Get result object
fixture_result = to_result(json_dict['result'])
fixture = Fixture(fixture_id, competition_id, json_dict['date'], json_dict['status'], json_dict['homeTeamName'],
home_team_id, json_dict['awayTeamName'], away_team_id, fixture_result, None,
json_dict['matchday'])
return fixture
def to_result(json_dict: dict) -> Result:
"""
Converts json dictionary to Result object
:param json_dict:
:return:
"""
if json_dict is None:
return None
result = Result(json_dict['goalsHomeTeam'], json_dict['goalsAwayTeam'], None, None)
if 'halfTime' in json_dict:
result.halftime_home_team_goals = json_dict['halfTime']['goalsHomeTeam']
result.halftime_away_team_goals = json_dict['halfTime']['goalsAwayTeam']
return result
def to_teamform(json_dict: dict) -> TeamForm:
"""
Converts json dictionary into TeamForm object
:param json_dict:
:return:
"""
if json_dict is None:
return None
result = TeamForm(json_dict['wins'], json_dict['draws'], json_dict['losses'], json_dict['goals'],
json_dict['goalsAgainst'])
return result
def to_headtohead(json_dict: dict) -> HeadToHead:
"""
Converts json dictionary to HeadToHead object
:param json_dict:
:return:
"""
if json_dict is None:
return None
last_home_win_home_team = to_fixture(json_dict['lastHomeWinHomeTeam'])
last_away_win_away_team = to_fixture(json_dict['lastAwayWinAwayTeam'])
last_win_home_team = to_fixture(json_dict['lastWinHomeTeam'])
last_win_away_team = to_fixture(json_dict['lastWinAwayTeam'])
fixtures = [to_fixture(fixture_json) for fixture_json in json_dict['fixtures']]
head2head = HeadToHead(json_dict['count'],
json_dict['timeFrameStart'],
json_dict['timeFrameEnd'],
json_dict['homeTeamWins'],
json_dict['awayTeamWins'],
json_dict['draws'],
last_home_win_home_team,
last_win_home_team,
last_away_win_away_team,
last_win_away_team,
fixtures)
return head2head
def to_leaguetable_standing(json_dict: dict) -> Standing:
"""
Converts json dictionary to Standing object
:param json_dict:
:return:
"""
if json_dict is None:
return None
team_url = json_dict['_links']['team']['href']
team_id = team_url[team_url.rfind("/") + 1:]
team_form = to_teamform(json_dict)
home_form = to_teamform(json_dict['home'])
away_form = to_teamform(json_dict['away'])
standing = Standing(team_id, json_dict['teamName'], json_dict['position'], json_dict['playedGames'],
json_dict['goals'], json_dict['points'], team_form, home_form, away_form)
return standing
def to_league_table(json_dict: dict) -> Union[LeagueTable, Dict[str, LeagueTable]]:
"""
Converts json dictionary to LeagueTable object, or list of LeagueTable objects depending on the contents
of json_dict
:param json_dict:
:return:
"""
if json_dict is None:
return None
if 'standings' in json_dict:
league_tables = to_cup_league_tables(json_dict)
return league_tables
else:
standings = [to_leaguetable_standing(standing_json) for standing_json in json_dict['standing']]
result = LeagueTable(standings)
return result
def to_cup_league_tables(json_dict: dict) -> Dict[str, LeagueTable]:
"""
Converts json dictionary to ordered dictionary of league tables.
Dictionary will be ordered by name
:param json_dict:
"""
league_dict = {} # type: Dict[str, LeagueTable]
for league in json_dict['standings']:
standings = [to_cup_standing(standing_json) for standing_json in json_dict['standings'][league]]
league_table = LeagueTable(standings)
league_dict[league] = league_table
# Order league tables by name
result = collections.OrderedDict(sorted(league_dict.items()))
return result
def to_cup_standing(json_dict: dict) -> Standing:
"""
Converts json dictionary to Standing object
:param json_dict:
:return:
"""
if json_dict is None:
return None
team_form = to_cup_teamform(json_dict)
standing = Standing(json_dict['teamId'], json_dict['team'], json_dict['rank'], json_dict['playedGames'],
json_dict['goals'], json_dict['points'], team_form, None, None)
return standing
def to_cup_teamform(json_dict: dict) -> TeamForm:
"""
Converts json dictionary into TeamForm object
:param json_dict:
:return:
"""
if json_dict is None:
return None
result = TeamForm(None, None, None, json_dict['goals'],
json_dict['goalsAgainst'])
return result