Exemple #1
0
def add_vegas_insider():
    total_sheet_data = []
    browser.get('https://www.vegasinsider.com/college-basketball/stats/iskoe-spreadsheet/')
    season_name = browser.find_element_by_class_name('viHeaderNorm').text
    season_name = season_name.split(' ')[0]
    tr_list = browser.find_element_by_class_name('viBodyBorderNorm').find_elements_by_tag_name('tr')
    for index, tr in enumerate(tr_list):
        if index <= 3:
            continue
        td_list = tr.find_elements_by_tag_name('td')
        try:
            team_name = td_list[1].find_element_by_class_name('tableText').get_attribute('innerHTML').strip()
        except NoSuchElementException:
            team_name = td_list[1].get_attribute('innerHTML').strip()
        # print(team_name)
        if action_type <= 1:
            team1 = session.query(NCAATeam).filter(or_(NCAATeam.ShortTeamName == team_name,
                                                       NCAATeam.FullTeamName == team_name)).first()
            if not team1:
                team1 = NCAATeam(team_name)
                session.add(team1)
                session.flush()
            db_record = session.query(VegasInsider).filter(VegasInsider.Season == season_name,
                                                           VegasInsider.TeamID == team1.ID).first()
            new_vegasinsider = VegasInsider(season_name, team1.ID, td_list[0].text,
                                            td_list[2].text, td_list[3].text, td_list[4].text, td_list[5].text,
                                            td_list[6].text, td_list[7].text, td_list[8].text, td_list[9].text,
                                            td_list[10].text, td_list[11].text, td_list[12].text,
                                            td_list[13].text,
                                            td_list[14].text, td_list[15].text, td_list[16].text,
                                            td_list[17].text,
                                            td_list[18].text, td_list[19].text, td_list[20].text,
                                            td_list[21].text,
                                            td_list[22].text, td_list[23].text)
            if not db_record:
                session.add(new_vegasinsider)
                if action_type == 0:
                    total_sheet_data.append((season_name, team1.ShortTeamName, team1.FullTeamName, td_list[0].text,
                                            td_list[2].text, td_list[3].text, td_list[4].text, td_list[5].text,
                                            td_list[6].text, td_list[7].text, td_list[8].text, td_list[9].text,
                                            td_list[10].text, td_list[11].text, td_list[12].text,
                                            td_list[13].text,
                                            td_list[14].text, td_list[15].text, td_list[16].text,
                                            td_list[17].text,
                                            td_list[18].text, td_list[19].text, td_list[20].text,
                                            td_list[21].text,
                                            td_list[22].text, td_list[23].text))
            else:
                new_vegasinsider.ID = db_record.ID
                session.merge(new_vegasinsider)
        if action_type == 2:
            team_formula_str = 'OR(SUBSTITUTE({' + ncaa_team_info[0] + '}, "\'", " ")="' + \
                               team_name.replace("'", " ") + '", SUBSTITUTE({' + \
                               ncaa_team_info[1] + '}, "\'", " ")="' + \
                               team_name.replace("'", " ") + '")'
            ncaa_team = airtable_ncaa_team.get_all(formula=team_formula_str)
            if not ncaa_team:
                ncaa_team = airtable_ncaa_team.insert(
                    {ncaa_team_info[0]: team_name})
            else:
                ncaa_team = ncaa_team[0]
            season = airtable_season.match(season_info[0], season_name)
            if not season:
                season = airtable_season.insert({season_info[0]: season_name})
            formula_str = 'AND(SUBSTITUTE({Team}, "\'", " ")="' + \
                          ncaa_team['fields']['NCAA Team Name'].replace("'", " ") + \
                          '", {Season}="' + season_name + '")'
            fields = {
                vegasinsider_ncaa_info[0]: [season['id']],
                vegasinsider_ncaa_info[1]: [ncaa_team['id']],
                vegasinsider_ncaa_info[2]: int(td_list[0].text),
                vegasinsider_ncaa_info[3]: float(td_list[2].text),
                vegasinsider_ncaa_info[4]: float(td_list[3].text),
                vegasinsider_ncaa_info[5]: float(td_list[4].text),
                vegasinsider_ncaa_info[6]: float(td_list[5].text),
                vegasinsider_ncaa_info[7]: float(td_list[6].text),
                vegasinsider_ncaa_info[8]: int(td_list[7].text),
                vegasinsider_ncaa_info[9]: int(td_list[8].text),
                vegasinsider_ncaa_info[10]: float(td_list[9].text),
                vegasinsider_ncaa_info[11]: float(td_list[10].text),
                vegasinsider_ncaa_info[12]: float(td_list[11].text),
                vegasinsider_ncaa_info[13]: int(td_list[12].text),
                vegasinsider_ncaa_info[14]: int(td_list[13].text),
                vegasinsider_ncaa_info[15]: int(td_list[14].text),
                vegasinsider_ncaa_info[16]: float(td_list[15].text),
                vegasinsider_ncaa_info[17]: float(td_list[16].text),
                vegasinsider_ncaa_info[18]: float(td_list[17].text),
                vegasinsider_ncaa_info[19]: float(td_list[18].text),
                vegasinsider_ncaa_info[20]: float(td_list[19].text),
                vegasinsider_ncaa_info[21]: float(td_list[20].text),
                vegasinsider_ncaa_info[22]: float(td_list[21].text),
                vegasinsider_ncaa_info[23]: int(td_list[22].text),
                vegasinsider_ncaa_info[24]: int(td_list[23].text)
            }
            record = airtable_vegasinsider_ncaa.get_all(formula=formula_str)
            if record:
                airtable_vegasinsider_ncaa.replace(record[0]['id'], fields)
            else:
                airtable_vegasinsider_ncaa.insert(fields)
    if action_type == 0 and len(total_sheet_data) > 0:  # store into gsheet
        df = pd.DataFrame(total_sheet_data, columns=VegasInsider.gsheet_table_columns)
        original_df = wks.get_as_df()
        original_df = original_df.append(df)
        wks.set_dataframe(original_df, (1, 1))
