Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.04 KB | None | 0 0
  1. # coding=utf-8
  2. __author__ = 'Willow'
  3. import mysql.connector
  4. import threading
  5. import csv
  6. import re
  7. from datetime import datetime
  8. import jaro
  9. import itertools
  10.  
  11.  
  12. class DutchMatcher:
  13.  
  14. def __init__(self):
  15. self.dbhost = 'localhost'
  16. self.dbuser = 'root'
  17. self.dbpass = 'ASDFqwer!@#$1234'
  18. self.dbpass = ''
  19. self.dbname = 'oddmatcher'
  20. '''
  21. self.dbhost = '138.201.105.247'
  22. self.dbuser = 'willow'
  23. self.dbpass = 'ASDFqwer!@#$1234'
  24. #self.dbpass = ''
  25. self.dbname = 'oddmatcher'
  26. '''
  27. self.cnx_object = mysql.connector.connect(user=self.dbuser, password=self.dbpass, host=self.dbhost, database=self.dbname)
  28.  
  29. #Last Id
  30. self.last_b_id = 0
  31. self.last_e_id = 0
  32. #self.getLastIds()
  33. self.threadCound = 30
  34.  
  35. self.runnerTypes = {1: {
  36. 1: ["1", "X", "2"],
  37. 2: ["goal", "nogoal"],
  38. 3: [
  39. ["under15", "over15"],
  40. ["under25", "over25"],
  41. ["under35", "over35"],
  42. ["under45", "over45"]
  43. ]},
  44. 2: {4: ["1", "2"]},
  45. 3: {5: ["1", "2"]}
  46. }
  47.  
  48. def getLastIds(self):
  49. cursor = self.cnx_object.cursor(buffered=True)
  50. sql ='SELECT last_b_id, last_e_id FROM adm_config WHERE id = 1'
  51. cursor.execute(sql)
  52. row = cursor.fetchone()
  53. if row != None:
  54. self.last_b_id = row[0]
  55. self.last_e_id = row[1]
  56.  
  57. def start(self):
  58. cursor = self.cnx_object.cursor(buffered=True)
  59. # 새로 추가된 매치들의 exchange_event 들을 얻는다
  60. sql = "SELECT DISTINCT(exchange_event) FROM match_result WHERE exchange_event IN (SELECT DISTINCT(exchange_event) FROM match_result WHERE bookmaker_event>%s) AND exchange_event>%s" % (self.last_b_id, self.last_e_id)
  61. cursor.execute(sql)
  62. self.rows = cursor.fetchall()
  63. for i in range(0, self.threadCound+1):
  64. thread = threading.Thread(target=self.matcherThread, args=[i])
  65. thread.start()
  66. cursor.close()
  67.  
  68. def matcherThread(self, index):
  69. cnx_object = mysql.connector.connect(user=self.dbuser, password=self.dbpass, host=self.dbhost, database=self.dbname)
  70. cursor = cnx_object.cursor(buffered=True)
  71. startPos = index * len(self.rows) / self.threadCound
  72. endPos = (index + 1) * len(self.rows) / self.threadCound
  73. if endPos > len(self.rows):
  74. endPos = len(self.rows)
  75. for i in range(startPos, endPos):
  76. row = self.rows[i]
  77. # 매 exchange_event 에 관한 bookmaker_event 들을 얻는다.
  78. sql = "SELECT DISTINCT(bookmaker_event) FROM match_result WHERE exchange_event=%s" % (row[0])
  79. cursor.execute(sql)
  80. subrows = cursor.fetchall()
  81. # 같은 exchange_event 에 대응하는 bookmaker_event 들이 3개 이상이면 그것들의 조합을 계산하고 매 조합에 관해서 순렬을 계산한다.
  82. if cursor.rowcount >=3:
  83. # 조합생성
  84. results = itertools.combinations(subrows, 3)
  85. for x in list(results):
  86. # 순렬 생성
  87. for y in list(itertools.permutations(x)):
  88. ids = []
  89. logRows = []
  90. for z in y:
  91. ids.append(z[0])
  92. sql = "SELECT * FROM match_result WHERE bookmaker_event=%s AND exchange_event=%s LIMIT 1" % (z[0], row[0])
  93. cursor.execute(sql)
  94. logRows.append(cursor.fetchone())
  95. self.insertMatch(cnx_object, ids, logRows)
  96.  
  97. def insertMatch(self, cnx_object, bookmakerIds, logRows):
  98. cursor = cnx_object.cursor(buffered=True)
  99. try:
  100. # 세개의 행들중에서 같은 bookmaker에 해당한 이벤트들이 존재하면 귀한한다.
  101. if logRows[0][6] == logRows[1][6] or logRows[1][6] == logRows[2][6] or logRows[0][6] == logRows[2][6]:
  102. return
  103. sql = "SELECT * FROM `match_dutch_result` WHERE bookmaker1_event=%s AND bookmaker2_event = %s AND bookmaker3_event = %s" % (
  104. bookmakerIds[0], bookmakerIds[1], bookmakerIds[2])
  105. cursor.execute(sql)
  106. if cursor.rowcount < 1:
  107. sql = ("INSERT INTO `match_dutch_result` "
  108. "(`datetime`, "
  109. "`sport`, "
  110. "`eventname`, "
  111. "`bookmaker1`, "
  112. "`bookmaker1_event`, "
  113. "`bookmaker1_active`, "
  114. "`bookmaker2`, "
  115. "`bookmaker2_event`, "
  116. "`bookmaker2_active`, "
  117. "`bookmaker3`, "
  118. "`bookmaker3_event`, "
  119. "`bookmaker3_active`, "
  120. "`market` ,"
  121. "`runner1`, "
  122. "`runner2`, "
  123. "`runner3`, "
  124. "`logid`) "
  125. "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
  126. )
  127. insertData = []
  128. for key in dict(self.runnerTypes[int(logRows[0][2])]).keys():
  129. item = self.runnerTypes[int(logRows[0][2])][key]
  130. if key != 3:
  131. item3 = ""
  132. if len(item) >= 3:
  133. item3 = item[2]
  134. insertData.append((logRows[0][1], logRows[0][2], logRows[0][3],
  135. logRows[0][6], logRows[0][8], logRows[0][9],
  136. logRows[1][6], logRows[1][8], logRows[1][9],
  137. logRows[2][6], logRows[2][8], logRows[2][9],
  138. key, item[0], item[1], item3, logRows[0][19]
  139. ))
  140. else:
  141. for subitem in item:
  142. insertData.append((logRows[0][1], logRows[0][2], logRows[0][3],
  143. logRows[0][6], logRows[0][8], logRows[0][9],
  144. logRows[1][6], logRows[1][8], logRows[1][9],
  145. logRows[2][6], logRows[2][8], logRows[2][9],
  146. key, subitem[0], subitem[1], None, logRows[0][19]
  147. ))
  148. cursor.executemany(sql, insertData)
  149. self.cnx_object.commit()
  150. except Exception as e:
  151. print(str(e))
  152. cursor.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement