Skip to content

Commit

Permalink
Added mapping to convert new tri-codes to old ones. See utils/tri_cod…
Browse files Browse the repository at this point in the history
…e_conversion.json for relevant tri-codes.
  • Loading branch information
HarryShomer committed Oct 25, 2021
1 parent 8f8f227 commit b7ac482
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@ v1.37
* Only print full error summary when the number of games scraped is >= 25
* Remove hardcoded exception for Sebastian Aho. Updated process to work without it.
* Always rescrape schedule pages

v1.3.8
------
* Convert tri-codes from new format to old in Html PBP. Mappings stored in utils/tri_code_conversion.json.
16 changes: 9 additions & 7 deletions hockey_scraper/nhl/pbp/html_pbp.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,16 +240,17 @@ def add_strength(event_dict, home_players, away_players):

def add_event_team(event_dict, event):
"""
Add event team for event
Add event team for event.
Always first thing in description
:param event_dict: dict of event info
:param event: list with parsed event info
:return: None
"""
if event_dict['Event'] in ['GOAL', 'SHOT', 'MISS', 'BLOCK', 'PENL', 'FAC', 'HIT', 'TAKE', 'GIVE']:
# Split the description and take the first thing (which is the team)
event_dict['Ev_Team'] = event[5].split()[0]
event_dict['Ev_Team'] = shared.convert_tricode(event[5].split()[0])
else:
event_dict['Ev_Team'] = ''

Expand Down Expand Up @@ -355,12 +356,13 @@ def get_player_name(number, players, team, home_team):
:param number: player's number
:param players: all players with info
:param team: team of player
:param home_team: home team
:param team: team of player listed in html
:param home_team: home team defined b4 hand (from json)
:return: dict with full and and id
"""
player = None
team = shared.convert_tricode(team) # Needed to convert from new format to old
venue = "Home" if team == home_team else "Away"

for name in players[venue]:
Expand Down Expand Up @@ -644,9 +646,9 @@ def add_event_players(event_dict, event, players, home_team):
:return: None
"""
description = event[5].strip()
ev_team = description.split()[0]
event_info = {}
description = event[5].strip()
ev_team = shared.convert_tricode(description.split()[0])

if event[4] == 'FAC':
event_info = parse_fac(description, players, ev_team, home_team)
Expand Down
11 changes: 11 additions & 0 deletions hockey_scraper/utils/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
with open(os.path.join(FILE_DIR, "team_tri_codes.json"), "r" ,encoding="utf-8") as f:
TEAMS = json.load(f)['teams']

with open(os.path.join(FILE_DIR, "tri_code_conversion.json"), "r" ,encoding="utf-8") as f:
TRI_CODES = json.load(f)['tri_codes']


def fix_name(name):
Expand All @@ -44,6 +46,15 @@ def get_team(team):
return TEAMS.get(team.upper(), team.upper()).upper()


def convert_tricode(tri):
"""
Convert the tri-code if found in 'tri_code_conversion.json'
:return Fixed tri-code or original
"""
return TRI_CODES.get(tri.upper(), tri.upper()).upper()


def custom_formatwarning(msg, *args, **kwargs):
"""
Override format for standard wanings
Expand Down
10 changes: 10 additions & 0 deletions hockey_scraper/utils/tri_code_conversion.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"_description": "Conversion of new tri-code to old",

"tri_codes": {
"NJD": "N.J",
"TBL": "T.B",
"LAK": "L.A",
"SJS": "S.J"
}
}
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def read():

setup(
name='hockey_scraper',
version='1.37.9',
version='1.38',
description="""Python Package for scraping NHL Play-by-Play and Shift data.""",
long_description=read(),
classifiers=[
Expand All @@ -34,3 +34,4 @@ def read():
# ],
# }
)

0 comments on commit b7ac482

Please sign in to comment.