def get_tournament_from_web_page(web_page):
    regions = ['LCK', 'LPL', 'LMS']
    url_parsed = urlparse(web_page)
    path = url_parsed.path
    # regex out the name of the tournament from the path
    p = re.compile('\/wiki\/(.+)\/Scoreboards')
    tournament_path = p.search(path).group(1)
    if 'League_Championship' in tournament_path:
        name, region, year, season = tournament_path.split('/')
        year = remove_season(year)
        season = remove_season(season)
        tournament = get_or_create(session, Tournament, name=name, region=region, year=int(year), season=season)
    elif any(region in tournament_path for region in regions):
        region, year, season = tournament_path.split('/')
        name = region
        year = remove_season(year)
        season = remove_season(season)
        tournament = get_or_create(session, Tournament, name=name, region=region, year=int(year), season=season)
    elif 'World_Championship' in tournament_path:
        tournament_split = tournament_path.split('_')
        year = tournament_split[0]
        name = '{}_{}'.format(tournament_split[2], tournament_split[3])
        region = 'ALL'
        season = tournament_split[2]
        tournament = get_or_create(session,Tournament, name=name, region=region, year=int(year), season=season)
    return tournament
def parse_team_game_info(color, game_info_table, game):
    # team = {'color': color, 'total_gold': 0, 'team_name': '', 'game_length_minutes': 0, 'won': None}
    team_stats = TeamStats(minions_killed=0, assists=0, deaths=0, kills=0, gold=0)
    session.add(team_stats)
    team_stats.color = color
    rows = game_info_table.find_all('tr')
    # row where team name is kept and how we determine the win vs the loss
    row_game_info = rows[1]
    cols = row_game_info.find_all('td')
    rows_game_stats_info = rows[3]
    cols_game_stats_info = rows_game_stats_info.find_all('td')
    # team['game_length_minutes'] = float(cols_game_stats_info[5].contents[0].strip().replace(':', '.').replace('!', '1'))
    game.game_length_minutes = float(cols_game_stats_info[5].contents[0].strip().replace(':', '.').replace('!', '1'))
    # cols[0] = blue team_name, cols[1] = blue win or loss, cols[3] = red team name, cols[2] = red won or losee
    if team_stats.color == 'red':
        team_name = cols[3].contents[0].strip()
        team_name = team_name.upper()
        team = get_or_create(session, Team, external_name=team_name)
        team.name = team_name
        team_stats.total_gold = float(cols_game_stats_info[10].contents[0].strip().replace('k', '')) * 1000
        team_stats.turrets = parse_column_stats(cols_game_stats_info[8])
        team_stats.dragons = parse_column_stats(cols_game_stats_info[7])
        team_stats.barons = parse_column_stats(cols_game_stats_info[6])
        try:
            if cols[2]['style'] == 'background-color:#ccffcc':
                team_stats.won = True
        except KeyError:
            team_stats.won = False
    elif team_stats.color == 'blue':
        team_name = cols[0].contents[0].strip()
        team_name = team_name.upper()
        team = get_or_create(session, Team, external_name=team_name)
        team.name = team_name
        team_stats.total_gold = float(cols_game_stats_info[0].contents[2].strip().replace('k', '')) * 1000
        team_stats.turrets = int(str(cols_game_stats_info[2].contents[2].strip()))
        team_stats.dragons = int(str(cols_game_stats_info[3].contents[2].strip()))
        team_stats.barons = int(str(cols_game_stats_info[4].contents[2].strip()))
        try:
            if cols[1]['style'] == 'background-color:#ccffcc':
                team_stats.won = True
        except KeyError:
            team_stats.won = False
    game.team_stats.append(team_stats)
    team.team_stats.append(team_stats)
    return team
def parse_player_stats(game, team, player_stat_table):
    rows = player_stat_table.find_all("tr")
    # assume len(row) == 1
    row = rows[0]
    cols = row.find_all('td')
    name = parse_column_player_name(cols[1])
    if session.query(Player).filter_by(name=name).first() is None:
        try:
            role, image = get_player_role_and_image(name, team)
            player = get_or_create(session, Player, name=name, role=role, image=image)
        except AttributeError:
            print('unable to find role and image for player {} from team {}'.format(name, team.name))
    player = get_or_create(session, Player, name=name)
    champion_played = parse_champion_name(cols[0])
    # Page was updated to include tokens, so the location of the stats were messed up, this handles both types of pages.
    try:
        gold = parse_gold_stat(cols[9])
        minions_killed = parse_column_stats(cols[10])
    except decimal.InvalidOperation:
        gold = parse_gold_stat(cols[10])
        minions_killed = parse_column_stats(cols[11])
    try:
        assists = parse_column_stats(cols[6])
        deaths = parse_column_stats(cols[5])
        kills = parse_column_stats(cols[4])
    except ValueError:
        assists = parse_column_stats(cols[7])
        deaths = parse_column_stats(cols[6])
        kills = parse_column_stats(cols[5])
    player_stat = PlayerStats(champion_played=champion_played, minions_killed=minions_killed, assists=assists,
                              deaths=deaths, kills=kills, gold=gold)
    # print('The col is {} and the stat is {}'.format(3, parse_column(cols[3])))
    session.add(player_stat)
    player.player_stats.append(player_stat)
    game.player_stats.append(player_stat)
    team.player_stats.append(player_stat)
    return player_stat