roll_bangqiu.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import datetime
  2. import pymongo
  3. import time
  4. import logging
  5. # import json
  6. from twisted.internet import defer, reactor
  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_Banqiupipeline(object):
  10. def open_spider(self, spider):
  11. self.mongo = pymongo.MongoClient(host=M_HOST, username=M_USER, password=M_PASSWORD, port=M_POST, authSource='kaiyou')
  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
  30. team_home = item['team_home']
  31. # 球队2
  32. team_guest = item['team_guest']
  33. # 数量(97>)
  34. tag_number = item['number']
  35. # 日期
  36. data_game = item['data_game'].split("/")
  37. month = str(data_game[1].strip())
  38. day = str(data_game[0])
  39. # 比赛时间
  40. time_game = str(item['time_game'])
  41. # 比赛时间,时间戳
  42. ctime = "2019" + "-" + month + "-" + day + "" + time_game + ":00".strip()
  43. # 现在时间,时间戳
  44. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  45. cdate = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  46. match_date = cdate.split(' ')[0]
  47. match_time = cdate.split(' ')[1]
  48. # 队1分数
  49. score_home = item['score_home']
  50. # 队2分数
  51. score_guest = item['score_guest']
  52. # 第几节
  53. jijie = item['jijie']
  54. pt = 4
  55. # 滚球这个位置获取不到这些字段
  56. # match_date, match_time = new_time(ctime)
  57. # 让球
  58. concedes_dict = item['concedes_dict']
  59. concedes_dict_rule = item['concedes_dict_rule']
  60. # 独赢
  61. capots_dict = item['capots_dict']
  62. # 总得分大小
  63. total_size_dict = item['total_size_dict']
  64. total_size_dict_rule = item['total_size_dict_rule']
  65. # 总得分单双
  66. odd_evens_dict = item['odd_evens_dict']
  67. odd_evens_dict_rule = item['odd_evens_dict_rule']
  68. # 具体比分
  69. source_dict = item['source_dict']
  70. uuid = Helper.genearte_uuid(league_name + 'hg3535')
  71. """联赛"""
  72. last_time = '{}-12-31 23:59:59'.format(datetime.datetime.now().year)
  73. if self.db.bq_league35.find({'lg_id': league_id}).count() < 1:
  74. league_dict = {"game_code": "bq", "title": "league", "source": "hg3535"}
  75. league_list = []
  76. league_key = ["name_chinese", "kind", "match_mode", "if_stop", "last_time", "lg_id", "source", "uuid"]
  77. league_value = [league_name, "1", "1", "0", last_time, league_id, "hg3535", uuid]
  78. league_data = dict(zip(league_key, league_value))
  79. league_list.append(league_data)
  80. league_dict['data'] = league_list
  81. res = Helper.async_post(LEAGUE_URL, league_dict)
  82. if res:
  83. if '成功' in res:
  84. self.db.bq_league35.insert(league_data)
  85. # self.db.bq_competition35.update({'lg_id': league_id}, {'$set': league_data},
  86. # upsert=True)
  87. logger.info('棒球滚球联赛提交成功, {}'.format(res))
  88. else:
  89. logger.warning('棒球滚球联赛提交失败, {}'.format(res))
  90. logger.warning(league_dict)
  91. else:
  92. logger.warning('棒球滚球联赛,接口异常,提交失败, {}'.format(res))
  93. logger.warning(league_dict)
  94. else:
  95. logger.info('棒球滚球联赛, 已存在不提交')
  96. """赛事"""
  97. if self.db.bq_competition35.find({"match_id": match_id, 'is_rollball': 1}).count() < 1:
  98. match_list = []
  99. match_dict = {"game_code": "bq", "title": "match", "source": "hg3535"}
  100. match_key = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  101. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  102. "half_match_id", "is_today", "is_horn"]
  103. match_value = [team_home, team_guest, league_id, 1, match_id, match_date, match_time, tag_number,
  104. "hg3535", 1, 0, 0, utime, uuid, 0, 0, 0]
  105. match_data = dict(zip(match_key, match_value))
  106. match_list.append(match_data)
  107. match_dict['data'] = match_list
  108. res = Helper.async_post(MATCH_URL, match_dict)
  109. if res:
  110. if '成功' in res:
  111. self.db.bq_competition35.insert(match_data)
  112. # self.db.bq_competition35.update({"match_id": match_id, 'is_rollball': 1}, {'$set': match_data},
  113. # upsert=True)
  114. else:
  115. logger.warning('棒球滚球赛事提交失败, {}'.format(res))
  116. logger.warning(match_data)
  117. else:
  118. logger.warning('棒球滚球赛事,接口异常,提交失败, {}'.format(res))
  119. logger.warning(match_data)
  120. else:
  121. logger.info("棒球赛事已存在,棒球赛事状态时间未改变,不做修改")
  122. """赔率"""
  123. data_list = []
  124. odds_onlys = []
  125. # 让球
  126. if concedes_dict:
  127. for key, value in concedes_dict.items():
  128. if value:
  129. sole_str = 'CO' + str(key) + '0' + str(match_id) + 'hg3535'
  130. sole = Helper.genearte_MD5(sole_str, pt)
  131. odds_str = 'CO' + str(key) + '0' + str(concedes_dict_rule[key]) + str(
  132. value) + 'hg3535' + str(match_id)
  133. odds_only = Helper.genearte_MD5(odds_str, pt)
  134. condition = concedes_dict_rule[key]
  135. concedes_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  136. "condition", "odds_only", "sole", "source", "type", "team"]
  137. concedes_value = [match_id, league_id, key, "0", 0, 'CO', value, condition,
  138. odds_only, sole, "hg3535", "0", ""]
  139. concedes_data = dict(zip(concedes_key, concedes_value))
  140. data_list.append(concedes_data)
  141. # 独赢
  142. if capots_dict:
  143. for key, value in capots_dict.items():
  144. if value:
  145. sole_str = 'C' + str(key) + '0' + str(match_id) + 'hg3535'
  146. sole = Helper.genearte_MD5(sole_str, pt)
  147. odds_str = 'C' + str(key) + '0' + '0' + str(value) + 'hg3535' + str(match_id)
  148. odds_only = Helper.genearte_MD5(odds_str, pt)
  149. capots_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  150. "condition", "odds_only", "sole", "source", "type", "team"]
  151. capots_value = [match_id, league_id, key, "0", 0, 'C', value, '',
  152. odds_only, sole, "hg3535", "0", ""]
  153. capots_data = dict(zip(capots_key, capots_value))
  154. data_list.append(capots_data)
  155. # 总得分大小
  156. if total_size_dict:
  157. for key, value in total_size_dict.items():
  158. if value:
  159. sole_str = 'TN' + str(key) + '0' + str(match_id) + 'hg3535'
  160. sole = Helper.genearte_MD5(sole_str, pt)
  161. odds_str = 'TN' + str(key) + '0' + str(total_size_dict_rule[key]) + str(
  162. value) + 'hg3535' + str(match_id)
  163. odds_only = Helper.genearte_MD5(odds_str, pt)
  164. condition = total_size_dict_rule[key]
  165. total_size_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  166. "condition", "odds_only", "sole", "source", "type", "team"]
  167. total_size_value = [match_id, league_id, key, "0", 0, 'TN', value, condition,
  168. odds_only, sole, "hg3535", "0", ""]
  169. total_size_data = dict(zip(total_size_key, total_size_value))
  170. data_list.append(total_size_data)
  171. # 总得分单双
  172. if odd_evens_dict:
  173. for key, value in odd_evens_dict.items():
  174. sole_str = 'TS' + str(key) + '0' + str(match_id) + 'hg3535'
  175. sole = Helper.genearte_MD5(sole_str, pt)
  176. odds_str = 'TS' + str(key) + '0' + str(odd_evens_dict_rule[key]) + str(value) + 'hg3535' + str(
  177. match_id)
  178. odds_only = Helper.genearte_MD5(odds_str, pt)
  179. condition = odd_evens_dict_rule[key]
  180. odd_evens_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  181. "condition", "odds_only", "sole", "source", "type", "team"]
  182. odd_evens_value = [match_id, league_id, key, "0", 0, 'TS', value, condition,
  183. odds_only, sole, "hg3535", "0", ""]
  184. odd_evens_data = dict(zip(odd_evens_key, odd_evens_value))
  185. data_list.append(odd_evens_data)
  186. odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid",
  187. "is_stringscene", "utime", "pt"]
  188. odds_value = ["bq", "odds", match_id, league_id, data_list, "hg3535", odds_onlys, tag_number, uuid,
  189. 0, utime, pt]
  190. odds_dict = dict(zip(odds_key, odds_value))
  191. if data_list:
  192. res = Helper.async_post(ODDS_URL, odds_dict)
  193. if res:
  194. if "成功" in res:
  195. logger.info('棒球滚球赔率提交成功, {}'.format(res))
  196. logger.info(odds_dict)
  197. else:
  198. logger.warning('棒球滚球赔率提交失败, {}'.format(res))
  199. logger.warning(odds_dict)
  200. else:
  201. logger.warning('棒球滚球赔率接口异常,提交失败, {}'.format(res))
  202. logger.warning(odds_dict)
  203. else:
  204. logger.info('棒球滚球详细赔率列表为空, 不提交')
  205. """赛事结果"""
  206. da_list = []
  207. bq_rball = {
  208. "lg_id": league_id, "home_team": team_home, "guest_team": team_guest, "home_rate": '', "guest_rate": '',
  209. "home_score": score_home, "guest_score": score_guest, "status": 1, "first_score": '',
  210. "last_score": '', "all_inning": '',
  211. "match_score": '', "match_winer": '', "updata_time": '',
  212. "match_time": time_game, "match_process": jijie,
  213. "tag": tag_number, "match_id": match_id, "source": 'hg3535',
  214. "ctime": ctime, "uuid": uuid, "result_mark": source_dict, "start_time": '', 'warn_more': '',
  215. }
  216. da_list.append(bq_rball)
  217. r_data_dict = {
  218. "game_code": "bq",
  219. "title": "match_result_r",
  220. "source": "hg3535",
  221. "data": da_list,
  222. }
  223. if da_list:
  224. try:
  225. res = Helper.async_post(MATCH_RESULT, r_data_dict)
  226. if "成功" in res:
  227. logger.info('棒球滚球结果记录提交成功, {}'.format(res))
  228. logger.info(r_data_dict)
  229. else:
  230. logger.warning('棒球滚球结果记录提交失败, {}'.format(res))
  231. logger.warning(r_data_dict)
  232. except Exception as e:
  233. logger.warning("滚球接口数据异常, 提交失败, {}".format(e))
  234. logging.warning(r_data_dict)
  235. else:
  236. logger.info('棒球滚球结果记录为空, 不提交')
  237. # status_dict = {"game_code": 'bq', "title": "match_status", "source": "hg3535"}
  238. # data_list = []
  239. # data = {'match_id': match_id, 'status': 1, "is_rollball": 0, "is_today": 0, "is_morningplate": 0,
  240. # "is_stringscene": 0, "is_horn": 0}
  241. # data_list.append(data)
  242. # status_dict['data'] = data_list
  243. # res = Helper.async_post(MATCH_STATUS, status_dict)
  244. # if res:
  245. # if "成功" in res:
  246. # self.db.match_status35.insert(status_dict)
  247. # logger.info('{},赛事结果状态交成功, {}'.format('bq', res))
  248. # logger.info(status_dict)
  249. # else:
  250. # logger.warning('{},赛事结果状态交失败, {}'.format('bq', res))
  251. # logger.warning(status_dict)
  252. # else:
  253. # logger.warning('{},赛事结果状态接口异常提交失败, {}'.format('bq', res))
  254. # logger.warning(status_dict)
  255. reactor.callFromThread(out.callback, item)