roll_wangqiu.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. # self.db.wq_league35.update({'lg_id': league_id}, {'$set': league_data},
  91. # upsert=True)
  92. logger.info('网球联赛提交成功, {}'.format(res))
  93. else:
  94. logger.warning('网球联赛提交失败, {}'.format(res))
  95. logger.warning(league_dict)
  96. else:
  97. logger.warning('网球滚球联赛接口异常提交失败, {}'.format(res))
  98. logger.warning(league_dict)
  99. else:
  100. logger.info('网球联赛已存在, 不提交')
  101. """赛事"""
  102. match_identity = Helper.genearte_uuid(team_home + team_guest + match_date)
  103. if self.db.wq_competition35.find({"match_id": match_id, 'is_rollball': 1}).count() < 1:
  104. match_list = []
  105. match_dict = {"game_code": "wq", "title": "match", "source": "hg3535"}
  106. match_key = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  107. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  108. "half_match_id", "is_today", 'rule', "is_horn", 'match_identity']
  109. match_value = [team_home, team_guest, league_id, 1, match_id, match_date, match_time, tag_number,
  110. "hg3535", 1, 0, 0, utime, uuid, 0, 0, rule, 0, match_identity]
  111. match_data = dict(zip(match_key, match_value))
  112. match_list.append(match_data)
  113. match_dict['data'] = match_list
  114. res = Helper.async_post(MATCH_URL, match_dict)
  115. if res:
  116. if res.get('status') == 1:
  117. self.db.wq_competition35.insert(match_data)
  118. # self.db.wq_competition35.update({'match_id': match_id, 'is_rollball': 1}, {'$set': match_data},
  119. # upsert=True)
  120. logger.info('网球赛事提交成功, {}'.format(res))
  121. else:
  122. logger.warning('网球赛事提交失败, {}'.format(res))
  123. # logger.warning(match_data)
  124. else:
  125. logger.warning('网球滚球赛事接口异常提交失败, {}'.format(res))
  126. # logger.warning(match_data)
  127. else:
  128. logging.info('网球赛事已存在, 不提交')
  129. """赔率"""
  130. data_list = []
  131. odds_onlys = []
  132. # 滚球独赢
  133. if kemps_dict:
  134. for key, value in kemps_dict.items():
  135. if value:
  136. sole_str = 'C' + str(key) + '0' + str(match_id) + 'hg3535'
  137. sole = Helper.genearte_MD5(sole_str, pt)
  138. odds_str = 'C' + str(key) + '0' + '0' + str(value) + 'hg3535' + str(match_id)
  139. odds_only = Helper.genearte_MD5(odds_str, pt)
  140. kemps_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  141. "condition", "odds_only", "sole", "source", "type", "team"]
  142. kemps_value = [match_id, league_id, key, "0", 0, 'C', value, '',
  143. odds_only, sole, "hg3535", "0", ""]
  144. kemps_data = dict(zip(kemps_key, kemps_value))
  145. data_list.append(kemps_data)
  146. # 滚球让盘
  147. if concedes_dict:
  148. for key, value in concedes_dict.items():
  149. if value:
  150. sole_str = 'LD' + str(key) + '0' + str(match_id) + 'hg3535'
  151. sole = Helper.genearte_MD5(sole_str, pt)
  152. odds_str = 'LD' + str(key) + '0' + str(concedes_dict_rule[key]) + str(
  153. value) + 'hg3535' + str(match_id)
  154. odds_only = Helper.genearte_MD5(odds_str, pt)
  155. condition = concedes_dict_rule[key]
  156. concedes_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  157. "condition", "odds_only", "sole", "source", "type", "team"]
  158. concedes_value = [match_id, league_id, key, "0", 0, 'LD', value, condition,
  159. odds_only, sole, "hg3535", "0", ""]
  160. concedes_data = dict(zip(concedes_key, concedes_value))
  161. data_list.append(concedes_data)
  162. # 滚球让局
  163. if bureaus_dict:
  164. for key, value in bureaus_dict.items():
  165. if value:
  166. sole_str = 'LB' + str(key) + '0' + str(match_id) + 'hg3535'
  167. sole = Helper.genearte_MD5(sole_str, pt)
  168. odds_str = 'LB' + str(key) + '0' + str(bureaus_dict_rule[key]) + str(
  169. value) + 'hg3535' + str(match_id)
  170. odds_only = Helper.genearte_MD5(odds_str, pt)
  171. condition = bureaus_dict_rule[key]
  172. bureaus_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  173. "condition", "odds_only", "sole", "source", "type", "team"]
  174. bureaus_value = [match_id, league_id, key, "0", 0, 'LB', value, condition,
  175. odds_only, sole, "hg3535", "0", ""]
  176. bureaus_data = dict(zip(bureaus_key, bureaus_value))
  177. data_list.append(bureaus_data)
  178. # 滚球总局数大小
  179. if total_number_dict:
  180. for key, value in total_number_dict.items():
  181. if value:
  182. sole_str = 'TN' + str(key) + '0' + str(match_id) + 'hg3535'
  183. sole = Helper.genearte_MD5(sole_str, pt)
  184. odds_str = 'TN' + str(key) + '0' + str(
  185. total_number_dict_rule[key]) + str(value) + 'hg3535' + str(match_id)
  186. odds_only = Helper.genearte_MD5(odds_str, pt)
  187. condition = total_number_dict_rule[key]
  188. total_number_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  189. "condition", "odds_only", "sole", "source", "type", "team"]
  190. total_number_value = [match_id, league_id, key, "0", 0, 'TN', value,
  191. condition, odds_only, sole, "hg3535", "0", ""]
  192. total_number_data = dict(zip(total_number_key, total_number_value))
  193. data_list.append(total_number_data)
  194. # 滚球总局数单双
  195. if odd_evens_dict:
  196. for key, value in odd_evens_dict.items():
  197. if value:
  198. condition = odd_evens_dict_rule[key]
  199. odds_str = "TS" + str(key) + '0' + str(odd_evens_dict_rule[key]) + str(
  200. value) + "hg3535" + str(match_id)
  201. sole_str = "TS" + str(key) + '0' + str(match_id) + "hg3535"
  202. odds_only = Helper.genearte_MD5(odds_str, pt)
  203. sole = Helper.genearte_MD5(sole_str, pt)
  204. odd_evens_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  205. "condition", "odds_only", "sole", "source", "type", "team"]
  206. odd_evens_value = [match_id, league_id, key, "0", 0, 'TS', value,
  207. condition, odds_only, sole, "hg3535", "0", ""]
  208. odd_evens_data = dict(zip(odd_evens_key, odd_evens_value))
  209. data_list.append(odd_evens_data)
  210. odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid",
  211. "is_stringscene", "utime", "pt", 'match_identity']
  212. odds_value = ["wq", "odds", match_id, league_id, data_list, "hg3535", odds_onlys, tag_number, uuid,
  213. 0, cdate, pt, match_identity]
  214. odds_dict = dict(zip(odds_key, odds_value))
  215. if data_list:
  216. res = Helper.async_post(ODDS_URL, odds_dict)
  217. if res:
  218. if res.get('status') == 1:
  219. logger.info('网球滚球赔率提交成功, {}'.format(res))
  220. # logger.info(odds_dict)
  221. else:
  222. logger.warning('网球滚球赔率提交失败, {}'.format(res))
  223. # logger.warning(odds_dict)
  224. else:
  225. logger.warning('网球滚球赔率接口异常提交失败, {}'.format(res))
  226. # logger.warning(odds_dict)
  227. else:
  228. logger.info('网球滚球详细赔率列表为空, 不提交')
  229. # 赛事结果
  230. da_list = []
  231. wq_rball = {"lg_id": league_id, "home_player_name": team_home, "guest_player_name": team_guest,
  232. "home_player_let_plate": '', "guest_player_let_plate": '', "home_player_let_inning": "",
  233. "guest_player_let_inning": "", "all_inning": "",
  234. "home_player_score": score_home, 'match_identity': match_identity,
  235. "guest_player_score": score_guest,
  236. "status": 1, "first_score_player": "", "last_score_player": "",
  237. "first_inning_score": "",
  238. "second_inning_score": "", "third_inning_score": "",
  239. "match_winer_player": "", "update_time": "",
  240. "match_time": time_game, "match_process": jijie, "tag": tag_number, "match_id": match_id,
  241. "source": "hg3535", "result_mark": score_dict, "warn_more": '', "uuid": uuid,
  242. }
  243. da_list.append(wq_rball)
  244. r_data_dict = {
  245. "game_code": "wq",
  246. "title": "match_result_r",
  247. "source": "hg3535",
  248. "data": da_list
  249. }
  250. if da_list:
  251. try:
  252. res = Helper.async_post(MATCH_RESULT, r_data_dict)
  253. if res.get('status') == 1:
  254. logger.info('网球滚球结果记录提交成功, {}'.format(res))
  255. self.db.wq_match_result35.insert(wq_rball)
  256. # logger.info(r_data_dict)
  257. else:
  258. logger.warning('网球滚球结果记录提交失败, {}'.format(res))
  259. # logger.warning(r_data_dict)
  260. except Exception as e:
  261. logger.warning("网球接口数据异常, 提交失败, {}".format(e))
  262. else:
  263. logger.info('网球滚球结果记录为空, 不提交')
  264. reactor.callFromThread(out.callback, item)