roll_wangqiu.py 13 KB

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