/
competition.py
52 lines (44 loc) · 1.72 KB
/
competition.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
import requests
from .fixture import Fixture
from .table import Table
from .team import Team
from ..utils import headers
class Competition():
"""
The Competition class.
"""
def __init__(self, competition):
self._competition_url = competition["_links"]["self"]["href"]
self._fixtures_url = competition["_links"]["fixtures"]["href"]
self._league_table_url = competition["_links"]["leagueTable"]["href"]
self._teams_url = competition["_links"]["teams"]["href"]
self.current_matchday = competition["currentMatchday"]
self.id = competition["id"]
self.last_update = competition["lastUpdated"]
self.name = competition["caption"]
self.number_games = competition["numberOfGames"]
self.number_matchdays = competition["numberOfMatchdays"]
self.number_teams = competition["numberOfTeams"]
self.shortname = competition["league"]
self.year = competition["year"]
def fixtures(self):
"""
Returns all current fixtures of the competition.
"""
response = requests.get(self._fixtures_url, headers=headers()).json()
return [Fixture(fixture) for fixture in response["fixtures"]]
def teams(self):
"""
Returns all teams currently participating in the competition.
"""
response = requests.get(self._teams_url, headers=headers()).json()
return [Team(team) for team in response["teams"]]
def table(self):
"""
Returns the current league table of the competition.
"""
response = requests.get(
self._league_table_url, headers=headers()).json()
return Table(response)
def __str__(self):
return self.name