Ejemplo n.º 1
0
import os
from sql.mlb_database import MlbDatabase
from mine.rotowire import mine_pregame_stats
from mine.draft_kings import Draftkings
from datetime import date, timedelta, datetime
from email_service import send_email
import cProfile

os.chdir("/home/cameron/workspaces/MlbDatabase/mlb_scrape/Released/mlbscrape_python")

databaseSession = MlbDatabase().open_session()

try:
    mine_pregame_stats()
    Draftkings.save_daily_csv()
    csv_dict = Draftkings.get_csv_dict()
    Draftkings.update_salaries(databaseSession, csv_dict)
    Draftkings.predict_daily_points(databaseSession, date.today())
    optimal_lineup = Draftkings.get_optimal_lineup(databaseSession, date.today())
    print optimal_lineup
    send_email(optimal_lineup.__str__())
except Exception as e:
    print e
    send_email("The predictor generated an exception: {0}".format(e))

databaseSession.close()

Ejemplo n.º 2
0
def mine_yesterdays_results(database_session):
    # Query the database for all hitter game entries from yesterday
    hitter_entries = database_session.query(PregameHitterGameEntry).filter(PregameHitterGameEntry.game_date == (date.today() - timedelta(days=1)))
    for pregame_hitter_entry in hitter_entries:
        hitter_entry = database_session.query(HitterEntry).get(pregame_hitter_entry.rotowire_id)
        try:
            stat_row_dict = BaseballReference.get_yesterdays_hitting_game_log(hitter_entry.baseball_reference_id)
        except BaseballReference.TableRowNotFound:
            print "Player %s %s did not play yesterday. Deleting pregame entry %s %s" % (hitter_entry.first_name,
                                                                                         hitter_entry.last_name,
                                                                                         pregame_hitter_entry.game_date,
                                                                                         pregame_hitter_entry.opposing_team)
            database_session.delete(pregame_hitter_entry)
            database_session.commit()
            continue

        postgame_hitter_entry = PostgameHitterGameEntry()
        postgame_hitter_entry.rotowire_id = hitter_entry.rotowire_id
        postgame_hitter_entry.game_date = pregame_hitter_entry.game_date
        postgame_hitter_entry.game_h = int(stat_row_dict["H"])
        postgame_hitter_entry.game_bb = int(stat_row_dict["BB"])
        postgame_hitter_entry.game_hbp = int(stat_row_dict["HBP"])
        postgame_hitter_entry.game_r = int(stat_row_dict["R"])
        postgame_hitter_entry.game_sb = int(stat_row_dict["SB"])
        postgame_hitter_entry.game_hr = int(stat_row_dict["HR"])
        postgame_hitter_entry.game_rbi = int(stat_row_dict["RBI"])
        postgame_hitter_entry.game_2b = int(stat_row_dict["2B"])
        postgame_hitter_entry.game_3b = int(stat_row_dict["3B"])
        postgame_hitter_entry.game_1b = postgame_hitter_entry.game_h - postgame_hitter_entry.game_2b - \
                                        postgame_hitter_entry.game_3b - postgame_hitter_entry.game_hr
        postgame_hitter_entry.actual_draftkings_points = Draftkings.get_hitter_points(postgame_hitter_entry)
        try:
            database_session.add(postgame_hitter_entry)
            database_session.commit()
        except IntegrityError:
            database_session.rollback()
            print "Attempt to duplicate hitter postgame results: %s %s %s %s" % (hitter_entry.first_name,
                                                                                 hitter_entry.last_name,
                                                                                 hitter_entry.team,
                                                                                 pregame_hitter_entry.game_date)

    # Query the database for all hitter game entries from yesterday
    pitcher_entries = database_session.query(PregamePitcherGameEntry).filter(PregamePitcherGameEntry.game_date == (date.today() - timedelta(days=1)))
    for pregame_pitcher_entry in pitcher_entries:
        pitcher_entry = database_session.query(PitcherEntry).get(pregame_pitcher_entry.rotowire_id)
        print "Mining yesterday for %s %s" % (pitcher_entry.first_name, pitcher_entry.last_name)
        try:
            stat_row_dict = BaseballReference.get_pitching_game_log(pitcher_entry.baseball_reference_id)
        except BaseballReference.TableRowNotFound:
            print "Player %s %s did not play yesterday. Deleting pregame entry %s %s" % (pitcher_entry.first_name,
                                                                                         pitcher_entry.last_name,
                                                                                         pregame_pitcher_entry.game_date,
                                                                                         pregame_pitcher_entry.opposing_team)
            database_session.delete(pregame_pitcher_entry)
            database_session.commit()
            continue

        postgame_pitcher_entry = PostgamePitcherGameEntry()
        postgame_pitcher_entry.rotowire_id = pitcher_entry.rotowire_id
        postgame_pitcher_entry.game_date = pregame_pitcher_entry.game_date
        postgame_pitcher_entry.game_ip = float(stat_row_dict["IP"])
        postgame_pitcher_entry.game_so = int(stat_row_dict["SO"])
        if str(stat_row_dict["Dec"])[0] == "W":
            postgame_pitcher_entry.game_wins = 1
        postgame_pitcher_entry.game_er = int(stat_row_dict["ER"])
        postgame_pitcher_entry.game_er = int(stat_row_dict["ER"])
        postgame_pitcher_entry.game_h = int(stat_row_dict["H"])
        postgame_pitcher_entry.game_bb = int(stat_row_dict["BB"])
        postgame_pitcher_entry.game_hbp = int(stat_row_dict["HBP"])
        if stat_row_dict["Inngs"] == "CG":
            postgame_pitcher_entry.game_cg = 1
        if stat_row_dict["Inngs"] == "SHO":
            postgame_pitcher_entry.game_cgso = 1
        if postgame_pitcher_entry.game_cg == 1 and postgame_pitcher_entry.game_h == 0:
            postgame_pitcher_entry.game_no_hitter = 1
        postgame_pitcher_entry.actual_draftkings_points = Draftkings.get_pitcher_points(postgame_pitcher_entry)
        try:
            database_session.add(postgame_pitcher_entry)
            database_session.commit()
        except IntegrityError:
            database_session.rollback()
            print "Attempt to duplicate pitcher postgame results: %s %s %s %s" % (pitcher_entry.first_name,
                                                                                  pitcher_entry.last_name,
                                                                                  pregame_pitcher_entry.opposing_team,
                                                                                  postgame_pitcher_entry.game_date)