Exemple #2
0
def add_curated_picks_ncaab():
    total_sheet_data = []
    browser.get('https://www.teamrankings.com/ncaa-basketball-betting-picks/')
    time.sleep(3)
    body = browser.find_element_by_id(
        'DataTables_Table_0').find_element_by_tag_name('tbody')
    tr_list = body.find_elements_by_tag_name('tr')
    first_td_list = tr_list[0].find_elements_by_tag_name('td')
    if len(first_td_list) == 1:
        print("no TeamRanking-NCAA data")
        return
    date = Select(browser.find_element_by_class_name(
        'redirectOnChange')).first_selected_option.text
    date_info = date.split(' ')[1] + "," + date.split(' ')[2] + "," + str(
        datetime.datetime.now().year)
    date = datetime.datetime.strptime(date_info, '%b,%d,%Y').date()
    date = str(date)
    for tr in tr_list:
        td_list = tr.find_elements_by_tag_name('td')
        rot_list = td_list[0].get_attribute('innerHTML').strip().split('<br>')
        team_list = td_list[1].get_attribute('innerHTML').strip().split('<br>')
        team_list[0] = team_list[0].strip()
        team_list[1] = team_list[1].strip()
        game_winner = td_list[2].find_element_by_class_name(
            'picks-block-in').text.strip()
        ATS = td_list[3].find_element_by_class_name(
            'picks-block-in').text.strip()
        total = td_list[4].find_element_by_class_name(
            'picks-block-in').text.strip()
        money_line_value = td_list[5].find_element_by_class_name(
            'picks-block-in').text.strip()
        if action_type <= 1:
            team1 = session.query(NCAATeam).filter(
                or_(NCAATeam.ShortTeamName == team_list[0],
                    NCAATeam.FullTeamName == team_list[0])).first()
            team2 = session.query(NCAATeam).filter(
                or_(NCAATeam.ShortTeamName == team_list[1],
                    NCAATeam.FullTeamName == team_list[1])).first()
            if not team1:
                team1 = NCAATeam(team_list[0])
                session.add(team1)
                session.flush()
            if not team2:
                team2 = NCAATeam(team_list[1])
                session.add(team2)
                session.flush()
            db_record = session.query(TeamRankingNCAA).filter(
                TeamRankingNCAA.Team1ID == team1.ID,
                TeamRankingNCAA.Team2ID == team2.ID,
                TeamRankingNCAA.Date == date).first()
            new_teamranking = TeamRankingNCAA(team1.ID,
                                              int(rot_list[0]), team2.ID,
                                              int(rot_list[1]), game_winner,
                                              ATS, total, money_line_value,
                                              date)
            if not db_record:
                session.add(new_teamranking)
                if action_type == 0:
                    total_sheet_data.append(
                        (team1.ShortTeamName, team1.FullTeamName,
                         team2.ShortTeamName, team2.FullTeamName, rot_list[0],
                         rot_list[1], game_winner, ATS, total,
                         money_line_value, date))
            else:
                new_teamranking.ID = db_record.ID
                session.merge(new_teamranking)
        if action_type == 0 or action_type == 2:
            team1_formula_str = 'OR(SUBSTITUTE({' + ncaa_team_info[0] + '}, "\'", " ")="' + \
                                team_list[0].replace("'", " ") + '", SUBSTITUTE({' + \
                                ncaa_team_info[1] + '}, "\'", " ")="' + \
                                team_list[0].replace("'", " ") + '")'
            team2_formula_str = 'OR(SUBSTITUTE({' + ncaa_team_info[0] + '}, "\'", " ")="' + \
                                team_list[1].replace("'", " ") + '", SUBSTITUTE({' + \
                                ncaa_team_info[1] + '}, "\'", " ")="' + \
                                team_list[1].replace("'", " ") + '")'
            ncaa_first_team = airtable_ncaa_team.get_all(
                formula=team1_formula_str)
            ncaa_second_team = airtable_ncaa_team.get_all(
                formula=team2_formula_str)
            if not ncaa_first_team:
                ncaa_first_team = airtable_ncaa_team.insert(
                    {ncaa_team_info[0]: team_list[0]})
            else:
                ncaa_first_team = ncaa_first_team[0]
            if not ncaa_second_team:
                ncaa_second_team = airtable_ncaa_team.insert(
                    {ncaa_team_info[0]: team_list[1]})
            else:
                ncaa_second_team = ncaa_second_team[0]
            game_date = airtable_game_date.match(game_date_info[0], date)
            if not game_date:
                game_date = airtable_game_date.insert(
                    {game_date_info[0]: date})
            formula_str = 'AND(SUBSTITUTE({Team1}, "\'", " ")="' + \
                          ncaa_first_team['fields']['NCAA Team Name'].replace("'", " ") + \
                          '", SUBSTITUTE({Team2}, "\'", " ")="' + \
                          ncaa_second_team['fields']['NCAA Team Name'].replace("'", " ") + '", {Date}="' + date + '")'
            fields = {
                teamranking_ncaa_info[0]: [ncaa_first_team['id']],
                teamranking_ncaa_info[1]: [ncaa_second_team['id']],
                teamranking_ncaa_info[2]: [game_date['id']],
                teamranking_ncaa_info[3]: int(rot_list[0]),
                teamranking_ncaa_info[4]: int(rot_list[1]),
                teamranking_ncaa_info[5]: game_winner,
                teamranking_ncaa_info[6]: ATS,
                teamranking_ncaa_info[7]: total,
                teamranking_ncaa_info[8]: money_line_value
            }
            record = airtable_teamranking_ncaa.get_all(formula=formula_str)
            if record:
                airtable_teamranking_ncaa.replace(record[0]['id'], fields)
            else:
                airtable_teamranking_ncaa.insert(fields)
    if action_type == 0 and len(total_sheet_data) > 0:  # store into gsheet
        df = pd.DataFrame(total_sheet_data,
                          columns=TeamRankingNCAA.gsheet_table_columns)
        original_df = wks_ncaa.get_as_df()
        original_df = original_df.append(df)
        wks_ncaa.set_dataframe(original_df, (1, 1))
