roll_wangqiu.py 14 KB

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