roll_bangqiu.py 12 KB

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