Exemple #3
0
def add_team_score():
    time.sleep(5)
    total_sheet_data = []
    date_selections = browser.find_element_by_id('cboUpcomingDates')
    date_info = date_selections.find_elements_by_tag_name(
        'option')[0].get_attribute('innerHTML').split(', ')
    date = date_info[1] + ", " + date_info[2]
    date = datetime.datetime.strptime(date, '%B %d, %Y').date()
    date = str(date)
    table = browser.find_element_by_id('myTable4')
    td_list = table.find_elements_by_xpath(
        '//*[starts-with(@id, "tdUpcoming_") and not(contains(@class, "filler"))]'
    )
    new_team_scores = [
        HaslaMetrics(),
        HaslaMetrics(),
        HaslaMetrics(),
        HaslaMetrics()
    ]
    new_team_scores[0].Date = date
    new_team_scores[1].Date = date
    new_team_scores[2].Date = date
    new_team_scores[3].Date = date
    add_index = 0
    for td in td_list:
        td_text = td.get_attribute('innerHTML')
        td_id = td.get_attribute('id')
        # if len(td_text) == 0 and '_sc' not in td_id:
        if len(td_text) == 0:
            continue
        td_infos = td_id.split('_')
        td_infos[1] = int(td_infos[1])
        td_infos[1] -= 1
        td_infos[1] %= 4
        if td_infos[2] == '1':
            if '_sc' not in td_id:
                first_team_name = td.find_element_by_tag_name(
                    'a').get_attribute('innerHTML')
                new_team_scores[td_infos[1]].Team1Name = first_team_name
                team1 = session.query(NCAATeam).filter(
                    or_(NCAATeam.ShortTeamName == first_team_name,
                        NCAATeam.FullTeamName == first_team_name)).first()
                if action_type <= 1:
                    if not team1:
                        team1 = NCAATeam(first_team_name)
                        session.add(team1)
                        session.flush()
                    new_team_scores[td_infos[1]].Team1ID = team1.ID
                new_team_scores[td_infos[1]].Team1Rank = int(
                    td.find_element_by_tag_name('sub').get_attribute(
                        'innerHTML'))
            else:
                new_team_scores[td_infos[1]].Team1Score = float(
                    td.get_attribute('innerHTML'))
        else:
            if '_sc' not in td_id:
                second_team_name = td.find_element_by_tag_name(
                    'a').get_attribute('innerHTML')
                new_team_scores[td_infos[1]].Team2Name = second_team_name
                team2 = session.query(NCAATeam).filter(
                    or_(NCAATeam.ShortTeamName == second_team_name,
                        NCAATeam.FullTeamName == second_team_name)).first()
                if action_type <= 1:
                    if not team2:
                        team2 = NCAATeam(second_team_name)
                        session.add(team2)
                        session.flush()
                    new_team_scores[td_infos[1]].Team2ID = team2.ID
                new_team_scores[td_infos[1]].Team2Rank = int(
                    td.find_element_by_tag_name('sub').get_attribute(
                        'innerHTML'))
            else:
                new_team_scores[td_infos[1]].Team2Score = float(
                    td.get_attribute('innerHTML'))
        add_index += 1
        if add_index % 16 == 0:
            for idx in range(4):
                if action_type <= 1:
                    db_record = session.query(HaslaMetrics).filter(
                        HaslaMetrics.Team1ID == new_team_scores[idx].Team1ID,
                        HaslaMetrics.Team2ID == new_team_scores[idx].Team2ID,
                        HaslaMetrics.Date == date).first()
                    if not db_record:
                        session.add(new_team_scores[idx])
                        if action_type == 0:
                            first_team = session.query(NCAATeam).filter(
                                NCAATeam.ID ==
                                new_team_scores[idx].Team1ID).first()
                            second_team = session.query(NCAATeam).filter(
                                NCAATeam.ID ==
                                new_team_scores[idx].Team2ID).first()
                            total_sheet_data.append(
                                (first_team.ShortTeamName,
                                 first_team.FullTeamName,
                                 second_team.ShortTeamName,
                                 second_team.FullTeamName,
                                 new_team_scores[idx].Date,
                                 new_team_scores[idx].Team1Rank,
                                 new_team_scores[idx].Team2Rank,
                                 new_team_scores[idx].Team1Score,
                                 new_team_scores[idx].Team2Score))
                    else:
                        new_team_scores[idx].ID = db_record.ID
                        session.merge(new_team_scores[idx])
                elif action_type == 0 or action_type == 2:
                    team1_formula_str = 'OR(SUBSTITUTE({' + ncaa_team_info[0] + '}, "\'", " ")="' + \
                                        new_team_scores[idx].Team1Name.replace("'", " ") + '", SUBSTITUTE({' + \
                                        ncaa_team_info[1] + '}, "\'", " ")="' + \
                                        new_team_scores[idx].Team1Name.replace("'", " ") + '")'
                    team2_formula_str = 'OR(SUBSTITUTE({' + ncaa_team_info[0] + '}, "\'", " ")="' + \
                                        new_team_scores[idx].Team2Name.replace("'", " ") + '", SUBSTITUTE({' + \
                                        ncaa_team_info[1] + '}, "\'", " ")="' + \
                                        new_team_scores[idx].Team2Name.replace("'", " ") + '")'
                    ncaa_first_team = airtable_ncaa_team.get_all(
                        formula=team1_formula_str)
                    ncaa_second_team = airtable_ncaa_team.get_all(
                        formula=team2_formula_str)
                    if not ncaa_first_team:
                        ncaa_first_team = airtable_ncaa_team.insert({
                            ncaa_team_info[0]:
                            new_team_scores[idx].Team1Name
                        })
                    else:
                        ncaa_first_team = ncaa_first_team[0]
                    if not ncaa_second_team:
                        ncaa_second_team = airtable_ncaa_team.insert({
                            ncaa_team_info[0]:
                            new_team_scores[idx].Team2Name
                        })
                    else:
                        ncaa_second_team = ncaa_second_team[0]
                    game_date = airtable_game_date.match(
                        game_date_info[0], date)
                    if not game_date:
                        game_date = airtable_game_date.insert(
                            {game_date_info[0]: date})
                    formula_str = 'AND(SUBSTITUTE({Team1}, "\'", " ")="' + \
                                  ncaa_first_team['fields']['NCAA Team Name'].replace("'", " ") + \
                                  '", SUBSTITUTE({Team2}, "\'", " ")="' + \
                                  ncaa_second_team['fields']['NCAA Team Name'].replace("'",
                                                                                       " ") + '", {Date}="' + date + '")'
                    fields = {
                        hasla_metrics_info[0]: [ncaa_first_team['id']],
                        hasla_metrics_info[1]: [ncaa_second_team['id']],
                        hasla_metrics_info[2]: [game_date['id']],
                        hasla_metrics_info[3]:
                        new_team_scores[idx].Team1Rank,
                        hasla_metrics_info[4]:
                        new_team_scores[idx].Team2Rank,
                        hasla_metrics_info[5]:
                        float(new_team_scores[idx].Team1Score),
                        hasla_metrics_info[6]:
                        float(new_team_scores[idx].Team2Score),
                    }
                    record = airtable_haslametrics.get_all(formula=formula_str)
                    if record:
                        airtable_haslametrics.replace(record[0]['id'], fields)
                    else:
                        airtable_haslametrics.insert(fields)
            new_team_scores = [
                HaslaMetrics(),
                HaslaMetrics(),
                HaslaMetrics(),
                HaslaMetrics()
            ]
            new_team_scores[0].Date = date
            new_team_scores[1].Date = date
            new_team_scores[2].Date = date
            new_team_scores[3].Date = date

    if action_type == 0 and len(total_sheet_data) > 0:  # store into gsheet
        df = pd.DataFrame(total_sheet_data,
                          columns=HaslaMetrics.gsheet_table_columns)
        original_df = wks.get_as_df()
        original_df = original_df.append(df)
        wks.set_dataframe(original_df, (1, 1))
Exemple #4
0
def add_curated_picks_wise_ncaab():
    total_sheet_data = []
    browser.get('https://www.pickswise.com/sports/college-basketball/')
    div_list = browser.find_elements_by_css_selector(
        '.ContentPanel.ContentPanel--block.ContentPanel--no-padding')

    for div in div_list:
        datetime_info = div.find_element_by_tag_name('h2').get_attribute(
            'innerHTML').strip()
        if '<br>' not in datetime_info:
            continue
        datetime_info = datetime_info.split('<br>')[0]
        datetime_info = datetime_info.strip()
        datetime_info = datetime_info.split(', ')[1]
        date = datetime_info.split(' - ')[0][:-2]
        time = datetime_info.split(' - ')[1]
        time = time.split(' ')[0]
        date = date + "," + str(datetime.datetime.now().year)
        date = datetime.datetime.strptime(date, '%b %d,%Y').date()
        date = str(date)
        try:
            team_list = div.find_element_by_class_name(
                'PreviewCard__teams-container').find_elements_by_class_name(
                    'Pick')
            first_team_name = team_list[0].find_element_by_class_name(
                'Pick__team-name').text
            second_team_name = team_list[1].find_element_by_class_name(
                'Pick__team-name').text
            first_team_name = first_team_name.strip()
            second_team_name = second_team_name.strip()
            pick_list = div.find_element_by_css_selector('.TabbedContent__tab.TabbedContent__tab--active') \
                .find_elements_by_class_name('Pick')
            first_team_pick_outcome = pick_list[0].find_element_by_class_name(
                'Pick__outcome').get_attribute('innerHTML')
            first_team_pick_market = pick_list[0].find_element_by_class_name(
                'Pick__market').get_attribute('innerHTML')
            first_team_prediction = pick_list[0].find_element_by_css_selector(
                '.Button.Button--border-red').get_attribute('innerHTML')
            second_team_pick_outcome = pick_list[1].find_element_by_class_name(
                'Pick__outcome').get_attribute('innerHTML')
            second_team_pick_market = pick_list[1].find_element_by_class_name(
                'Pick__market').get_attribute('innerHTML')
            second_team_prediction = pick_list[1].find_element_by_css_selector(
                '.Button.Button--border-red').get_attribute('innerHTML')
            if action_type <= 1:
                team1 = session.query(NCAATeam).filter(
                    or_(NCAATeam.ShortTeamName == first_team_name,
                        NCAATeam.FullTeamName == first_team_name)).first()
                team2 = session.query(NCAATeam).filter(
                    or_(NCAATeam.ShortTeamName == second_team_name,
                        NCAATeam.FullTeamName == second_team_name)).first()
                if not team1:
                    team1 = NCAATeam('', first_team_name)
                    session.add(team1)
                    session.flush()
                if not team2:
                    team2 = NCAATeam('', second_team_name)
                    session.add(team2)
                    session.flush()
                db_record = session.query(PicksWiseNCAA).filter(
                    PicksWiseNCAA.Team1ID == team1.ID,
                    PicksWiseNCAA.Team2ID == team2.ID,
                    PicksWiseNCAA.Date == date,
                    PicksWiseNCAA.Time == time).first()
                new_pickwise = PicksWiseNCAA(
                    team1.ID, team2.ID, first_team_prediction,
                    first_team_pick_outcome, first_team_pick_market,
                    second_team_prediction, second_team_pick_outcome,
                    second_team_pick_market, date, time)
                if not db_record:
                    session.add(new_pickwise)
                    if action_type == 0:
                        total_sheet_data.append(
                            (team1.ShortTeamName, team1.FullTeamName,
                             team2.ShortTeamName, team2.FullTeamName, date,
                             time, first_team_prediction,
                             first_team_pick_outcome, first_team_pick_market,
                             second_team_prediction, second_team_pick_outcome,
                             second_team_pick_market))
                else:
                    new_pickwise.ID = db_record.ID
                    session.merge(new_pickwise)
            if action_type == 2:
                team1_formula_str = 'OR(SUBSTITUTE({' + ncaa_team_info[0] + '}, "\'", " ")="' + \
                                    first_team_name.replace("'", " ") + '", SUBSTITUTE({' + \
                                    ncaa_team_info[1] + '}, "\'", " ")="' + \
                                    first_team_name.replace("'", " ") + '")'
                team2_formula_str = 'OR(SUBSTITUTE({' + ncaa_team_info[0] + '}, "\'", " ")="' + \
                                    second_team_name.replace("'", " ") + '", SUBSTITUTE({' + \
                                    ncaa_team_info[1] + '}, "\'", " ")="' + \
                                    second_team_name.replace("'", " ") + '")'
                ncaa_first_team = airtable_ncaa_team.get_all(
                    formula=team1_formula_str)
                ncaa_second_team = airtable_ncaa_team.get_all(
                    formula=team2_formula_str)
                if not ncaa_first_team:
                    ncaa_first_team = airtable_ncaa_team.insert(
                        {ncaa_team_info[0]: first_team_name})
                else:
                    ncaa_first_team = ncaa_first_team[0]
                if not ncaa_second_team:
                    ncaa_second_team = airtable_ncaa_team.insert(
                        {ncaa_team_info[0]: second_team_name})
                else:
                    ncaa_second_team = ncaa_second_team[0]
                game_date = airtable_game_date.match(game_date_info[0], date)
                if not game_date:
                    game_date = airtable_game_date.insert(
                        {game_date_info[0]: date})
                game_time = airtable_game_time.match(game_time_info[0], time)
                if not game_time:
                    game_time = airtable_game_time.insert(
                        {game_time_info[0]: time})
                formula_str = 'AND(SUBSTITUTE({Team1}, "\'", " ")="' + \
                              ncaa_first_team['fields']['NCAA Team Name'].replace("'", " ") + \
                              '", SUBSTITUTE({Team2}, "\'", " ")="' + \
                              ncaa_second_team['fields']['NCAA Team Name'].replace("'",
                                                                                   " ") + '", {Date}="' + date + '")'
                fields = {
                    pickwise_ncaa_info[0]: [ncaa_first_team['id']],
                    pickwise_ncaa_info[1]: [ncaa_second_team['id']],
                    pickwise_ncaa_info[2]: [game_date['id']],
                    pickwise_ncaa_info[3]: [game_time['id']],
                    pickwise_ncaa_info[4]: first_team_prediction,
                    pickwise_ncaa_info[5]: second_team_prediction,
                    pickwise_ncaa_info[6]: first_team_pick_outcome,
                    pickwise_ncaa_info[7]: second_team_pick_outcome,
                    pickwise_ncaa_info[8]: first_team_pick_market,
                    pickwise_ncaa_info[9]: second_team_pick_market
                }
                record = airtable_pickwise_ncaa.get_all(formula=formula_str)
                if record:
                    airtable_pickwise_ncaa.replace(record[0]['id'], fields)
                else:
                    airtable_pickwise_ncaa.insert(fields)
        except NoSuchElementException:
            print("No Pickwise NCAAB data")
            continue
    if action_type == 0 and len(total_sheet_data) > 0:  # store into gsheet
        df = pd.DataFrame(total_sheet_data,
                          columns=PicksWiseNCAA.gsheet_table_columns)
        original_df = wks_pickwise_ncaa.get_as_df()
        original_df = original_df.append(df)
        wks_pickwise_ncaa.set_dataframe(original_df, (1, 1))