Ejemplo n.º 3
0
def mine_yesterdays_results(database_session):
    # Query the database for all hitter game entries from yesterday
    hitter_entries = database_session.query(PregameHitterGameEntry).filter(
        PregameHitterGameEntry.game_date == (date.today() - timedelta(days=1)))
    for pregame_hitter_entry in hitter_entries:
        hitter_entry = database_session.query(HitterEntry).get(
            pregame_hitter_entry.rotowire_id)
        try:
            stat_row_dict = BaseballReference.get_yesterdays_hitting_game_log(
                hitter_entry.baseball_reference_id)
        except BaseballReference.TableRowNotFound:
            print "Player %s %s did not play yesterday. Deleting pregame entry %s %s" % (
                hitter_entry.first_name, hitter_entry.last_name,
                pregame_hitter_entry.game_date,
                pregame_hitter_entry.opposing_team)
            database_session.delete(pregame_hitter_entry)
            database_session.commit()
            continue

        postgame_hitter_entry = PostgameHitterGameEntry()
        postgame_hitter_entry.rotowire_id = hitter_entry.rotowire_id
        postgame_hitter_entry.game_date = pregame_hitter_entry.game_date
        postgame_hitter_entry.game_h = int(stat_row_dict["H"])
        postgame_hitter_entry.game_bb = int(stat_row_dict["BB"])
        postgame_hitter_entry.game_hbp = int(stat_row_dict["HBP"])
        postgame_hitter_entry.game_r = int(stat_row_dict["R"])
        postgame_hitter_entry.game_sb = int(stat_row_dict["SB"])
        postgame_hitter_entry.game_hr = int(stat_row_dict["HR"])
        postgame_hitter_entry.game_rbi = int(stat_row_dict["RBI"])
        postgame_hitter_entry.game_2b = int(stat_row_dict["2B"])
        postgame_hitter_entry.game_3b = int(stat_row_dict["3B"])
        postgame_hitter_entry.game_1b = postgame_hitter_entry.game_h - postgame_hitter_entry.game_2b - \
                                        postgame_hitter_entry.game_3b - postgame_hitter_entry.game_hr
        postgame_hitter_entry.actual_draftkings_points = Draftkings.get_hitter_points(
            postgame_hitter_entry)
        try:
            database_session.add(postgame_hitter_entry)
            database_session.commit()
        except IntegrityError:
            database_session.rollback()
            print "Attempt to duplicate hitter postgame results: %s %s %s %s" % (
                hitter_entry.first_name, hitter_entry.last_name,
                hitter_entry.team, pregame_hitter_entry.game_date)

    # Query the database for all hitter game entries from yesterday
    pitcher_entries = database_session.query(PregamePitcherGameEntry).filter(
        PregamePitcherGameEntry.game_date == (date.today() -
                                              timedelta(days=1)))
    for pregame_pitcher_entry in pitcher_entries:
        pitcher_entry = database_session.query(PitcherEntry).get(
            pregame_pitcher_entry.rotowire_id)
        print "Mining yesterday for %s %s" % (pitcher_entry.first_name,
                                              pitcher_entry.last_name)
        try:
            stat_row_dict = BaseballReference.get_pitching_game_log(
                pitcher_entry.baseball_reference_id)
        except BaseballReference.TableRowNotFound:
            print "Player %s %s did not play yesterday. Deleting pregame entry %s %s" % (
                pitcher_entry.first_name, pitcher_entry.last_name,
                pregame_pitcher_entry.game_date,
                pregame_pitcher_entry.opposing_team)
            database_session.delete(pregame_pitcher_entry)
            database_session.commit()
            continue

        postgame_pitcher_entry = PostgamePitcherGameEntry()
        postgame_pitcher_entry.rotowire_id = pitcher_entry.rotowire_id
        postgame_pitcher_entry.game_date = pregame_pitcher_entry.game_date
        postgame_pitcher_entry.game_ip = float(stat_row_dict["IP"])
        postgame_pitcher_entry.game_so = int(stat_row_dict["SO"])
        if str(stat_row_dict["Dec"])[0] == "W":
            postgame_pitcher_entry.game_wins = 1
        postgame_pitcher_entry.game_er = int(stat_row_dict["ER"])
        postgame_pitcher_entry.game_er = int(stat_row_dict["ER"])
        postgame_pitcher_entry.game_h = int(stat_row_dict["H"])
        postgame_pitcher_entry.game_bb = int(stat_row_dict["BB"])
        postgame_pitcher_entry.game_hbp = int(stat_row_dict["HBP"])
        if stat_row_dict["Inngs"] == "CG":
            postgame_pitcher_entry.game_cg = 1
        if stat_row_dict["Inngs"] == "SHO":
            postgame_pitcher_entry.game_cgso = 1
        if postgame_pitcher_entry.game_cg == 1 and postgame_pitcher_entry.game_h == 0:
            postgame_pitcher_entry.game_no_hitter = 1
        postgame_pitcher_entry.actual_draftkings_points = Draftkings.get_pitcher_points(
            postgame_pitcher_entry)
        try:
            database_session.add(postgame_pitcher_entry)
            database_session.commit()
        except IntegrityError:
            database_session.rollback()
            print "Attempt to duplicate pitcher postgame results: %s %s %s %s" % (
                pitcher_entry.first_name, pitcher_entry.last_name,
                pregame_pitcher_entry.opposing_team,
                postgame_pitcher_entry.game_date)
Ejemplo n.º 4
0
import os
from sql.mlb_database import MlbDatabase
from mine.rotowire import mine_pregame_stats
from mine.draft_kings import Draftkings
from datetime import date, timedelta
from email_service import send_email
import cProfile

os.chdir("/home/cameron/workspaces/MlbDatabase/mlb_scrape/Released/mlbscrape_python")

databaseSession = MlbDatabase().open_session()

#try:
    #cProfile.run('mine_pregame_stats()')
    #Draftkings.save_daily_csv()
    #csv_dict = Draftkings.get_csv_dict()
    #Draftkings.update_salaries(databaseSession, csv_dict)
Draftkings.predict_daily_points(databaseSession, date.today())
optimal_lineup = Draftkings.get_optimal_lineup(databaseSession, date.today())
print optimal_lineup
send_email(optimal_lineup.__str__())
"""except Exception as e:
    print e
    send_email("The predictor generated an exception: {0}".format(e))
"""
databaseSession.close()