Skip to content

Commit

Permalink
Merge pull request #62 from JackMa222/new-player-endpoint
Browse files Browse the repository at this point in the history
New player endpoint for major_teams
  • Loading branch information
dwillis authored Jan 13, 2024
2 parents 8613177 + 2c09fee commit 80878bb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion espncricinfo/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ class NoSeriesError(TypeError):
"""
Exception raised if a series_id is not valid or does not exist.
"""
pass
pass
15 changes: 11 additions & 4 deletions espncricinfo/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ def __init__(self, player_id):
self.player_id=player_id
self.url = "https://www.espncricinfo.com/player/player-name-{0}".format(str(player_id))
self.json_url = "http://core.espnuk.org/v2/sports/cricket/athletes/{0}".format(str(player_id))
self.new_json_url = "https://hs-consumer-api.espncricinfo.com/v1/pages/player/home?playerId={0}".format(str(player_id))
self.parsed_html = self.get_html()
self.json = self.get_json()
self.new_json = self.get_new_json()
self.cricinfo_id = str(player_id)
self.__unicode__ = self._full_name()
self.name = self._name()
Expand All @@ -23,9 +25,7 @@ def __init__(self, player_id):
self.playing_role = self._playing_role()
self.batting_style = self._batting_style()
self.bowling_style = self._bowling_style()

if self.parsed_html:
self.major_teams = self._major_teams()
self.major_teams = self._major_teams()

def get_html(self):
r = requests.get(self.url)
Expand All @@ -40,6 +40,13 @@ def get_json(self):
raise PlayerNotFoundError
else:
return r.json()

def get_new_json(self):
r = requests.get(self.new_json_url, headers={'user-agent': 'Mozilla/5.0'})
if r.status_code == 404:
raise PlayerNotFoundError
else:
return r.json()

def _name(self):
return self.json['name']
Expand All @@ -63,7 +70,7 @@ def _current_age(self):
return self.json['age']

def _major_teams(self):
return [x.text for x in self.parsed_html.find('div', class_='overview-teams-grid').find_all('h5')]
return [x['team']['longName'] for x in self.new_json['content']['teams']]

def _playing_role(self):
return self.json['position']
Expand Down

0 comments on commit 80878bb

Please sign in to comment.