def add_espn_ncaa():
    browser.get('https://www.espn.com/mens-college-basketball/bpi/_/view/bpi')
    date = browser.find_element_by_css_selector('.bpi__updateTime.u-fz-xs').find_elements_by_tag_name('span')[0]. \
        get_attribute('innerHTML')
    season_name = browser.find_element_by_css_selector(
        '.season.button-filter.med.dropdown-toggle').text
    match_content = browser.find_element_by_class_name('matchup-content')
    li_list = match_content.find_element_by_class_name(
        'pagination').find_elements_by_tag_name('li')
    page_count = li_list[len(li_list) - 2].find_element_by_tag_name(
        'a').get_attribute('innerHTML')
    for page in range(1, int(page_count) + 1):
        browser.get(
            'https://www.espn.com/mens-college-basketball/bpi/_/view/bpi/page/'
            + str(page))
        match_content = browser.find_element_by_class_name('matchup-content')
        tr_list = match_content.find_element_by_class_name('bpi__table').find_element_by_tag_name('tbody'). \
            find_elements_by_tag_name('tr')
        for tr in tr_list:
            td_list = tr.find_elements_by_tag_name('td')
            ranking = td_list[0].get_attribute('innerHTML')
            team_name = td_list[1].find_element_by_class_name(
                'team-names').get_attribute('innerHTML').strip()
            team_name_temple = td_list[1].find_element_by_tag_name(
                'abbr').get_attribute('innerHTML').strip()
            conf = td_list[2].get_attribute('innerHTML')
            w_l = td_list[3].get_attribute('innerHTML')
            bpi_off = td_list[4].get_attribute('innerHTML')
            bpi_def = td_list[5].get_attribute('innerHTML')
            bpi = td_list[6].get_attribute('innerHTML')
            rank_class = td_list[7].find_element_by_tag_name(
                'span').get_attribute('class')
            week_rank_chg = td_list[7].find_element_by_tag_name(
                'span').get_attribute('innerHTML').strip()
            if 'positive' in rank_class:
                week_rank_chg = '+ ' + week_rank_chg
            elif 'negative' in rank_class:
                week_rank_chg = '- ' + week_rank_chg
            # print(week_rank_chg)
            # if action_type == 1:
            #     new_espn_ncaab = ESPNNCAAB(team_name, team_name_temple, ranking, conf, w_l, bpi_off, bpi_def,
            #                                week_rank_chg,
            #                                date)
            #     session.add(new_espn_ncaab)
            if action_type == 2:
                team1_formula_str = 'OR(SUBSTITUTE({' + ncaa_team_info[0] + '}, "\'", " ")="' + \
                                    team_name.replace("'", " ") + '", SUBSTITUTE({' + \
                                    ncaa_team_info[1] + '}, "\'", " ")="' + \
                                    team_name.replace("'", " ") + '")'
                ncaa_first_team = airtable_ncaa_team.get_all(
                    formula=team1_formula_str)
                if not ncaa_first_team:
                    ncaa_first_team = airtable_ncaa_team.insert(
                        {ncaa_team_info[0]: team_name})
                else:
                    ncaa_first_team = ncaa_first_team[0]
                season = airtable_season.match(season_info[0], season_name)
                if not season:
                    season = airtable_season.insert(
                        {season_info[0]: season_name})
                formula_str = 'AND(SUBSTITUTE({Team}, "\'", " ")="' + \
                              ncaa_first_team['fields']['NCAA Team Name'].replace("'", " ") + \
                              '", {Season}="' + season_name + '")'
                # print(formula_str)
                if len(airtable_espn_ncaa.get_all(formula=formula_str)) == 0:
                    airtable_espn_ncaa.insert({
                        espn_ncaa_info[0]: [season['id']],
                        espn_ncaa_info[1]: [ncaa_first_team['id']],
                        espn_ncaa_info[2]:
                        int(ranking),
                        espn_ncaa_info[3]:
                        conf,
                        espn_ncaa_info[4]:
                        w_l,
                        espn_ncaa_info[5]:
                        float(bpi_off),
                        espn_ncaa_info[6]:
                        float(bpi_def),
                        espn_ncaa_info[7]:
                        week_rank_chg
                    })