forked from brendanahart/MLBDailyProjections
-
Notifications
You must be signed in to change notification settings - Fork 1
/
odds.py
124 lines (99 loc) · 3.89 KB
/
odds.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import mysql.connector
import urllib2
import lxml.etree as etree
import mlbgame
from datetime import date
import constants
def getMLBOdds(year, month, day, cursor):
findGame = 'SELECT iddates FROM dates WHERE date = %s'
findGameData = (date(year, month, day),)
cursor.execute(findGame, findGameData)
dateID = -1
for datez in cursor:
dateID = datez[0]
base_url = constants.BovadaOdds
data = urllib2.urlopen(base_url)
xml = etree.parse(data)
root = xml.getroot()
mlbOdds = {}
for game in root.iterfind(".//Event"):
for team in game.iterfind(".//Competitor"):
bballTeam = team.get("NAME")
for line in team.iterfind(".//Line"):
if line.get("TYPE") == "Moneyline":
odds = line.find(".//Odds")
odd = odds.get("Line")
if odd == "EVEN":
odd = "+100"
mlbOdds[bballTeam] = odd
for key, value in mlbOdds.iteritems():
# convert value to probability
lineC = value[1:]
probability = 0
if value[0] == '+':
probability = (100/(float(lineC) + 100))
else:
probability = (float(lineC)/(float(lineC) + 100))
getAbbrev = "SELECT FANGRAPHSABBR FROM teammap WHERE BOVADA = %s"
getAbbrevD = (key,)
cursor.execute(getAbbrev, getAbbrevD)
teamAbbr = ""
for data in cursor:
teamAbbr = data[0]
getGameQ = "SELECT * FROM games WHERE date = %s and home = %s"
getGameD = (dateID, teamAbbr)
cursor.execute(getGameQ, getGameD)
gameData = cursor.fetchall()
# away team - data is empty
if not gameData:
setGameOddQ = "UPDATE games SET moneyLineAway = %s, awayWinProb = %s WHERE date = %s and away = %s"
getGameOddD = (value, probability, dateID, teamAbbr)
cursor.execute(setGameOddQ, getGameOddD)
# home team
else:
setGameOddQ = "UPDATE games SET moneyLineHome = %s, homeWinProb = %s WHERE date = %s and home = %s"
getGameOddD = (value, probability, dateID, teamAbbr)
cursor.execute(setGameOddQ, getGameOddD)
print "Set Odds for Games on %s/%s/%s" % (month, day, year)
def getSchedule(year, month, day, cursor):
addDate = "INSERT INTO dates (date) VALUES(%s)"
dateData = (date(year, month, day),)
cursor.execute(addDate, dateData)
gameID = cursor.lastrowid
games = mlbgame.day(year, month, day)
for game in games:
awayTeam = game.away_team
homeTeam = game.home_team
# select fangraphs abbrev
awayTeamQ = "SELECT FANGRAPHSABBR FROM teammap WHERE MLBAPI = %s"
awayTeamD = (awayTeam, )
cursor.execute(awayTeamQ, awayTeamD)
awayTeamAbbr = ""
for team in cursor:
awayTeamAbbr = team[0]
homeTeamQ = "SELECT FANGRAPHSABBR FROM teammap WHERE MLBAPI = %s"
homeTeamD = (homeTeam,)
cursor.execute(homeTeamQ, homeTeamD)
homeTeamAbbr = ""
for team in cursor:
homeTeamAbbr = team[0]
# insert into games
addGame = "INSERT INTO games (home, away, date) VALUES (%s, %s, %s)"
gameData = (homeTeamAbbr, awayTeamAbbr, gameID)
cursor.execute(addGame, gameData)
print "Loaded Games for %s/%s/%s" % (month, day, year)
if __name__ == "__main__":
cnx = mysql.connector.connect(user=constants.databaseUser,
host=constants.databaseHost,
database=constants.databaseName,
password=constants.databasePassword)
cursor = cnx.cursor()
# get games
year = constants.yearP
month = constants.monthP
day = constants.dayP
getSchedule(year, month, day, cursor)
getMLBOdds(year, month, day, cursor)
cursor.close()
cnx.commit()
cnx.close()