outside-edge / python-espncricinfo

Python wrapper for the ESPNCricInfo JSON API
MIT License
146 stars 64 forks source link

Player object fix #61

Closed JackMa222 closed 8 months ago

JackMa222 commented 8 months ago

Player Object Update - Major Teams (_major_teams)

This attempts to fix issue [BUG] Player class is not working with Python 3.11.0 #59

The ESPN CricInfo html page has been updated with class and syntax changes which mean the previous _major_teams function did not work:

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

The JSON for each player provides a list of their teams through links with links like http://core.espnuk.org/v2/sports/cricket/teams/2

I have written an updated function that visits gets each of these json files, gets the team name and appends the names to a list which is then returned

def _major_teams(self):
        teams = []
        for x in self.json['majorTeams']:
            r = requests.get(x['$ref'])
            if r.status_code == 404:
                raise TeamNotFoundError
            else:
                teams.append(r.json()['name'])
        return teams

I also wrote a new error in exceptions.py to handle the 404 HTTP errors - TeamNotFoundError

Time consideration

Unfortunately, due to the manner in which this function individually requests each team page, the function is considerably slower the before. Further players with a large amount of teams, eg. Steve Smith take up ~16 seconds. This seriosuly affects the usage of the function. For this reason there may be another way to get the major teams, for example seperate this function from the rest.

JackMa222 commented 8 months ago

Appears to be a better way (as always) - currently working on. Team info appears inside the json at the bottom of the HTML inside the script <script id="__NEXT_DATA__" type="application/json">. How we access this via API endpoints I am unsure at the moment.

JackMa222 commented 8 months ago

I will close this request and open a new PR with a rewrite of the player class.