diff --git a/espncricinfo/player.py b/espncricinfo/player.py index 99b8f3c..25540a4 100644 --- a/espncricinfo/player.py +++ b/espncricinfo/player.py @@ -3,14 +3,16 @@ import dateparser from espncricinfo.exceptions import PlayerNotFoundError from espncricinfo.match import Match +import csv class Player(object): 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.parsed_html = self.get_html() - self.json = self.get_json() + self.parsed_html = self.get_html() + self.json = self.get_json() self.cricinfo_id = str(player_id) self.__unicode__ = self._full_name() self.name = self._name() @@ -96,3 +98,101 @@ def bowling_for_match(self, match_id): if stats: bowling_stats.append({ 'innings': innings, 'overs': next((x['value'] for x in stats['stats'] if x['name'] == 'overs')), 'maidens': next((x['value'] for x in stats['stats'] if x['name'] == 'maidens')), 'conceded': next((x['value'] for x in stats['stats'] if x['name'] == 'conceded')), 'wickets': next((x['value'] for x in stats['stats'] if x['name'] == 'wickets')), 'economy_rate': next((x['value'] for x in stats['stats'] if x['name'] == 'economyRate')), 'dots': next((x['value'] for x in stats['stats'] if x['name'] == 'dots'), None), 'fours_conceded': next((x['value'] for x in stats['stats'] if x['name'] == 'foursConceded'), None), 'sixes_conceded': next((x['value'] for x in stats['stats'] if x['name'] == 'sixesConceded'), None), 'wides': next((x['value'] for x in stats['stats'] if x['name'] == 'wides'), None), 'no_balls': next((x['value'] for x in stats['stats'] if x['name'] == 'noballs'), None)}) return bowling_stats + + def get_career_averages(self, file_name=None, match_format=11, data_type='allround') : + + """Get Player career averages + + Arguements: + file_name {string}: File name to save data + match_format {int}: Match format (default is 11) (1-Test), (2-Odi) (3-T20I), (11-All International), (20-Youth Tests), (21-Youth ODI) + data_type {string}: Data type (default is allround) (allround, batting, bowling, fielding) + + Return: + Data in csv file + """ + self.match_format = match_format + self.data_type = data_type + self.file_name = file_name + + if self.file_name is None: + self.file_name = f"{self.player_id}_{self.match_format}_{self.data_type}_career_averages.csv" + + self.url=f"https://stats.espncricinfo.com/ci/engine/player/{self.player_id}.html?class={self.match_format};template=results;type={self.data_type}" + html_doc = requests.get(self.url) + soup = BeautifulSoup(html_doc.text, 'html.parser') + tables = soup.find_all("table")[2] + table_rows = tables.find_all("tr") + scores =[] + for tr in table_rows: + scores.append(tr.text) + with open(self.file_name, "w") as csv_file: + writer = csv.writer(csv_file, delimiter=',') + for row in scores: + writer.writerow(row.splitlines()) + + def get_career_summary(self, file_name=None, match_format=11, data_type='allround'): + + """Get Player data match by match sorted by date + + Arguements: + file_name {string}: File name to save data + match_format {int}: Match format (default is 11) (1-Test), (2-Odi) (3-T20I), (11-All International), (20-Youth Tests), (21-Youth ODI) + data_type {string}: Data type (default is allround) (allround, batting, bowling, fielding) + + Return: + Data in csv file + """ + self.match_format = match_format + self.data_type = data_type + self.file_name = file_name + + if self.file_name is None: + self.file_name = f"{self.player_id}_{self.match_format}_{self.data_type}_career_summary.csv" + + self.url=f"https://stats.espncricinfo.com/ci/engine/player/{self.player_id}.html?class={self.match_format};template=results;type={self.data_type}" + html_doc = requests.get(self.url) + soup = BeautifulSoup(html_doc.text, 'html.parser') + tables = soup.find_all("table")[3] + table_rows = tables.find_all("tr") + scores =[] + for tr in table_rows: + scores.append(tr.text) + with open(self.file_name, "w") as csv_file: + writer = csv.writer(csv_file, delimiter=',') + for row in scores: + writer.writerow(row.splitlines()) + + def get_data(self, file_name=None, match_format=11, data_type='allround', view='match'): + + """Get Player data match by match sorted by date + + Arguements: + file_name {string}: File name to save data + match_format {int}: Match format (default is 11) (1-Test), (2-Odi) (3-T20I), (11-All International), (20-Youth Tests), (21-Youth ODI) + data_type {string}: Data type (default is allround) (allround, batting, bowling, fielding) + view {string}: View type (default is match) (match, innings, cumulative, reverse_cumulative, series, tour, ground) + + Return: + Data in csv file + """ + self.match_format = match_format + self.data_type = data_type + self.view = view + self.file_name = file_name + + if self.file_name is None: + self.file_name = f"{self.player_id}_{self.match_format}_{self.data_type}_{self.view}.csv" + + self.url=f"https://stats.espncricinfo.com/ci/engine/player/{self.player_id}.html?class={self.match_format};template=results;type={self.data_type};view={self.view}" + html_doc = requests.get(self.url) + soup = BeautifulSoup(html_doc.text, 'html.parser') + tables = soup.find_all("table")[3] + table_rows = tables.find_all("tr") + scores =[] + for tr in table_rows: + scores.append(tr.text) + with open(self.file_name, "w") as csv_file: + writer = csv.writer(csv_file, delimiter=',') + for row in scores: + writer.writerow(row.splitlines()) \ No newline at end of file