-
Notifications
You must be signed in to change notification settings - Fork 0
/
tennis_db.py
315 lines (273 loc) · 9.55 KB
/
tennis_db.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy import MetaData
from sqlalchemy import Column, Integer, String, ARRAY, Boolean, Float
from sqlalchemy import inspect
from sqlalchemy.ext.mutable import Mutable
from sqlalchemy import ForeignKey
from sqlalchemy import PrimaryKeyConstraint
from sqlalchemy import ForeignKeyConstraint
from sqlalchemy.orm import mapper
from sqlalchemy.orm import relationship
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
import hashlib
import sqlite3
import sys
import time
import hashlib
CONN = None
ENGINE = None
SESSION = None
def init_session(engine):
global ENGINE
ENGINE = engine
global SESSION
if (SESSION == None):
Session = sessionmaker(bind=engine)
session = Session()
SESSION = session
def get_session():
# returns SESSION global variable
return SESSION
def Conn(database):
global CONN
if database:
print("[+] Inserting into Homemade Database: " + str(database))
# avoid creating multiple connection to the database
if(CONN == None):
conn = init(database)
CONN = conn
else:
conn = CONN
if isinstance(conn, str):
print(str)
sys.exit(1)
else:
conn = ""
return conn
# method from twatt project
def init(db):
try:
# init an interface to the database
database_link = "sqlite:///" + str(db)
# why echo twice on every item ?
engine = create_engine(database_link, echo = False)
# generate metadata for all tables in base model
Base.metadata.create_all(bind=engine)
init_session(engine)
# or work with conn to allow connection and sql langage execute statement
conn = engine.raw_connection()
return conn
except Exception as e:
return str(e)
# defining templates for tables to create when init
Base = declarative_base()
class MutableList(Mutable, list):
def append(self, value):
list.append(self, value)
self.changed()
@classmethod
def coerce(cls, key, value):
if not isinstance(value, MutableList):
if isinstance(value, list):
return MutableList(value)
return Mutable.coerce(key, value)
else:
return value
class waitlist(Base):
__tablename__ = 'waitlist'
player_hash_id = Column(Integer, primary_key = True, nullable = False)
player_name = Column(String, nullable = False)
player_aces = Column(Integer)
player_dfs = Column(Integer)
scraped = Column(Boolean, nullable = False, default = False)
class players(Base):
__tablename__ = 'players'
last_update = Column(String, nullable = False)
player_id = Column(Integer, primary_key = True)
player_hash_id = Column(Integer, primary_key = True, nullable = False)
player_name = Column(String, nullable = False)
player_height = Column(Float)
player_rank = Column(Float)
player_age = Column(Float)
# stats for last games
ace = Column(Float)
df = Column(Float)
svpt = Column(Float)
firstIn = Column(Float)
firstWon = Column(Float)
sndWon = Column(Float)
bpSaved = Column(Float)
bpFaced = Column(Float)
SvGms = Column(Float)
# dimensions of the array ?
#aces = Column(MutableList.as_mutableARRAY(Integer))
#dfs = Column(MutableList.as_mutableARRAY(Integer))
#svpts = Column(MutableList.as_mutableARRAY(Integer))
#firstIns = Column(MutableList.as_mutableARRAY(Integer))
#firstWons = Column(MutableList.as_mutableARRAY(Integer))
#sndWons = Column(MutableList.as_mutableARRAY(Integer))
#bpSaveds = Column(MutableList.as_mutableARRAY(Integer))
#bpFaceds = Column(MutableList.as_mutableARRAY(Integer))
#SvGms_s = Column(MutableList.as_mutableARRAY(Float))
def __init__(self, last_update, player_id, player_hash_id, player_name,
player_height, player_rank, player_age, ace, df, svpt, firstIn,
firstWon, sndWon, bpSaved, bpFaced, SvGms):
self.last_update = last_update
self.player_id = player_id
self.player_hash_id = player_hash_id
self.player_name = player_name
self.player_height = player_height
self.player_age = player_age
self.ace = ace
self.df = df
self.svpt = svpt
self.firstIn = firstIn
self.firstWon = firstWon
self.sndWon = sndWon
self.bpSaved = bpSaved
self.bpFaced = bpFaced
self.SvGms = SvGms
class games(Base):
__tablename__ = 'games'
match_id = Column(Integer, primary_key = True, nullable = False)
match_date = Column(String, nullable = False)
match_time = Column(String)
# created using a hashid local
tourney_id = Column(String)
tourney_name = Column(String)
surface = Column(String)
score = Column(String)
minutes = Column(Float)
# we consider player_1 to be the highest ranked player ?
player_1_id = Column(Integer, nullable = False)
player_1_name = Column(String, nullable = False)
player_1_rank = Column(Integer, nullable = False)
player_1_rank_points = Column(Integer)
player_1_ioc = Column(String)
player_1_ace = Column(Integer)
player_1_df = Column(Integer)
player_1_svpt = Column(Integer)
player_1_1stIn = Column(Integer)
player_1_1stWon = Column(Integer)
player_1_2ndWon = Column(Integer)
player_1_SvGms = Column(Integer)
player_1_bpf = Column(Integer)
player_1_bps = Column(Integer)
# and by default player_2 to be the least ranked
player_2_id = Column(Integer, nullable = False)
player_2_name = Column(String, nullable = False)
player_2_age = Column(Float)
player_2_height = Column(Float)
player_2_rank = Column(Integer, nullable = False)
player_2_rank_points = Column(Integer)
player_2_ioc = Column(String)
player_2_ace = Column(Integer)
player_2_df = Column(Integer)
player_2_svpt = Column(Integer)
player_2_1stIn = Column(Integer)
player_2_1stWon = Column(Integer)
player_2_2ndWon = Column(Integer)
player_2_SvGms = Column(Integer)
player_2_bpf = Column(Integer)
player_2_bps = Column(Integer)
# stats for the given player over the last five games
l5_player_1_ace = Column(Integer)
l5_player_1_df = Column(Integer)
l5_player_1_svpt = Column(Integer)
l5_player_1_1stIn = Column(Integer)
l5_player_1_1stWon = Column(Integer)
l5_player_1_2ndWon = Column(Integer)
l5_player_1_SvGms = Column(Integer)
l5_player_1_bpf = Column(Integer)
l5_player_1_bps = Column(Integer)
# stats for the given player over the last five games
l5_player_2_ace = Column(Integer)
l5_player_2_df = Column(Integer)
l5_player_2_svpt = Column(Integer)
l5_player_2_1stIn = Column(Integer)
l5_player_2_1stWon = Column(Integer)
l5_player_2_2ndWon = Column(Integer)
l5_player_2_SvGms = Column(Integer)
l5_player_2_bpf = Column(Integer)
l5_player_2_bps = Column(Integer)
# for the training games, where outcome of the game is known
# 1 for the player 1, 0 for the player 2
target = Column(Integer)
player_1_odd = Column(Integer)
player_2_odd = Column(Integer)
# % for player_1 to win
# for the record
prediction_ = Column(Integer)
# explicit the prediction of the algorithm
prediction_player = Column(String)
# the bet registered by the algorithm for this match
# decision made by the algorithm
bet = Column(Integer)
# outcome of the bet: if predicted player won, otherwise false
bet_result = Column(Integer)
def __init__(self, match_id, match_date, match_time, player_1_id,
player_1_name, player_1_odd, player_2_id, player_2_name, player_2_odd):
# add values for this game
# add the statistics for the last five games of each players
self.match_id = match_id
self.match_date = match_date
self.match_time = match_time
self.player_1_id = player_1_id
self.player_1_name = player_1_name
self.player_2_id = player_2_id
self.player_2_name = player_2_name
self.player_1_odd = player_1_odd
self.player_2_odd = player_2_odd
def create_hashID(search):
new_id = int(hashlib.sha256(search.encode('utf-8')).hexdigest(), 16) % 10**8
return new_id
# update the player profile
def update_player_stats(player):
session = get_session()
players_inDb = session.query(players.player_id).all()
if player.player_id not in players_inDb:
player = players(last_update = time.time(), player_id = player.player_id,
player_name = player.player_name, player_height = player.player_height,
player_age = player.player_age, ace = player.ace, df = player.df,
svpt = player.svpt, firstIn = player.firstIn, firstWon = player.firstWon,
sndWon = player.sndWon, bpSaved = player.bpSaved, bpFaced = player.bpFaced)
session.add(player)
session.commit()
if player.player_id in players_inDb:
player_ = session.query(players).filter_by(player_id = player.player_id).first()
player_.last_update = time.time()
player_.player_age = player.player_age
player_.ace = player.ace
player_.df = player.df
player_.svpt = player.svpt
player_.firstIn = player.firstIn
player_.firstWon = player.firstWon
player_.sndWon = player.sndWon
player_.bpSaved = player.bpSaved
player_.bpFaced = player.bpFaced
session.commit()
def add_match(match):
session = get_session()
games_inDb = session.query(games.match_id).all()
if match.match_id not in games_inDb:
game = games(match_id = match.match_id, match_date = match.match_date,
match_time = match.match_time, player_1_id = match.player_1_id,
player_1_name = match.player_1_name, player_1_odd = match.player_1_odd,
player_2_id = match.player_2_id, player_2_name =match.player_2_name,
player_2_odd = match.player_2_odd)
session.add(game)
session.commit()
# either precompute ids in scraper program or craft it here
players_inDb = session.query(players.player_id).all()
if match.player_1_id not in players_inDb:
player = players(last_update = time.time(), player_id = match.player_1_id,
player_name = match.player_1_name)
session.add(player)
session.commit()
if match.player_2_id not in players_inDb:
player = players(last_update = time.time(), player_id = match.player_2_id,
player_name = match.player_2_name)
session.add(player)
session.commit()