def load_salaries_positions_for_date_with_filepath(self, date, filepath): print(f'Filepath {filepath}') with open(filepath, 'r') as f: reader = csv.reader(f) next(reader, None) for row in reader: name = self._name_from_row(row) pos, sal = self._position_salary_from_row(row) team_abbrv = self._team_abbrv_from_row(row) player_site_id = self._player_site_id_from_row(row) player = actor.find_player_by_exact_name(name) team = actor.find_team_by_abbrv(team_abbrv) game = actor.find_game_by_date_and_team(date, team['id']) if not player or not game: import pdb pdb.set_trace() stat_line = actor.find_stat_line_by_player_and_game( player['id'], game['id']) if stat_line: actor.update_stat_line_position_salary( self.site_abbrv, stat_line['id'], pos, sal, player_site_id) else: print( f'No existing stat_line for {name}. Creating one now.') actor.create_stat_line_with_position_salary( self.site_abbrv, player['id'], team['id'], game['id'], pos, sal, player_site_id)
def load_games_for_season(season): _, end_year = season.split('-') year = int(f'20{end_year}') directory = f"{games_base_directory}/{season}" for month_num in range(1, 13): filepath = f"{directory}/{f'{month_num:02}'}.html" with open(filepath, 'r') as f: page = f.read() tree = html.fromstring(page) game_infos = tree.xpath( '//table[@id="schedule"]//tbody//tr[not(contains(@class, "thead"))]' ) date_format = '%a, %b %d, %Y' start_time_format = '%I%p' for game_info in game_infos: date_string = game_info[0].xpath('./a')[0].text date = datetime.datetime.strptime(date_string, date_format) start_time = game_info[1].text better_start_time = f'{date_string} {start_time.upper()}M' start_time_format = f'{date_format} %I:%M%p' start_time = datetime.datetime.strptime( better_start_time, start_time_format) away_team_name = game_info[2].xpath('./a')[0].text home_team_name = game_info[4].xpath('./a')[0].text home_team = actor.find_team_by_name(home_team_name) away_team = actor.find_team_by_name(away_team_name) game = actor.find_game_by_date_and_team(date, away_team['id']) if not game: actor.create_game(date, home_team['id'], away_team['id'], season, start_time) else: actor.update_game(game['id'], date, home_team['id'], away_team['id'], season, start_time)
def load_stat_lines_on_date(date): logger.info(f'Loading stat_lines on {date}') for stat_line in loop_stat_lines_on_date(date): logger.debug(f'{stat_line}') stat_dict = dict(zip(stat_csv_keys, stat_line)) player_name = stat_dict.pop('name') player = actor.find_player_by_br_name(player_name) team_abbrv = stat_dict.pop('team') team = actor.find_team_by_site_abbrv('br', team_abbrv) minutes = stat_dict.pop('minutes_numeric') active = True if stat_dict.pop('active') == 'True' else False game = actor.find_game_by_date_and_team(date, team['id']) if not player or not game: import pdb pdb.set_trace() continue stat_line = actor.find_stat_line_by_player_and_game( player['id'], game['id']) dk_points = stat_dict.pop('dk_points') fd_points = stat_dict.pop('fd_points') actor.create_or_update_stat_line_with_stats(player['id'], team['id'], game['id'], dk_points, fd_points, stat_dict, minutes, active=active)
def find_game_from_box_score(date, box_score_html): teams = box_score_html.xpath('//div[@itemprop="performer"]/strong/a') away_team_name = teams[0].text home_team_name = teams[1].text away_team = actor.find_team_by_name(away_team_name) home_team = actor.find_team_by_name(home_team_name) game = actor.find_game_by_date_and_team(date, away_team['id']) return game, away_team, home_team
def update_stat_line(date, site_abbrv, row): name, sal, team_abbrv, pos = row[:4] player = actor.find_player_by_exact_name(name.strip()) team = actor.find_team_by_site_abbrv('rg', team_abbrv) game = actor.find_game_by_date_and_team(date, team['id']) if not player or not game: logger.info(f'No player {name} or game on {date}') return stat_line = actor.find_stat_line_by_player_and_game( player['id'], game['id']) if stat_line: actor.update_stat_line_position_salary(site_abbrv, stat_line['id'], pos, sal)
def load_projections_from_file(site_abbrv, date, filepath): logger.info(f'Loading projections from {filepath}') with open(filepath, 'r') as f: rows = csv.DictReader(f) for row in rows: name = row['Player Name'] logger.debug(f'Player Name: {name}') team_abbrv = row['Team'] player = actor.find_player_by_exact_name(name) team = actor.find_team_by_site_abbrv('dfn', team_abbrv) game = actor.find_game_by_date_and_team(date, team['id']) logger.debug( f"DFN Projection -- Name: {name}; Team: {team['dfn_abbrv']}") bulk = {} if not player or not game: import pdb pdb.set_trace() logging.warning( f'No player found for DFN with name {name}. Continuing') continue stat_line = actor.find_stat_line_by_player_and_game( player['id'], game['id']) if not stat_line: logger.warning(f'No stat_line for {name} on {date}') continue minutes = row['Proj Min'] version = '0.1-dfn' if site_abbrv == 'fd': fd_points = row['S FP'] fdpp36 = None actor.create_or_update_fd_projection(stat_line['id'], source, bulk=bulk, minutes=minutes, fd_points=fd_points, fdpp36=fdpp36, version=version) else: dk_points = row['S FP'] dkpp36 = None actor.create_or_update_dk_projection(stat_line['id'], source, bulk=bulk, minutes=minutes, dk_points=dk_points, dkpp36=dkpp36, version=version)
def _get_player_information_from_dict(pinfo, date, site_abbrv): player_name = pinfo['n'] team_abbrv = pinfo['t'] proj_minutes = pinfo['dfnMin'] proj_points = pinfo['dfn'] status = pinfo['is'] if 'is' in pinfo else None bulk = pinfo player = actor.find_player_by_exact_name(player_name) team = actor.find_team_by_site_abbrv('dfn', team_abbrv) game = actor.find_game_by_date_and_team(date, team['id']) if not player or not game: import pdb pdb.set_trace() logging.warning( f'No player found by DFN with name {player_name}. Continuing') return stat_line = actor.find_stat_line_by_player_and_game( player['id'], game['id']) if not stat_line: logger.warning( f'No stat_line found by DFN for {player_name} on {date}') return version = '0.1-dfn' logger.debug(f'Creating or updating DFN projection for {player_name}') if site_abbrv == 'fd': fd_points = proj_points fdpp36 = None actor.create_or_update_fd_projection(stat_line['id'], source, bulk=bulk, minutes=proj_minutes, fd_points=fd_points, fdpp36=fdpp36, version=version, status=status) else: dk_points = proj_points dkpp36 = None actor.create_or_update_dk_projection(stat_line['id'], source, bulk=bulk, minutes=proj_minutes, dk_points=dk_points, dkpp36=dkpp36, version=version, status=status)
def load_player_info_for_date(date): player_info = _get_player_info_for_date(date) # need the teams first team_info = player_info['teams'] team_abbrvs = _get_team_abbrvs_by_id(team_info) players = player_info['players'] for player_ in players: first_name = player_['first_name'] last_name = player_['last_name'] fullname = f'{first_name} {last_name}'.strip() sal = player_['salary'] try: pos = player_['position'] except: import pdb pdb.set_trace() asdf = 5 # find team player_slate_id = player_['id'] team_id = player_['team']['_members'][0] team_abbrv = team_abbrvs[team_id] logger.info(f'{fullname}, {team_abbrv}, {pos}, {sal}') player = actor.find_player_by_exact_name(fullname) team = actor.find_team_by_abbrv(team_abbrv) game = actor.find_game_by_date_and_team(date, team['id']) if not player or not game: import pdb pdb.set_trace() continue stat_line = actor.find_stat_line_by_player_and_game( player['id'], game['id']) if stat_line: actor.update_stat_line_position_salary('fd', stat_line['id'], pos, sal, player_slate_id) else: logger.info( f'No existing stat_line for {player["fd_name"]}. Creating one now.' ) actor.create_stat_line_with_position_salary( 'fd', player['id'], team['id'], game['id'], pos, sal, player_slate_id) asdf = 5
def _get_game_from_date_and_cell(date, cell): home_team_abbrv = cell.xpath('.//a[@class="black"]')[3].text home_team = actor.find_team_by_site_abbrv('vi', home_team_abbrv) game = actor.find_game_by_date_and_team(date, home_team['id']) return game