Advertisement
Guest User

Untitled

a guest
Feb 27th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.41 KB | None | 0 0
  1. __author__ = 'Willow'
  2. import mysql.connector
  3. import csv
  4.  
  5. class WDatabase:
  6.  
  7. def __init__(self):
  8. self.dbhost = '138.201.105.247'
  9. self.dbuser = 'willow'
  10. self.dbpass = 'ASDFqwer!@#$1234'
  11. self.dbname = 'oddmatcher'
  12. '''
  13. self.dbhost = 'localhost'
  14. self.dbuser = 'root'
  15. self.dbpass = 'ASDFqwer!@#$1234'
  16. #self.dbpass = ''
  17. '''
  18. self.cnx_object = mysql.connector.connect(user=self.dbuser, password=self.dbpass, host=self.dbhost, database=self.dbname)
  19.  
  20. def truncateTable(self):
  21. cursor = self.cnx_object.cursor(buffered=True)
  22. sql = "TRUNCATE TABLE bookmaker_event"
  23. cursor.execute(sql)
  24. sql = "TRUNCATE TABLE exchange_event"
  25. cursor.execute(sql)
  26. sql = "TRUNCATE TABLE match_log"
  27. cursor.execute(sql)
  28. sql = "TRUNCATE TABLE match_result"
  29. cursor.execute(sql)
  30. sql = "TRUNCATE TABLE adm_report"
  31. cursor.execute(sql)
  32. sql = "UPDATE adm_config SET last_b_id=0, last_e_id=0 WHERE id=1"
  33. cursor.execute(sql)
  34. self.cnx_object.commit()
  35. cursor.close()
  36.  
  37. def truncateEventTable(self, siteType, siteId):
  38. cursor = self.cnx_object.cursor(buffered=True)
  39. if siteType == 1:
  40. table = 'bookmaker_event'
  41. else:
  42. table = 'exchange_event'
  43. sql = "DELETE FROM "+table+" WHERE siteid = "+str(siteId)
  44. cursor.execute(sql)
  45. self.cnx_object.commit()
  46. cursor.close()
  47.  
  48. def truncateOddTable(self, siteType, siteId):
  49. cursor = self.cnx_object.cursor(buffered=True)
  50. if siteType == 1:
  51. sql = "DELETE FROM bookmaker_odd WHERE eventid IN (SELECT id FROM bookmaker_event WHERE siteid="+str(siteId)+")"
  52. else:
  53. sql = "DELETE FROM exchange_odd WHERE eventid IN (SELECT id FROM exchange_event WHERE siteid="+str(siteId)+")"
  54. cursor.execute(sql)
  55. self.cnx_object.commit()
  56. cursor.close()
  57.  
  58. def setUpdateFlag(self):
  59. try:
  60. cursor = self.cnx_object.cursor(buffered=True)
  61. sql ='UPDATE bookmaker_event SET update_flag=0 WHERE 1=1'
  62. cursor.execute(sql)
  63. sql ='UPDATE exchange_event SET update_flag=0 WHERE 1=1'
  64. cursor.execute(sql)
  65. self.cnx_object.commit()
  66. cursor.close()
  67. except Exception as e:
  68. print("ok")
  69.  
  70. def deleteRemovedEvents(self):
  71. try:
  72. cursor = self.cnx_object.cursor(buffered=True)
  73. sql = "DELETE FROM `match_result` WHERE bookmaker_event IN (SELECT id FROM bookmaker_event WHERE update_flag=0 )"
  74. cursor.execute(sql)
  75. self.cnx_object.commit()
  76.  
  77. sql = "DELETE FROM `match_result` WHERE exchange_event IN (SELECT id FROM exchange_event WHERE update_flag=0 )"
  78. cursor.execute(sql)
  79. self.cnx_object.commit()
  80.  
  81. sql = "DELETE FROM `match_log` WHERE bookmaker IN (SELECT id FROM bookmaker_event WHERE update_flag=0 )"
  82. cursor.execute(sql)
  83. self.cnx_object.commit()
  84.  
  85. sql = "DELETE FROM `match_log` WHERE exchange IN (SELECT id FROM exchange_event WHERE update_flag=0 )"
  86. cursor.execute(sql)
  87. self.cnx_object.commit()
  88.  
  89. sql = "DELETE FROM `exchange_event` WHERE update_flag=0"
  90. cursor.execute(sql)
  91. self.cnx_object.commit()
  92.  
  93. sql = "DELETE FROM `bookmaker_event` WHERE update_flag=0"
  94. cursor.execute(sql)
  95. self.cnx_object.commit()
  96. cursor.close()
  97. except Exception as e:
  98. self.log(0, "Database: deleteRemovedEvents >>exception " +str(e))
  99. cursor.close()
  100.  
  101. def deleteOldEvents(self):
  102. try:
  103. cursor = self.cnx_object.cursor(buffered=True)
  104. #sql = "DELETE FROM `match_result` WHERE match_result.datetime < NOW() - INTERVAL 8 HOUR"
  105. sql = "DELETE FROM `match_result` WHERE match_result.datetime < NOW()"
  106. sql = "DELETE FROM match_result WHERE id IN (SELECT * FROM (SELECT id FROM match_result WHERE match_result.datetime < NOW() order by id) AS X);"
  107. cursor.execute(sql)
  108. self.cnx_object.commit()
  109. sql = "DELETE FROM `exchange_event` WHERE exchange_event.datetime < NOW()"
  110. sql = "DELETE FROM exchange_event WHERE id IN (SELECT * FROM (SELECT id FROM exchange_event WHERE exchange_event.datetime < NOW() order by id) AS X)"
  111. cursor.execute(sql)
  112. self.cnx_object.commit()
  113. sql = "DELETE FROM `bookmaker_event` WHERE bookmaker_event.datetime < NOW()"
  114. sql = "DELETE FROM bookmaker_event WHERE id IN (SELECT * FROM (SELECT id FROM bookmaker_event WHERE bookmaker_event.datetime < NOW() order by id) AS X)"
  115. cursor.execute(sql)
  116. self.cnx_object.commit()
  117. sql = "DELETE FROM `match_log` WHERE match_log.datetime < NOW()"
  118. sql = "DELETE FROM match_log WHERE id IN (SELECT * FROM (SELECT id FROM match_log WHERE match_log.datetime < NOW() order by id) AS X) "
  119. cursor.execute(sql)
  120. self.cnx_object.commit()
  121. cursor.close()
  122. except Exception as e:
  123. self.log(0, "Database: deleteOldEvents >>exception " +str(e))
  124. cursor.close()
  125.  
  126.  
  127. def insertLeagueMatch(self, siteId, sportId, name, rowId=-1):
  128. '''
  129. cursor = self.cnx_object.cursor(buffered=True)
  130. sql = 'SELECT * FROM cfg_league_match WHERE s_id=%s AND league="%s" AND g_id=%s' % (siteId, name, sportId)
  131. cursor.execute(sql)
  132. if cursor.rowcount < 1:
  133. sql = 'INSERT INTO cfg_league_match(s_id, l_id, g_id, league) VALUES (%s, %s, %s, "%s")' % (siteId, rowId, sportId, name)
  134. cursor.execute(sql)
  135. self.cnx_object.commit()
  136. cursor.close()
  137. '''
  138. return
  139.  
  140. def insertLeague(self,sportId, name):
  141. row_no = -1
  142. cursor = self.cnx_object.cursor(buffered=True)
  143. sql = 'SELECT id FROM cfg_leagues WHERE league="%s" AND g_id=%s' % (name, sportId)
  144. cursor.execute(sql)
  145. if cursor.rowcount < 1:
  146. sql = ("INSERT INTO cfg_leagues"
  147. "(g_id, league)"
  148. "VALUES (%s, %s)")
  149. cursor.execute(sql , (sportId, name))
  150. self.cnx_object.commit()
  151. row_no = cursor.lastrowid
  152. else:
  153. row_no = cursor.fetchone()[0]
  154. return row_no
  155. cursor.close()
  156.  
  157. def insertEvent(self, name, league, datetime, siteid, gametype, sitetype, eventId):
  158. try:
  159. name = str(name).replace("\\", "")
  160. if str(league).lower().__contains__("fantasy"):
  161. return
  162. cursor = self.cnx_object.cursor(buffered=True)
  163. table = 'bookmaker_event'
  164. if sitetype == 1:
  165. table = 'bookmaker_event'
  166. else:
  167. table = 'exchange_event'
  168. sql = 'SELECT * FROM '+table+' WHERE name="'+name+'" AND siteid = '+str(siteid) +' AND gametype = ' + str(gametype)
  169. cursor.execute(sql)
  170.  
  171. if cursor.rowcount < 1:
  172. sql = ("INSERT INTO "+table+" "
  173. "(name, league, datetime, siteid, gametype, eventId, update_flag) "
  174. "VALUES (%s, %s, %s, %s, %s, %s, %s)")
  175. cursor.execute(sql , (name, league, datetime, siteid, gametype, eventId, 1))
  176. self.cnx_object.commit()
  177. else:
  178. sql ='UPDATE '+table+' SET update_flag=1 WHERE datetime="%s" AND name="%s" AND siteid = %s AND gametype = %s' % (datetime, name, siteid, gametype)
  179. self.cnx_object.commit()
  180. cursor.execute(sql)
  181. row_no = cursor.lastrowid
  182. cursor.close()
  183. return row_no
  184. except Exception as e:
  185. self.log(0, "Database: insertEvent >>exception " +str(e))
  186. cursor.close()
  187. return None
  188.  
  189. '''
  190. description: Under / Over 4.5
  191. name: : Under
  192. '''
  193. def insertOdd(self, sitetype, eventid, oddtype, description, name, odd, liquidity = 0):
  194. cursor1 = self.cnx_object.cursor(buffered=True)
  195. try:
  196. if sitetype == 1:
  197. sql ='UPDATE match_result SET backodd_update=1, backodd=%s WHERE bookmaker_event = %s AND runner="%s" AND market=%s' % (odd, eventid, name, oddtype)
  198. cursor1.execute(sql)
  199. #table = 'bookmaker_odd'
  200. #sql = ("INSERT INTO bookmaker_odd(eventid, oddtype, description, name, odd) VALUES (%s, %s, %s, %s, %s)")
  201. #cursor1.execute(sql , (eventid, oddtype, description,name, odd))
  202. else:
  203. sql ='UPDATE match_result SET layodd_update=1, layodd=%s, liquidity=%s WHERE exchange_event = %s AND runner="%s" AND market=%s' % (odd, liquidity, eventid, name, oddtype)
  204. cursor1.execute(sql)
  205. self.cnx_object.commit()
  206. cursor1.close()
  207. except Exception as e:
  208. self.log(0, "Database: insertOdd >>exception " +str(e))
  209. cursor1.close()
  210. return None
  211.  
  212.  
  213. def getSiteInfo(self, name):
  214. try:
  215. cursor = self.cnx_object.cursor(buffered=True)
  216. sql = "SELECT * FROM cfg_bookies WHERE link='"+name+"'"
  217. cursor.execute(sql)
  218. ret = cursor.fetchone()
  219. return ret
  220. except Exception as e:
  221. self.log(0, "Database: getSiteInfo >>exception " +str(e))
  222. return None
  223.  
  224. def getSports(self):
  225. try:
  226. cursor = self.cnx_object.cursor(buffered=True)
  227. sql = "SELECT * FROM cfg_games"
  228. cursor.execute(sql)
  229. ret = []
  230. results = cursor.fetchall()
  231. for row in results:
  232. sport = dict()
  233. sport[row[0]] = row
  234. ret.append(sport)
  235. return ret
  236. except Exception as e:
  237. self.log(0, "Database: getSports >>exception " +str(e))
  238. return None
  239.  
  240.  
  241. def getOdds(self, siteid):
  242. try:
  243. cursor = self.cnx_object.cursor(buffered=True)
  244. sql = "SELECT * FROM cfg_parse JOIN cfg_oddtype ON cfg_parse.oddId = cfg_oddtype.id WHERE cfg_parse.siteId='" + str(siteid) +"'"
  245. cursor.execute(sql)
  246. ret = []
  247. results = cursor.fetchall()
  248. for row in results:
  249. temp = dict()
  250. temp["id"] = row[2]
  251. temp["pattern"] = row[3]
  252. temp["sportId"] = row[5]
  253. ret.append(temp)
  254. return ret
  255. except Exception as e:
  256. self.log(0, "Database: getOdds >>exception " +str(e))
  257. return None
  258.  
  259. def loadSeparator(self, siteid):
  260. try:
  261. # Getting Team Separtor Letter
  262. cursor = self.cnx_object.cursor(buffered=True)
  263. sql = "SELECT * FROM cfg_team_sep WHERE siteId='"+str(siteid)+"'"
  264. cursor.execute(sql)
  265. row = cursor.fetchone()
  266. return row[2]
  267. except Exception as e:
  268. self.log(0, "Database: loadSeparator >>exception " +str(e))
  269. cursor.close()
  270. return None
  271.  
  272. # Read all matched events from 'matched_event' table by siteID
  273. # id, name, league, datetime, siteid, gametype, eventId (Table structure)
  274. def getMatchedEvents(self, siteId, siteType):
  275. try:
  276. ret = []
  277.  
  278. cursor = self.cnx_object.cursor(buffered=True)
  279. if siteType == 1:
  280. sql = "UPDATE match_result SET backodd_update=0 WHERE bookmaker='"+str(siteId)+"'"
  281. else:
  282. sql = "UPDATE match_result SET layodd_update=0 WHERE exchange='"+str(siteId)+"'"
  283. cursor.execute(sql)
  284. self.cnx_object.commit()
  285.  
  286. if siteType == 1:
  287. table_name = "bookmaker_event"
  288. sql = "SELECT DISTINCT(bookmaker_event) FROM match_result WHERE bookmaker='"+str(siteId)+"'"
  289. #sql = "SELECT DISTINCT(id) FROM bookmaker_event WHERE siteid='"+str(siteId)+"'"
  290. else:
  291. table_name = "exchange_event"
  292. sql = "SELECT DISTINCT(exchange_event) FROM match_result WHERE exchange='"+str(siteId)+"'"
  293. cursor.execute(sql)
  294. rows = cursor.fetchall()
  295. for row in rows:
  296. sql = 'SELECT * FROM %s WHERE id="%s" ' % (table_name, row[0])
  297. cursor.execute(sql)
  298. ret.append(cursor.fetchone())
  299. return ret
  300. except Exception as e:
  301. self.log(0, "Database: getMatchedEvents >>exception " +str(e))
  302. cursor.close()
  303.  
  304. def updateLastIds(self):
  305. cursor = self.cnx_object.cursor(buffered=True)
  306. sql = "SELECT id FROM bookmaker_event ORDER BY id DESC LIMIT 0,1"
  307. cursor.execute(sql)
  308. row = cursor.fetchone()
  309. if row == None:
  310. b_lastId = 0
  311. else:
  312. b_lastId = row[0]
  313. sql = "SELECT id FROM exchange_event ORDER BY id DESC LIMIT 0,1"
  314. cursor.execute(sql)
  315. row = cursor.fetchone()
  316. if row == None:
  317. e_lastId = 0
  318. else:
  319. e_lastId = row[0]
  320. sql ='UPDATE adm_config SET last_b_id=%s, last_e_id=%s WHERE id = %s' % (b_lastId, e_lastId, 1)
  321. cursor.execute(sql)
  322. self.cnx_object.commit()
  323. cursor.close()
  324.  
  325. def executeSQL(self, sql):
  326. cursor = self.cnx_object.cursor(buffered=True)
  327. cursor.execute(sql)
  328. self.cnx_object.commit()
  329. cursor.close()
  330.  
  331. # Sisal, Lottomatica
  332. def getMatchedLeagues(self, siteId, siteType):
  333. try:
  334. ret = []
  335. cursor = self.cnx_object.cursor(buffered=True)
  336. if siteType == 1:
  337. table_name = "bookmaker_event";
  338. sql = "SELECT DISTINCT(league) FROM bookmaker_event WHERE id IN ( SELECT DISTINCT(bookmaker_event) FROM match_result WHERE bookmaker=%s)" %(siteId)
  339. #sql = "SELECT DISTINCT(id) FROM bookmaker_event WHERE siteid='"+str(siteId)+"'"
  340. else:
  341. table_name = "exchange_event";
  342. sql = "SELECT DISTINCT(league) FROM exchange_event WHERE id IN ( SELECT DISTINCT(exchange_event) FROM match_result WHERE exchange=%s)" % (siteId)
  343. cursor.execute(sql)
  344. rows = cursor.fetchall()
  345. for row in rows:
  346. if str(row[0]).__contains__('"'):
  347. sql = "SELECT * FROM %s WHERE league = '%s' AND siteid=%s" % ( table_name, str(row[0]) , siteId)
  348. else:
  349. sql = 'SELECT * FROM %s WHERE league = "%s" AND siteid=%s' % ( table_name, str(row[0]) ,siteId)
  350. cursor.execute(sql)
  351. record = cursor.fetchone()
  352. ret.append(record)
  353. return ret
  354. except Exception as e:
  355. self.log(0, "Database: getMatchedLeagues >>exception " +str(e))
  356. cursor.close()
  357.  
  358. def updateEventReport(self, siteId, f_count, t_count, b_count, takeTime, endTime):
  359. try:
  360. cursor = self.cnx_object.cursor(buffered=True)
  361. sql ="SELECT * FROM adm_report WHERE siteId="+str(siteId)
  362. cursor.execute(sql)
  363. if cursor.rowcount < 1:
  364. sql = ("INSERT INTO adm_report (siteId, f_count, t_count, b_count, event_taken_time,last_event_time) VALUES (%s, %s, %s, %s, %s, %s)")
  365. cursor.execute(sql , (siteId, f_count, t_count,b_count, takeTime, endTime))
  366. else:
  367. sql ='UPDATE adm_report SET f_count=%s, t_count=%s, b_count=%s, event_taken_time=%s, last_event_time ="%s", e_update_count=e_update_count + 1 WHERE siteId=%s ' % (f_count, t_count, b_count, takeTime, endTime, siteId)
  368. cursor.execute(sql)
  369. self.cnx_object.commit()
  370. except Exception as e:
  371. self.log(0, "Database: updateEventReport >>exception " +str(e))
  372. cursor.close()
  373.  
  374. def updateOddReport(self,siteType, siteId, m_count, odd_count, takenTime, endTime):
  375. cursor = self.cnx_object.cursor(buffered=True)
  376. try:
  377.  
  378. sql ="SELECT * FROM adm_report WHERE siteId="+str(siteId)
  379. cursor.execute(sql)
  380. if cursor.rowcount < 1:
  381. sql = ("INSERT INTO adm_report (siteId, m_count, oddcount, odd_taken_time,last_odd_time) VALUES (%s, %s, %s, %s, %s)")
  382. cursor.execute(sql , (siteId, m_count, odd_count, takenTime, endTime))
  383. else:
  384. sql ='UPDATE adm_report SET m_count=%s, oddcount=%s, odd_taken_time=%s, last_odd_time ="%s", o_update_count=o_update_count+1 WHERE siteId=%s ' % (m_count, odd_count,takenTime, endTime, siteId)
  385. cursor.execute(sql)
  386. self.cnx_object.commit()
  387.  
  388.  
  389. if siteType == 1:
  390. sql = "UPDATE match_result SET backodd=0 WHERE id IN (SELECT * FROM (SELECT id FROM match_result WHERE bookmaker='%s' AND backodd_update = 0) AS X)" % (siteId)
  391. else:
  392. sql = "UPDATE match_result SET layodd=0 WHERE id IN (SELECT * FROM (SELECT id FROM match_result WHERE exchange='%s' AND layodd_update = 0) AS X)" % (siteId)
  393.  
  394. cursor.execute(sql)
  395. self.cnx_object.commit()
  396.  
  397. except Exception as e:
  398. self.log(0, "Database: updateOddReport >>exception " +str(e))
  399. cursor.close()
  400.  
  401. def getTimerConfig(self):
  402. try:
  403. cursor = self.cnx_object.cursor(buffered=True)
  404. sql ='SELECT e_timer, o_timer FROM adm_config WHERE id = 1'
  405. cursor.execute(sql)
  406. row = cursor.fetchone()
  407. cursor.close()
  408. if row != None:
  409. return row
  410. else:
  411. return None
  412. except Exception as e:
  413. self.log(0, "Database: getTimerConfig >>exception " +str(e))
  414. cursor.close()
  415. return None
  416.  
  417.  
  418. def getActive(self, siteId):
  419. try:
  420. cursor = self.cnx_object.cursor(buffered=True)
  421. sql ='SELECT active FROM cfg_bookies WHERE id = ' + str(siteId)
  422. cursor.execute(sql)
  423. row = cursor.fetchone()
  424. cursor.close()
  425. return row[0]
  426. except Exception as e:
  427. self.log(0, "Database: getActive >>exception " +str(e))
  428. cursor.close()
  429. return 0
  430.  
  431. def log(self, type, message):
  432. print(message)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement