roll_wangqiu.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. import datetime
  2. import pymongo
  3. import time
  4. import logging
  5. import json
  6. from .ball_func import fuhao
  7. from ..utils.helper import Helper
  8. # from .ball_func import get_pcode, new_time, out_time
  9. # from concurrent.futures import ThreadPoolExecutor
  10. # from requests_futures.sessions import FuturesSession
  11. from ..settings import M_HOST, M_USER, M_PASSWORD, M_POST, M_DB, LEAGUE_URL, ODDS_URL, MATCH_URL, MATCH_RESULT
  12. from twisted.internet import defer, reactor
  13. class Roll_Wangqiupipeline(object):
  14. def open_spider(self, spider):
  15. self.mongo = pymongo.MongoClient(host=M_HOST, username=M_USER, password=M_PASSWORD, port=M_POST,
  16. authSource='kaiyou')
  17. self.db = self.mongo[M_DB]
  18. # @defer.inlineCallbacks
  19. # def process_item(self, item, spider):
  20. # out = defer.Deferred()
  21. # reactor.callInThread(self._do_calculation, item, out)
  22. # yield out
  23. # defer.returnValue(item)
  24. #
  25. # def _do_calculation(self, item, out):
  26. def process_item(self, item, spider):
  27. logger = logging.getLogger(__name__)
  28. # 联赛id
  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. # 比赛状态
  42. zhuangtai = item['zhuangtai']
  43. # 日期
  44. # data_game = item['data_game']
  45. rule = item['rule']
  46. # try:
  47. # data_game = item['data_game'].split("/")
  48. # month = str(data_game[1].strip())
  49. # day = str(data_game[0])
  50. # except Exception as e:
  51. # logger.warning(e)
  52. # data_game = item['data_game'].split(" ")
  53. # months = str(data_game[1].strip())
  54. # month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  55. # 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  56. # month = month_dict[months]
  57. # day = str(data_game[0])
  58. # 比赛时间
  59. time_game = str(item['time_game'])
  60. # 比赛时间,时间戳
  61. # ctime = "2019" + "-" + month + "-" + day + "" + time_game + ":00"
  62. # r_ctime = "2019" + "-" + month + "-" + day
  63. # 现在时间,时间戳
  64. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  65. match_date = datetime.datetime.now().strftime("%Y-%m-%d")
  66. # 队1分数
  67. score_home = item['score_home']
  68. # 队2分数
  69. score_guest = item['score_guest']
  70. # 第几节
  71. jijie = item['jijie']
  72. # 球队得分
  73. qiudui = item['qiudui']
  74. pt = 4
  75. # 取不到 暂时注掉
  76. # match_date, match_time = new_times(ctime)
  77. # 每局比分
  78. score_dict = item['score_dict']
  79. # 让盘
  80. concedes_dict = item['concedes_dict']
  81. concedes_dict_rule = item['concedes_dict_rule']
  82. # 冠军 独赢
  83. kemps_dict = item['kemps_dict']
  84. # 让局
  85. bureaus_dict = item['bureaus_dict']
  86. bureaus_dict_rule = item['bureaus_dict_rule']
  87. # 总局数大小
  88. total_number_dict = item['total_number_dict']
  89. total_number_dict_rule = item['total_number_dict_rule']
  90. # 总局数单双
  91. odd_evens_dict = item['odd_evens_dict']
  92. odd_evens_dict_rule = item['odd_evens_dict_rule']
  93. sq_dict = item['sq_dict']
  94. new_sq_dict = json.dumps(sq_dict)
  95. uuid = Helper.genearte_uuid(league_name + 'hg3535')
  96. """联赛"""
  97. last_time = '{}-12-31 23:59:59'.format(datetime.datetime.now().year)
  98. if self.db.wq_league35.find({'lg_id': league_id}).count() < 1:
  99. league_dict = {"game_code": "wq", "title": "league", "source": "hg3535"}
  100. league_list = []
  101. league_key = ["name_chinese", "kind", "match_mode", "if_stop", "last_time", "lg_id", "source", "uuid"]
  102. league_value = [league_name, "1", "1", "0", last_time, league_id, "hg3535", uuid]
  103. league_data = dict(zip(league_key, league_value))
  104. league_list.append(league_data)
  105. league_dict['data'] = league_list
  106. res = Helper.async_post(LEAGUE_URL, league_dict)
  107. if '成功' in res:
  108. self.db.wq_league35.insert(league_data)
  109. logger.info('网球联赛提交成功, {}'.format(res))
  110. else:
  111. logger.warning('网球联赛提交失败, {}'.format(res))
  112. logger.warning(league_dict)
  113. else:
  114. logger.info('网球联赛已存在, 不提交')
  115. """赛事"""
  116. if self.db.hg3535_wq_competition35.find({"match_id": match_id, 'is_rollball': 1}).count() < 1:
  117. match_list = []
  118. match_dict = {"game_code": "wq", "title": "match", "source": "hg3535"}
  119. match_key = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  120. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  121. "half_match_id", "is_today", 'rule']
  122. match_value = [team_home, team_guest, league_id, 1, match_id, match_date, '00:00', tag_number,
  123. "hg3535", 1, 0, 0, utime, uuid, 0, 0, rule]
  124. match_data = dict(zip(match_key, match_value))
  125. match_list.append(match_data)
  126. match_dict['data'] = match_list
  127. res = Helper.async_post(MATCH_URL, match_dict)
  128. if '成功' in res:
  129. self.db.wq_competition35.insert(match_data)
  130. logger.info('网球赛事提交成功, {}'.format(res))
  131. else:
  132. logger.warning('网球赛事提交失败, {}'.format(res))
  133. logger.warning(match_data)
  134. else:
  135. logging.info('网球赛事已存在, 不提交')
  136. """赔率"""
  137. data_list = []
  138. odds_onlys = []
  139. # 滚球独赢
  140. if kemps_dict:
  141. for key, value in kemps_dict.items():
  142. if value:
  143. sole_str = 'C' + str(key) + '0' + str(match_id) + 'hg3535'
  144. sole = Helper.genearte_MD5(sole_str, pt)
  145. odds_str = 'C' + str(key) + '0' + '0' + str(value) + 'hg3535' + str(match_id)
  146. odds_only = Helper.genearte_MD5(odds_str, pt)
  147. kemps_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  148. "condition", "odds_only", "sole", "source", "type", "team"]
  149. kemps_value = [match_id, league_id, key, "0", 0, 'C', value, '',
  150. odds_only, sole, "hg3535", "0", ""]
  151. kemps_data = dict(zip(kemps_key, kemps_value))
  152. data_list.append(kemps_data)
  153. # 滚球让盘
  154. if concedes_dict:
  155. for key, value in concedes_dict.items():
  156. if value:
  157. sole_str = 'LD' + str(key) + '0' + str(match_id) + 'hg3535'
  158. sole = Helper.genearte_MD5(sole_str, pt)
  159. odds_str = 'LD' + str(key) + '0' + str(concedes_dict_rule[key]) + str(
  160. value) + 'hg3535' + str(match_id)
  161. odds_only = Helper.genearte_MD5(odds_str, pt)
  162. condition = fuhao(concedes_dict_rule[key])
  163. concedes_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  164. "condition", "odds_only", "sole", "source", "type", "team"]
  165. concedes_value = [match_id, league_id, key, "0", 0, 'LD', value, condition,
  166. odds_only, sole, "hg3535", "0", ""]
  167. concedes_data = dict(zip(concedes_key, concedes_value))
  168. data_list.append(concedes_data)
  169. # 滚球让局
  170. if bureaus_dict:
  171. for key, value in bureaus_dict.items():
  172. if value:
  173. sole_str = 'LB' + str(key) + '0' + str(match_id) + 'hg3535'
  174. sole = Helper.genearte_MD5(sole_str, pt)
  175. odds_str = 'LB' + str(key) + '0' + str(bureaus_dict_rule[key]) + str(
  176. value) + 'hg3535' + str(match_id)
  177. odds_only = Helper.genearte_MD5(odds_str, pt)
  178. condition = fuhao(bureaus_dict_rule[key])
  179. bureaus_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  180. "condition", "odds_only", "sole", "source", "type", "team"]
  181. bureaus_value = [match_id, league_id, key, "0", 0, 'LB', value, condition,
  182. odds_only, sole, "hg3535", "0", ""]
  183. bureaus_data = dict(zip(bureaus_key, bureaus_value))
  184. data_list.append(bureaus_data)
  185. # 滚球总局数大小
  186. if total_number_dict:
  187. for key, value in total_number_dict.items():
  188. if value:
  189. sole_str = 'TN' + str(key) + '0' + str(match_id) + 'hg3535'
  190. sole = Helper.genearte_MD5(sole_str, pt)
  191. odds_str = 'TN' + str(key) + '0' + str(
  192. total_number_dict_rule[key]) + str(value) + 'hg3535' + str(match_id)
  193. odds_only = Helper.genearte_MD5(odds_str, pt)
  194. condition = fuhao(total_number_dict_rule[key])
  195. total_number_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  196. "condition", "odds_only", "sole", "source", "type", "team"]
  197. total_number_value = [match_id, league_id, key, "0", 0, 'TN', value,
  198. condition, odds_only, sole, "hg3535", "0", ""]
  199. total_number_data = dict(zip(total_number_key, total_number_value))
  200. data_list.append(total_number_data)
  201. # 滚球总局数单双
  202. if odd_evens_dict:
  203. for key, value in odd_evens_dict.items():
  204. if value:
  205. condition = odd_evens_dict_rule[key]
  206. odds_str = "TS" + str(key) + '0' + str(odd_evens_dict_rule[key]) + str(
  207. value) + "hg3535" + str(match_id)
  208. sole_str = "TS" + str(key) + '0' + str(match_id) + "hg3535"
  209. odds_only = Helper.genearte_MD5(odds_str, pt)
  210. sole = Helper.genearte_MD5(sole_str, pt)
  211. odd_evens_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  212. "condition", "odds_only", "sole", "source", "type", "team"]
  213. odd_evens_value = [match_id, league_id, key, "0", 0, 'TS', value,
  214. condition, odds_only, sole, "hg3535", "0", ""]
  215. odd_evens_data = dict(zip(odd_evens_key, odd_evens_value))
  216. data_list.append(odd_evens_data)
  217. odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid",
  218. "is_stringscene", "utime", "pt"]
  219. odds_value = ["wq", "odds", match_id, league_id, data_list, "hg3535", odds_onlys, tag_number, uuid,
  220. 0, utime, pt]
  221. odds_dict = dict(zip(odds_key, odds_value))
  222. if data_list:
  223. res = Helper.async_post(ODDS_URL, odds_dict)
  224. if "成功" in res:
  225. logger.info('网球滚球赔率提交成功, {}'.format(res))
  226. logger.info(odds_dict)
  227. else:
  228. logger.warning('网球滚球赔率提交失败, {}'.format(res))
  229. logger.warning(odds_dict)
  230. else:
  231. logger.info('网球滚球详细赔率列表为空, 不提交')
  232. # 赛事结果
  233. da_list = []
  234. wq_rball = {"lg_id": league_id, "home_player_name": team_home, "guest_player_name": team_guest,
  235. "home_player_let_plate": '', "guest_player_let_plate": '', "home_player_let_inning": "",
  236. # "guest_player_let_inning": "", "all_inning": score_dict['total_inning'],
  237. "guest_player_let_inning": "", "all_inning": "",
  238. "home_player_score": score_home,
  239. "guest_player_score": score_guest,
  240. "status": 1, "first_score_player": "", "last_score_player": "",
  241. "first_inning_score": "",
  242. "second_inning_score": "", "third_inning_score": "",
  243. "match_winer_player": "", "update_time": "",
  244. "match_time": time_game, "match_process": jijie, "tag": tag_number, "match_id": match_id,
  245. "source": "hg3535", "result_mark": score_dict, "warn_more": '', "uuid": uuid,
  246. # "match_score": score_dict['current_point_home_guest'], "ctime": ctime, "inning": "",
  247. # "strat_time": ""
  248. }
  249. da_list.append(wq_rball)
  250. r_data_dict = {
  251. "game_code": "wq",
  252. "title": "match_result_r",
  253. "source": "hg3535",
  254. "data": da_list
  255. }
  256. if da_list:
  257. try:
  258. res = Helper.async_post(MATCH_RESULT, r_data_dict)
  259. if "成功" in res:
  260. logger.info('网球滚球结果记录提交成功, {}'.format(res))
  261. logger.info(r_data_dict)
  262. else:
  263. logger.warning('网球滚球结果记录提交失败, {}'.format(res))
  264. logger.warning(r_data_dict)
  265. except Exception as e:
  266. logger.warning("网球接口数据异常, 提交失败, {}".format(e))
  267. else:
  268. logger.info('网球滚球结果记录为空, 不提交')
  269. # reactor.callFromThread(out.callback, item)