roll_lanqiu.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. import datetime
  2. import pymongo
  3. import time
  4. import logging
  5. from twisted.internet import defer, reactor
  6. # from .ball_func import
  7. from ..utils.helper import Helper
  8. from ..settings import M_HOST, M_USER, M_PASSWORD, M_POST, M_DB, LEAGUE_URL, ODDS_URL, MATCH_URL, MATCH_RESULT, MATCH_STATUS
  9. class Roll_Lanqiupipeline(object):
  10. def open_spider(self, spider):
  11. self.mongo = pymongo.MongoClient(host=M_HOST, username=M_USER, password=M_PASSWORD, port=M_POST)
  12. self.db = self.mongo[M_DB]
  13. @defer.inlineCallbacks
  14. def process_item(self, item, spider):
  15. out = defer.Deferred()
  16. reactor.callInThread(self._do_calculation, item, out)
  17. yield out
  18. defer.returnValue(item)
  19. # def process_item(self, item, spider):
  20. def _do_calculation(self, item, out):
  21. # 使用twisted将mysql插入变成异步执行
  22. logger = logging.getLogger(__name__)
  23. # 联赛id
  24. league_id = item['league_id']
  25. # 联赛名
  26. league_name = item['league_name']
  27. # 比赛id
  28. match_id = item['game_id']
  29. # 球队1 #home_team
  30. team_home = item['team_home']
  31. # 球队2 # guest_team
  32. team_guest = item['team_guest']
  33. # 数量(97>)
  34. number = item['number']
  35. # 比赛状态
  36. result_mark = item['score_dict']
  37. try:
  38. data_game = item['data_game'].split("/")
  39. month = str(data_game[1].strip())
  40. day = str(data_game[0])
  41. except Exception as e:
  42. logger.warning(e)
  43. data_game = item['data_game'].split(" ")
  44. months = str(data_game[1].strip())
  45. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  46. 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  47. month = month_dict[months]
  48. day = str(data_game[0])
  49. # 比赛时间
  50. time_game = str(item['time_game'])
  51. # 现在时间,时间戳
  52. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  53. match_date = datetime.datetime.now().strftime("%Y-%m-%d")
  54. # 队1分数
  55. score_home = item['score_home']
  56. # 队2分数
  57. score_guest = item['score_guest']
  58. # 第几节
  59. jijie = item['jijie']
  60. # 球队得分
  61. pt = 4
  62. match_score = "{}:{}".format(score_home, score_guest)
  63. concedes_dict = item['concede']
  64. concedes_dict_rule = item['concede_rule']
  65. odd_evens_dict = item['odd_even']
  66. odd_evens_dict_rule = item['odd_even_rule']
  67. total_sizes_dict = item['total_size']
  68. total_sizes_dict_rule = item['total_size_rule']
  69. last_numbers_dict = item['last_number']
  70. capots_dict = item['capot']
  71. team_scores_dict = item['team_score']
  72. team_scores_dict_rule = item['team_score_rule']
  73. last_time = "{}-12-31 23:59:59".format(datetime.datetime.now().year)
  74. uuid = Helper.genearte_uuid(league_name + 'hg3535')
  75. if self.db.lq_league35222.find({'lg_id': league_id}).count() < 1:
  76. league_dict = {"game_code": "lq", "title": "league", "source": "hg3535"}
  77. league_list = []
  78. league_key = ["name_chinese", "kind", "match_mode", "if_stop", "last_time", "lg_id", "source", "uuid"]
  79. league_value = [league_name, "1", "1", "0", last_time, league_id, "hg3535", uuid]
  80. league_data = dict(zip(league_key, league_value))
  81. league_list.append(league_data)
  82. league_dict['data'] = league_list
  83. res = Helper.async_post(LEAGUE_URL, league_dict)
  84. if res:
  85. if '成功' in res:
  86. logger.info('篮球滚球联赛提交成功, {}'.format(res))
  87. self.db.lq_league35222.insert(league_data)
  88. logger.info(league_dict)
  89. else:
  90. logger.warning('篮球滚球联赛提交失败, {}'.format(res))
  91. else:
  92. logger.warning('篮球滚球联赛接口, 提交失败, {}'.format(res))
  93. logger.warning(league_dict)
  94. else:
  95. logger.info('篮球滚球联赛已存在')
  96. if self.db.lq_competition35.find({"match_id": match_id, 'is_rollball': 1}).count() < 1:
  97. match_list = []
  98. match_dict = {"game_code": "lq", "title": "match", "source": "hg3535"}
  99. match_kay = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  100. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  101. "half_match_id", "is_today", "is_horn"]
  102. match_value = [team_home, team_guest, league_id, 1, match_id, match_date, "00:00", number,
  103. "hg3535", 1, 0, 0, utime, uuid, 0, 0, 0]
  104. match_data = dict(zip(match_kay, match_value))
  105. match_list.append(match_data)
  106. match_dict['data'] = match_list
  107. res = Helper.async_post(MATCH_URL, match_dict)
  108. if res:
  109. if '成功' in res:
  110. logger.info('篮球滚球赛事提交成功, {}'.format(res))
  111. self.db.hg3535_lq_competition.insert(match_data)
  112. logger.info(match_dict)
  113. else:
  114. logger.warning('篮球滚球赛事提交失败, {}'.format(res))
  115. logger.warning(match_dict)
  116. else:
  117. logger.warning('篮球滚球赛事接口异常,提交失败, {}'.format(res))
  118. logger.warning(match_dict)
  119. else:
  120. logger.info('篮球滚球赛事已存在')
  121. # 赔率
  122. data_list = []
  123. odds_onlys = []
  124. if concedes_dict:
  125. for key, value in concedes_dict.items():
  126. if value:
  127. for index, concede_value in enumerate(value):
  128. hash_str = "CO" + str(key) + str(index) + str(concedes_dict_rule[key][index]) + str(
  129. concede_value) + "hg3535" + str(match_id)
  130. sole_str = "CO" + str(key) + str(index) + str(match_id) + "hg3535"
  131. odds_only = Helper.genearte_MD5(hash_str, pt)
  132. sole = Helper.genearte_MD5(sole_str, pt)
  133. concede_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  134. "condition", "odds_only", "sole", "source", "type", "team"]
  135. condition = concedes_dict_rule[key][index]
  136. concede_value = [match_id, league_id, key, "0", index, 'CO', concede_value, condition,
  137. odds_only, sole, "hg3535", "0", ""]
  138. concede_data = dict(zip(concede_key, concede_value))
  139. data_list.append(concede_data)
  140. if total_sizes_dict:
  141. for key, value in total_sizes_dict.items():
  142. if value:
  143. for index, total_sizes_value in enumerate(value):
  144. condition = total_sizes_dict_rule[key][index]
  145. hash_str = "TN" + str(key) + str(index) + str(condition) + str(
  146. total_sizes_value) + "hg3535" + str(match_id)
  147. sole_str = "TN" + str(key) + str(index) + str(match_id) + "hg3535"
  148. odds_only = Helper.genearte_MD5(hash_str, pt)
  149. sole = Helper.genearte_MD5(sole_str, pt)
  150. total_sizes_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  151. "condition", "odds_only", "sole", "source", "type", "team"]
  152. total_sizes_value = [match_id, league_id, key, "0", index, 'TN', total_sizes_value,
  153. condition, odds_only, sole, "hg3535", "0", ""]
  154. total_sizes_data = dict(zip(total_sizes_key, total_sizes_value))
  155. data_list.append(total_sizes_data)
  156. if odd_evens_dict:
  157. for key, value in odd_evens_dict.items():
  158. if value:
  159. condition = odd_evens_dict_rule[key]
  160. hash_str = "TS" + str(key) + '0' + str(condition) + str(
  161. value) + "hg3535" + str(match_id)
  162. sole_str = "TS" + str(key) + '0' + str(match_id) + "hg3535"
  163. odds_only = Helper.genearte_MD5(hash_str, pt)
  164. sole = Helper.genearte_MD5(sole_str, pt)
  165. odd_evens_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  166. "condition", "odds_only", "sole", "source", "type", "team"]
  167. odd_evens_value = [match_id, league_id, key, "0", 0, 'TS', value,
  168. condition, odds_only, sole, "hg3535", "0", ""]
  169. odd_evens_data = dict(zip(odd_evens_key, odd_evens_value))
  170. data_list.append(odd_evens_data)
  171. lastnumber_dict = {'0或5': 'lnh0', '1或6': 'lnh1', '2或7': 'lnh2', '3或8': 'lnh3', '4或9': 'lnh4'}
  172. last_home = last_numbers_dict['last_home']
  173. if last_home:
  174. for key, value in last_home.items():
  175. odds_code =lastnumber_dict[key]
  176. hash_str = "LN" + odds_code + '0' + str(key) + str(
  177. value) + "hg3535" + str(match_id)
  178. sole_str = "LN" + str(key) + '0' + str(match_id) + "hg3535"
  179. odds_only = Helper.genearte_MD5(hash_str, pt)
  180. sole = Helper.genearte_MD5(sole_str, pt)
  181. last_home_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  182. "condition", "odds_only", "sole", "source", "type", "team"]
  183. last_home_value = [match_id, league_id, odds_code, "0", 0, 'LN', value,
  184. key, odds_only, sole, "hg3535", "0", ""]
  185. last_home_data = dict(zip(last_home_key, last_home_value))
  186. data_list.append(last_home_data)
  187. last_guest = last_numbers_dict['last_guest']
  188. if last_guest:
  189. for key, value in last_guest.items():
  190. odds_code = lastnumber_dict[key]
  191. # condition = lastnumber_dict[key]
  192. hash_str = "LN" + odds_code + '0' + str(key) + str(value) + "hg3535" + str(match_id)
  193. sole_str = "LN" + str(key) + '0' + str(match_id) + "hg3535"
  194. odds_only = Helper.genearte_MD5(hash_str, pt)
  195. sole = Helper.genearte_MD5(sole_str, pt)
  196. last_guest_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  197. "condition", "odds_only", "sole", "source", "type", "team"]
  198. last_guest_value = [match_id, league_id, odds_code, "0", 0, 'LN', value,
  199. key, odds_only, sole, "hg3535", "0", ""]
  200. last_guest_data = dict(zip(last_guest_key, last_guest_value))
  201. data_list.append(last_guest_data)
  202. if capots_dict:
  203. for key, value in capots_dict.items():
  204. if value:
  205. hash_str = "C" + str(key) + '0' + '0' + str(value) + "hg3535" + str(match_id)
  206. sole_str = "C" + str(key) + '0' + str(match_id) + "hg3535"
  207. odds_only = Helper.genearte_MD5(hash_str, pt)
  208. sole = Helper.genearte_MD5(sole_str, pt)
  209. capots_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  210. "condition", "odds_only", "sole", "source", "type", "team"]
  211. capots_value = [match_id, league_id, key, "0", 0, 'C', value,
  212. "", odds_only, sole, "hg3535", "0", ""]
  213. capots_data = dict(zip(capots_key, capots_value))
  214. data_list.append(capots_data)
  215. if team_scores_dict:
  216. for key, team_scores_value in team_scores_dict.items():
  217. if team_scores_value:
  218. for index, value in enumerate(team_scores_value):
  219. hash_str = "TB" + str(key) + str(index) + str(team_scores_dict_rule[key][index]) + str(
  220. value) + "hg3535" + str(match_id)
  221. sole_str = "TB" + str(key) + str(index) + str(match_id) + "hg3535"
  222. odds_only = Helper.genearte_MD5(hash_str, pt)
  223. sole = Helper.genearte_MD5(sole_str, pt)
  224. team_scores_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  225. "condition", "odds_only", "sole", "source", "type", "team"]
  226. condition = team_scores_dict_rule[key][index]
  227. team_scores_value = [match_id, league_id, key, "0", index, 'TB', value, condition,
  228. odds_only, sole, "hg3535", "0", ""]
  229. team_scores_data = dict(zip(team_scores_key, team_scores_value))
  230. data_list.append(team_scores_data)
  231. odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid",
  232. "is_stringscene", "utime", "pt"]
  233. odds_value = ["lq", "odds", match_id, league_id, data_list, "hg3535", odds_onlys, number, uuid,
  234. 0, utime, pt]
  235. odds_dict = dict(zip(odds_key, odds_value))
  236. if data_list:
  237. res = Helper.async_post(ODDS_URL, odds_dict)
  238. if res:
  239. if "成功" in res:
  240. logger.info('篮球滚球详细赔率提交成功, {}'.format(res))
  241. else:
  242. logger.warning('篮球滚球详细赔率提交失败, {}'.format(res))
  243. logger.warning(odds_dict)
  244. else:
  245. logger.warning('篮球滚球详细赔率接口异常, 提交失败, {}'.format(res))
  246. logger.warning(odds_dict)
  247. else:
  248. logger.info('篮球滚球详细赔率列表, 不提交, {}'.format(res))
  249. data_list = []
  250. lq_rball = {"home_team": team_home, "guest_team": team_guest,
  251. "lg_id": league_id, "home_rate": 0,
  252. "guest_rate": 0, "home_score": score_home,
  253. "guest_score": score_guest, "all_goal": "", "status": 1,
  254. "first_score": "", "last_score": "", "match_score": match_score, "uuid": uuid,
  255. "match_winer": "", "match_time": time_game, "u_home_score": "", "u_guest_score": "",
  256. "match_process": jijie, "tag": number, "result_mark": result_mark,
  257. "match_id": match_id, "p_code": ""}
  258. data_list.append(lq_rball)
  259. r_data_dict = {
  260. "game_code": "lq",
  261. "title": "match_result_r",
  262. "source": "hg3535",
  263. "data": data_list
  264. }
  265. if data_list:
  266. try:
  267. res = Helper.async_post(MATCH_RESULT, r_data_dict)
  268. if "成功" in res:
  269. logger.info('篮球滚球赛事结果记录提交成功, {}'.format(res))
  270. else:
  271. logger.warning('篮球滚球赛事结果记录提交失败, {}'.format(res))
  272. logger.warning(r_data_dict)
  273. except Exception as e:
  274. logger.warning('篮球滚球赛事结果数据,接口异常,提交失败, {}'.format(e))
  275. else:
  276. logger.info('篮球滚球赛事结果数据,为空不提交')
  277. # status_dict = {"game_code": 'lq', "title": "match_status", "source": "hg3535"}
  278. # data_list = []
  279. # data = {'match_id': match_id, 'status': 1, "is_rollball": 0, "is_today": 0, "is_morningplate": 0,
  280. # "is_stringscene": 0, "is_horn": 0}
  281. # data_list.append(data)
  282. # status_dict['data'] = data_list
  283. # res = Helper.async_post(MATCH_STATUS, status_dict)
  284. # if res:
  285. # if "成功" in res:
  286. # self.db.match_status35.insert(status_dict)
  287. # logger.info('{},赛事结果状态交成功, {}'.format('lq', res))
  288. # logger.info(status_dict)
  289. # else:
  290. # logger.warning('{},赛事结果状态交失败, {}'.format('lq', res))
  291. # logger.warning(status_dict)
  292. # else:
  293. # logger.warning('{},赛事结果状态接口异常提交失败, {}'.format('lq', res))
  294. # logger.warning(status_dict)
  295. reactor.callFromThread(out.callback, item)