Exemplo n.º 1
0
def parse_shift(shift):
    """
    Parse shift for json
    
    :param shift: json for shift
    
    :return: dict with shift info
    """
    shift_dict = dict()

    name = shared.fix_name(' '.join([
        shift['firstName'].strip(' ').upper(),
        shift['lastName'].strip(' ').upper()
    ]))
    shift_dict['Player'] = name
    shift_dict['Player_Id'] = shift['playerId']
    shift_dict['Period'] = shift['period']
    shift_dict['Team'] = fix_team_tricode(shift['teamAbbrev'])

    # At the end of the json they list when all the goal events happened. They are the only one's which have their
    # eventDescription be not null
    if shift['eventDescription'] is None:
        shift_dict['Start'] = shared.convert_to_seconds(shift['startTime'])
        shift_dict['End'] = shared.convert_to_seconds(shift['endTime'])
        shift_dict['Duration'] = shared.convert_to_seconds(shift['duration'])
    else:
        shift_dict = dict()

    return shift_dict
Exemplo n.º 2
0
def analyze_shifts(shift, name, team, home_team, player_ids):
    """
    Analyze shifts for each player when using.
    Prior to this each player (in a dictionary) has a list with each entry being a shift.
    This function is only used for the html
    :param shift: info on shift
    :param name: player name
    :param team: 
    :param home_team:
    :param player_ids: dict with info on players
    :return: dict with info for shift
    """
    shifts = dict()

    shifts['Player'] = name.upper()
    shifts['Period'] = '4' if shift[1] == 'OT' else shift[1]
    shifts['Team'] = shared.TEAMS[team.strip(' ')]
    shifts['Start'] = shared.convert_to_seconds(shift[2].split('/')[0])
    shifts['End'] = shared.convert_to_seconds(shift[3].split('/')[0])
    shifts['Duration'] = shared.convert_to_seconds(shift[4].split('/')[0])

    try:
        if home_team == team:
            shifts['Player_Id'] = player_ids['Home'][name.upper()]['id']
        else:
            shifts['Player_Id'] = player_ids['Away'][name.upper()]['id']
    except KeyError:
        shifts['Player_Id'] = ''

    return shifts
Exemplo n.º 3
0
def parse_event(event):
    """
    Parses a single event when the info is in a json format

    :param event: json of event

    :return: dictionary with the info
    """
    play = dict()

    play['period'] = event['about']['period']
    play['event'] = str(change_event_name(event['result']['eventTypeId']))
    play['seconds_elapsed'] = shared.convert_to_seconds(event['about']['periodTime'])

    # If there's a players key that means an event occurred on the play.
    if 'players' in event.keys():
        play['p1_name'] = shared.fix_name(event['players'][0]['player']['fullName'])
        play['p1_ID'] = event['players'][0]['player']['id']

        for i in range(len(event['players'])):
            if event['players'][i]['playerType'] != 'Goalie':
                play['p{}_name'.format(i + 1)] = shared.fix_name(event['players'][i]['player']['fullName'].upper())
                play['p{}_ID'.format(i + 1)] = event['players'][i]['player']['id']

        # Coordinates aren't always there
        try:
            play['xC'] = event['coordinates']['x']
            play['yC'] = event['coordinates']['y']
        except KeyError:
            play['xC'] = ''
            play['yC'] = ''

    return play
Exemplo n.º 4
0
def parse_event(event):
    """
    Parses a single event when the info is in a json format
    :param event: json of event 
    :return: dictionary with the info
    """
    play = dict()

    play['period'] = event['about']['period']
    play['event'] = str(change_event_name(event['result']['eventTypeId']))
    play['seconds_elapsed'] = shared.convert_to_seconds(
        event['about']['periodTime'])

    # If there's a players key that means an event occurred on the play.
    if 'players' in event.keys():
        play['p1_name'] = shared.fix_name(
            event['players'][0]['player']['fullName'])
        play['p1_ID'] = event['players'][0]['player']['id']

        for i in range(len(event['players'])):
            if event['players'][i]['playerType'] != 'Goalie':
                play['p{}_name'.format(i + 1)] = shared.fix_name(
                    event['players'][i]['player']['fullName'].upper())
                play['p{}_ID'.format(i +
                                     1)] = event['players'][i]['player']['id']

        # Coordinates aren't always there
        try:
            play['xC'] = event['coordinates']['x']
            play['yC'] = event['coordinates']['y']
        except KeyError:
            play['xC'] = ''
            play['yC'] = ''
    """
    # Sometimes they record events for shots in the wrong zone (or maybe not)...so change it
    if play['xC'] != 'Na' and play['yC'] != 'Na':

        if play['Ev_Team'] == home_team:
            # X should be negative in 1st and 3rd for home_team
            if (play['Period'] == 1 or play['Period'] == 3) and play['xC'] > 0:
                play['xC'] = -int(play['xC'])
                play['yC'] = -int(play['yC'])
            elif play['Period'] == 2 and play['xC'] < 0:
                play['xC'] = -int(play['xC'])
                play['yC'] = -int(play['yC'])
        else:
            # X should be positive in 1st and 3rd for away_team
            if (play['Period'] == 1 or play['Period'] == 3) and play['xC'] < 0:
                play['xC'] = -int(play['xC'])
                play['yC'] = -int(play['yC'])
            elif play['Period'] == 2 and play['xC'] > 0:
                play['xC'] = -int(play['xC'])
                play['yC'] = -int(play['yC'])
    """

    return play
Exemplo n.º 5
0
def analyze_shifts(shift, name, team, home_team, player_ids):
    """
    Analyze shifts for each player when using.
    Prior to this each player (in a dictionary) has a list with each entry being a shift.

    :param shift: info on shift
    :param name: player name
    :param team: given team
    :param home_team: home team for given game
    :param player_ids: dict with info on players
    
    :return: dict with info for shift
    """
    shifts = dict()

    regex = re.compile('\d+')  # Used to check if something contains a number

    shifts['Player'] = name.upper()
    shifts['Period'] = '4' if shift[1] == 'OT' else shift[1]
    shifts['Team'] = shared.TEAMS[team.strip(' ')]
    shifts['Home_Team'] = shared.TEAMS[home_team.strip(' ')]
    shifts['Start'] = shared.convert_to_seconds(shift[2].split('/')[0])
    shifts['Duration'] = shared.convert_to_seconds(shift[4].split('/')[0])

    # I've had problems with this one...if there are no digits the time is f****d up
    if regex.findall(shift[3].split('/')[0]):
        shifts['End'] = shared.convert_to_seconds(shift[3].split('/')[0])
    else:
        shifts['End'] = shifts['Start'] + shifts['Duration']

    try:
        if home_team == team:
            shifts['Player_Id'] = player_ids['Home'][name.upper()]['id']
        else:
            shifts['Player_Id'] = player_ids['Away'][name.upper()]['id']
    except KeyError:
        shifts['Player_Id'] = ''

    return shifts
Exemplo n.º 6
0
def add_time(event_dict, event):
    """
    Fill in time and seconds elapsed
    
    :param event_dict: dict of parsed event stuff
    :param event: event info from pbp
    
    :return: None
    """
    event_dict['Time_Elapsed'] = str(event[3])

    if event[3] != '':
        event_dict['Seconds_Elapsed'] = shared.convert_to_seconds(event[3])
    else:
        event_dict['Seconds_Elapsed'] = ''
Exemplo n.º 7
0
def parse_event(event):
    """
    Parse each event
    In the string each field is separated by a '~'. 
    Relevant for here: The first two are the x and y coordinates. And the 4th and 5th are the time elapsed and period.
    :param event: string with info
    :return: return dict with relevant info
    """
    info = dict()
    fields = event.split('~')

    # Shootouts screw everything up so don't bother...coordinates don't matter here either way
    if fields[4] == '5':
        return None

    info['xC'] = fields[0]
    info['yC'] = fields[1]
    info['time_elapsed'] = shared.convert_to_seconds(fields[3])
    info['period'] = fields[4]
    info['event'] = event_type(fields[8].upper())

    return info
Exemplo n.º 8
0
def parse_event(event, players, home_team, if_plays_in_json, current_score):
    """
    Receievs an event and parses it
    :param event: 
    :param players: players in game
    :param home_team:
    :param if_plays_in_json: If the pbp json contains the plays
    :param current_score: current score for both teams
    :return: dict with info
    """
    event_dict = dict()

    away_players = event[6]
    home_players = event[7]

    try:
        event_dict['Period'] = int(event[1])
    except ValueError:
        event_dict['Period'] = 0

    event_dict['Description'] = event[5]
    event_dict['Event'] = str(event[4])

    if event_dict['Event'] in [
            'GOAL', 'SHOT', 'MISS', 'BLOCK', 'PENL', 'FAC', 'HIT', 'TAKE',
            'GIVE'
    ]:
        event_dict['Ev_Team'] = event[5].split(
        )[0]  # Split the description and take the first thing (which is the team)

    # If it's a goal change the score
    if event[4] == 'GOAL':
        if event_dict['Ev_Team'] == home_team:
            current_score['Home'] += 1
        else:
            current_score['Away'] += 1

    event_dict['Home_Score'] = current_score['Home']
    event_dict['Away_Score'] = current_score['Away']

    # Populate away and home player info
    for j in range(6):
        try:
            name = shared.fix_name(away_players[j][0].upper())
            event_dict['awayPlayer{}'.format(j + 1)] = name
            event_dict['awayPlayer{}_id'.format(
                j + 1)] = players['Away'][name]['id']
        except KeyError:
            event_dict['awayPlayer{}_id'.format(j + 1)] = 'NA'
        except IndexError:
            event_dict['awayPlayer{}'.format(j + 1)] = ''
            event_dict['awayPlayer{}_id'.format(j + 1)] = ''

        try:
            name = shared.fix_name(home_players[j][0].upper())
            event_dict['homePlayer{}'.format(j + 1)] = name
            event_dict['homePlayer{}_id'.format(
                j + 1)] = players['Home'][name]['id']
        except KeyError:
            event_dict['homePlayer{}_id'.format(j + 1)] = 'NA'
        except IndexError:
            event_dict['homePlayer{}'.format(j + 1)] = ''
            event_dict['homePlayer{}_id'.format(j + 1)] = ''

    # Did this because above method assumes the goalie is at end of player list
    for x in away_players:
        if x[2] == 'G':
            event_dict['Away_Goalie'] = shared.fix_name(x[0].upper())
            try:
                event_dict['Away_Goalie_Id'] = players['Away'][
                    event_dict['Away_Goalie']]['id']
            except KeyError:
                event_dict['Away_Goalie_Id'] = 'NA'
        else:
            event_dict['Away_Goalie'] = ''
            event_dict['Away_Goalie_Id'] = ''

    for x in home_players:
        if x[2] == 'G':
            event_dict['Home_Goalie'] = shared.fix_name(x[0].upper())
            try:
                event_dict['Home_Goalie_Id'] = players['Home'][
                    event_dict['Home_Goalie']]['id']
            except KeyError:
                event_dict['Home_Goalie_Id'] = 'NA'
        else:
            event_dict['Home_Goalie'] = ''
            event_dict['Home_Goalie_Id'] = ''

    event_dict['Away_Players'] = len(away_players)
    event_dict['Home_Players'] = len(home_players)

    try:
        home_skaters = event_dict['Home_Players'] - 1 if event_dict[
            'Home_Goalie'] != '' else len(home_players)
        away_skaters = event_dict['Away_Players'] - 1 if event_dict[
            'Away_Goalie'] != '' else len(away_players)
    except KeyError:
        # Getting a key error here means that home/away goalie isn't there..which means home/away players are empty
        home_skaters = 0
        away_skaters = 0

    event_dict['Strength'] = 'x'.join([str(home_skaters), str(away_skaters)])
    event_dict['Ev_Zone'] = which_zone(event[5])

    if 'PENL' in event[4]:
        event_dict['Type'] = get_penalty(event[5])
    else:
        event_dict['Type'] = shot_type(event[5]).upper()

    event_dict['Time_Elapsed'] = str(event[3])

    if event[3] != '':
        event_dict['Seconds_Elapsed'] = shared.convert_to_seconds(event[3])
    else:
        event_dict['Seconds_Elapsed'] = ''

    # I like getting the event players from the json
    if not if_plays_in_json:
        if event_dict['Event'] in [
                'GOAL', 'SHOT', 'MISS', 'BLOCK', 'PENL', 'FAC', 'HIT', 'TAKE',
                'GIVE'
        ]:
            event_dict.update(get_event_players(
                event, players, home_team))  # Add players involves in event

    return [event_dict, current_score]