-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnbaapi.py
131 lines (114 loc) · 6.63 KB
/
nbaapi.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
125
126
127
128
129
130
131
import requests
import json
import sqlite3
from datetime import date
import os
def odds_finder(team_name):
url = "https://odds.p.rapidapi.com/v1/odds"
querystring = {"sport":"basketball_nba","region":"us","mkt":"h2h","dateFormat":"iso","oddsFormat":"american"}
headers = {
'x-rapidapi-key': "xxxxxxxxxxxxxxxxxxxxxx",
'x-rapidapi-host': "xxxxxxxxxxxxxxxxxxxxx"
}
response = requests.request("GET", url, headers=headers, params=querystring)
data = json.loads(response.text)
if data['success'] == False:
print("Unsuccessful Initial Request")
return None
list1 = data['data']
playToday = False
homeTeam = False
for i in list1:
if team_name in i['teams']:
playToday = True
if team_name == i['home_team']:
homeTeam = True
if team_name == i['teams'][0]:
oddInd = 0
opponent = i['teams'][1]
if team_name == i['teams'][1]:
oddInd = 1
opponent = i['teams'][0]
break
if playToday == True:
outputOdds = []
for j in i['sites']:
name = j['site_nice']
odd = j['odds']['h2h'][oddInd]
home = "No"
if homeTeam == True:
home = "Yes"
outputOdds.append((name, odd, home, opponent))
return outputOdds
else:
print("Specified team not playing today.")
return None
def winPercCalc(num):
if num < 0:
num *= -1
perc = float(num/(num+100))
else:
perc = float(100/(100+num))
return round(perc*100, 2)
def outputter(name, outputList):
print(name + ", Home Team: " + outputList[0][3] + "; Opponent: " + outputList[0][4])
print("BOOK ODDS WINNING PERCENTAGE")
for i in outputList:
print(i[0] + " " + str(i[1]) + " " + str(i[2]))
def dbMaker(cur, conn):
cur.execute('''CREATE TABLE IF NOT EXISTS Moneylines (Date TEXT, Team_id INTEGER, HomeTeam TEXT, Opponent_id INTEGER, oddsFANDUEL INTEGER,
winpercFANDUEL REAL, oddsFOXBET INTEGER, winpercFOXBET REAL, oddsBOVADA INTEGER, winpercBOVADA REAL,
oddsUNIBET INTEGER, winpercUNIBET REAL, oddsBETRIVERS INTEGER, winpercBETRIVER REAL, oddsDRAFTKINGS INTEGER,
winpercDRAFTKINGS REAL, oddsSUGARHOUSE INTEGER, winpercSUGARHOUSE REAL, oddsPOINTSBET INTEGER, winpercPOINTSBET REAL,
oddsBETFAIR INTEGER, winpercBETFAIR REAL, oddsBETONLINE INTEGER, winpercBETONLINE REAL, oddsWILLIAMHILL INTEGER,
winpercWILLIAMHILL REAL, oddsINTERTOPS INTEGER, winpercINTERTOPS REAL, oddsGTBETS INTEGER, winpercGTBETS REAL,
oddsBOOKMAKER INTEGER, winpercBOOKMAKER REAL, oddsMYBOOKIE INTEGER, winpercMYBOOKIE REAL, oddsCAESARS INTEGER,
winpercCAESARS REAL)''')
conn.commit()
def updateTable(tid, oL, cur, conn):
cur.execute('''UPDATE Moneylines SET oddsFANDUEL = ?, winpercFANDUEL = ?, oddsFOXBET = ?, winpercFOXBET = ?, oddsBOVADA = ?,
winpercBOVADA = ?, oddsUNIBET = ?, winpercUNIBET = ?, oddsBETRIVERS = ?, winpercBETRIVER = ?, oddsDRAFTKINGS = ?,
winpercDRAFTKINGS = ?, oddsSUGARHOUSE = ?,
winpercSUGARHOUSE = ?, oddsPOINTSBET = ?, winpercPOINTSBET = ?, oddsBETFAIR = ?, winpercBETFAIR = ?, oddsBETONLINE = ?, winpercBETONLINE = ?, oddsWILLIAMHILL = ?,
winpercWILLIAMHILL = ?, oddsINTERTOPS = ?, winpercINTERTOPS = ?, oddsGTBETS = ?, winpercGTBETS = ?, oddsBOOKMAKER = ?, winpercBOOKMAKER = ?, oddsMYBOOKIE = ?,
winpercMYBOOKIE = ?, oddsCAESARS = ?, winpercCAESARS = ? WHERE Date = ? AND Team_id = ?''', (oL[0][1], oL[0][2], oL[1][1], oL[1][2], oL[2][1], oL[2][2], oL[3][1], oL[3][2], oL[4][1], oL[4][2], oL[5][1], oL[5][2], oL[6][1], oL[6][2],
oL[7][1], oL[7][2], oL[8][1], oL[8][2], oL[9][1], oL[9][2], oL[10][1], oL[10][2], oL[11][1], oL[11][2], oL[12][1], oL[12][2], oL[13][1], oL[13][2],
oL[14][1], oL[14][2], oL[15][1], oL[15][2], str(date.today()), tid))
conn.commit()
print("Database updated for changed lines.")
def dbAddition(name, oL, cur, conn):
cur.execute("SELECT id FROM Teams WHERE Team = ?", (name,))
tid = int(cur.fetchone()[0])
cur.execute("SELECT id FROM Teams WHERE Team = ?", (oL[0][4],))
oid = int(cur.fetchone()[0])
cur.execute("SELECT * FROM Moneylines WHERE Date = ? AND Team_id = ?", (str(date.today()), tid))
if len(cur.fetchall()) > 0:
updateTable(tid, oL, cur, conn)
else:
cur.execute('''INSERT INTO Moneylines (Date, Team_id, HomeTeam, Opponent_id, oddsFANDUEL, winpercFANDUEL, oddsFOXBET, winpercFOXBET, oddsBOVADA,
winpercBOVADA, oddsUNIBET, winpercUNIBET, oddsBETRIVERS, winpercBETRIVER, oddsDRAFTKINGS, winpercDRAFTKINGS, oddsSUGARHOUSE,
winpercSUGARHOUSE, oddsPOINTSBET, winpercPOINTSBET, oddsBETFAIR, winpercBETFAIR, oddsBETONLINE, winpercBETONLINE, oddsWILLIAMHILL,
winpercWILLIAMHILL, oddsINTERTOPS, winpercINTERTOPS, oddsGTBETS, winpercGTBETS, oddsBOOKMAKER, winpercBOOKMAKER, oddsMYBOOKIE,
winpercMYBOOKIE, oddsCAESARS, winpercCAESARS) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''', (str(date.today()), tid, oL[0][3], oid,
oL[0][1], oL[0][2], oL[1][1], oL[1][2], oL[2][1], oL[2][2], oL[3][1], oL[3][2], oL[4][1], oL[4][2], oL[5][1], oL[5][2], oL[6][1], oL[6][2],
oL[7][1], oL[7][2], oL[8][1], oL[8][2], oL[9][1], oL[9][2], oL[10][1], oL[10][2], oL[11][1], oL[11][2], oL[12][1], oL[12][2], oL[13][1], oL[13][2],
oL[14][1], oL[14][2], oL[15][1], oL[15][2]))
conn.commit()
print("Data added to database")
if __name__ == "__main__":
name = input("Enter a team name with capitalized first letters and the city (ex. Washington Wizards): ")
siteList = odds_finder(name)
if siteList != None:
oddsList = []
for i in siteList:
oddsList.append((i[0], i[1], winPercCalc(i[1]), i[2], i[3]))
outputter(name, oddsList)
if len(oddsList) < 17:
print("Game has likely already started, so the lines above are LIVE LINES")
else:
path = os.path.dirname(os.path.abspath(__file__))
conn = sqlite3.connect(path+'/'+'stats.db')
cur = conn.cursor()
dbMaker(cur, conn)
dbAddition(name, oddsList, cur, conn)