roll_wangqiu.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. import datetime
  2. import pymongo
  3. import time
  4. import logging
  5. import json
  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, MATCH_STATUS
  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. #
  21. def _do_calculation(self, item, out):
  22. # def process_item(self, item, spider):
  23. logger = logging.getLogger(__name__)
  24. # 联赛id
  25. league_id = item['league_id']
  26. # 联赛名
  27. league_name = item['league_name']
  28. # result = item['result']
  29. # 比赛id
  30. match_id = item['game_id']
  31. # 球队1
  32. team_home = item['team_home']
  33. # 球队2
  34. team_guest = item['team_guest']
  35. # 数量(97>)
  36. tag_number = item['number']
  37. # 比赛状态
  38. zhuangtai = item['zhuangtai']
  39. # 日期
  40. # data_game = item['data_game']
  41. rule = item['rule']
  42. # try:
  43. # data_game = item['data_game'].split("/")
  44. # month = str(data_game[1].strip())
  45. # day = str(data_game[0])
  46. # except Exception as e:
  47. # logger.warning(e)
  48. # data_game = item['data_game'].split(" ")
  49. # months = str(data_game[1].strip())
  50. # month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  51. # 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  52. # month = month_dict[months]
  53. # day = str(data_game[0])
  54. # 比赛时间
  55. time_game = str(item['time_game'])
  56. # 比赛时间,时间戳
  57. # ctime = "2019" + "-" + month + "-" + day + "" + time_game + ":00"
  58. # r_ctime = "2019" + "-" + month + "-" + day
  59. # 现在时间,时间戳
  60. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  61. match_date = datetime.datetime.now().strftime("%Y-%m-%d")
  62. # 队1分数
  63. score_home = item['score_home']
  64. # 队2分数
  65. score_guest = item['score_guest']
  66. # 第几节
  67. jijie = item['jijie']
  68. # # 球队得分
  69. # qiudui = item['qiudui']
  70. pt = 4
  71. # 取不到 暂时注掉
  72. # match_date, match_time = new_times(ctime)
  73. # 每局比分
  74. score_dict = item['score_dict']
  75. # 让盘
  76. concedes_dict = item['concedes_dict']
  77. concedes_dict_rule = item['concedes_dict_rule']
  78. # 冠军 独赢
  79. kemps_dict = item['kemps_dict']
  80. # 让局
  81. bureaus_dict = item['bureaus_dict']
  82. bureaus_dict_rule = item['bureaus_dict_rule']
  83. # 总局数大小
  84. total_number_dict = item['total_number_dict']
  85. total_number_dict_rule = item['total_number_dict_rule']
  86. # 总局数单双
  87. odd_evens_dict = item['odd_evens_dict']
  88. odd_evens_dict_rule = item['odd_evens_dict_rule']
  89. sq_dict = item['sq_dict']
  90. new_sq_dict = json.dumps(sq_dict)
  91. uuid = Helper.genearte_uuid(league_name + 'hg3535')
  92. """联赛"""
  93. last_time = '{}-12-31 23:59:59'.format(datetime.datetime.now().year)
  94. if self.db.wq_league35.find({'lg_id': league_id}).count() < 1:
  95. league_dict = {"game_code": "wq", "title": "league", "source": "hg3535"}
  96. league_list = []
  97. league_key = ["name_chinese", "kind", "match_mode", "if_stop", "last_time", "lg_id", "source", "uuid"]
  98. league_value = [league_name, "1", "1", "0", last_time, league_id, "hg3535", uuid]
  99. league_data = dict(zip(league_key, league_value))
  100. league_list.append(league_data)
  101. league_dict['data'] = league_list
  102. res = Helper.async_post(LEAGUE_URL, league_dict)
  103. if res:
  104. if '成功' in res:
  105. self.db.wq_league35.insert(league_data)
  106. logger.info('网球联赛提交成功, {}'.format(res))
  107. else:
  108. logger.warning('网球联赛提交失败, {}'.format(res))
  109. logger.warning(league_dict)
  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', "is_horn"]
  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, 0]
  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 res:
  129. if '成功' in res:
  130. self.db.wq_competition35.insert(match_data)
  131. logger.info('网球赛事提交成功, {}'.format(res))
  132. else:
  133. logger.warning('网球赛事提交失败, {}'.format(res))
  134. logger.warning(match_data)
  135. else:
  136. logger.warning('网球滚球赛事接口异常提交失败, {}'.format(res))
  137. logger.warning(match_data)
  138. else:
  139. logging.info('网球赛事已存在, 不提交')
  140. """赔率"""
  141. data_list = []
  142. odds_onlys = []
  143. # 滚球独赢
  144. if kemps_dict:
  145. for key, value in kemps_dict.items():
  146. if value:
  147. sole_str = 'C' + str(key) + '0' + str(match_id) + 'hg3535'
  148. sole = Helper.genearte_MD5(sole_str, pt)
  149. odds_str = 'C' + str(key) + '0' + '0' + str(value) + 'hg3535' + str(match_id)
  150. odds_only = Helper.genearte_MD5(odds_str, pt)
  151. kemps_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  152. "condition", "odds_only", "sole", "source", "type", "team"]
  153. kemps_value = [match_id, league_id, key, "0", 0, 'C', value, '',
  154. odds_only, sole, "hg3535", "0", ""]
  155. kemps_data = dict(zip(kemps_key, kemps_value))
  156. data_list.append(kemps_data)
  157. # 滚球让盘
  158. if concedes_dict:
  159. for key, value in concedes_dict.items():
  160. if value:
  161. sole_str = 'LD' + str(key) + '0' + str(match_id) + 'hg3535'
  162. sole = Helper.genearte_MD5(sole_str, pt)
  163. odds_str = 'LD' + str(key) + '0' + str(concedes_dict_rule[key]) + str(
  164. value) + 'hg3535' + str(match_id)
  165. odds_only = Helper.genearte_MD5(odds_str, pt)
  166. condition = concedes_dict_rule[key]
  167. concedes_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  168. "condition", "odds_only", "sole", "source", "type", "team"]
  169. concedes_value = [match_id, league_id, key, "0", 0, 'LD', value, condition,
  170. odds_only, sole, "hg3535", "0", ""]
  171. concedes_data = dict(zip(concedes_key, concedes_value))
  172. data_list.append(concedes_data)
  173. # 滚球让局
  174. if bureaus_dict:
  175. for key, value in bureaus_dict.items():
  176. if value:
  177. sole_str = 'LB' + str(key) + '0' + str(match_id) + 'hg3535'
  178. sole = Helper.genearte_MD5(sole_str, pt)
  179. odds_str = 'LB' + str(key) + '0' + str(bureaus_dict_rule[key]) + str(
  180. value) + 'hg3535' + str(match_id)
  181. odds_only = Helper.genearte_MD5(odds_str, pt)
  182. condition = bureaus_dict_rule[key]
  183. bureaus_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  184. "condition", "odds_only", "sole", "source", "type", "team"]
  185. bureaus_value = [match_id, league_id, key, "0", 0, 'LB', value, condition,
  186. odds_only, sole, "hg3535", "0", ""]
  187. bureaus_data = dict(zip(bureaus_key, bureaus_value))
  188. data_list.append(bureaus_data)
  189. # 滚球总局数大小
  190. if total_number_dict:
  191. for key, value in total_number_dict.items():
  192. if value:
  193. sole_str = 'TN' + str(key) + '0' + str(match_id) + 'hg3535'
  194. sole = Helper.genearte_MD5(sole_str, pt)
  195. odds_str = 'TN' + str(key) + '0' + str(
  196. total_number_dict_rule[key]) + str(value) + 'hg3535' + str(match_id)
  197. odds_only = Helper.genearte_MD5(odds_str, pt)
  198. condition = total_number_dict_rule[key]
  199. total_number_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  200. "condition", "odds_only", "sole", "source", "type", "team"]
  201. total_number_value = [match_id, league_id, key, "0", 0, 'TN', value,
  202. condition, odds_only, sole, "hg3535", "0", ""]
  203. total_number_data = dict(zip(total_number_key, total_number_value))
  204. data_list.append(total_number_data)
  205. # 滚球总局数单双
  206. if odd_evens_dict:
  207. for key, value in odd_evens_dict.items():
  208. if value:
  209. condition = odd_evens_dict_rule[key]
  210. odds_str = "TS" + str(key) + '0' + str(odd_evens_dict_rule[key]) + str(
  211. value) + "hg3535" + str(match_id)
  212. sole_str = "TS" + str(key) + '0' + str(match_id) + "hg3535"
  213. odds_only = Helper.genearte_MD5(odds_str, pt)
  214. sole = Helper.genearte_MD5(sole_str, pt)
  215. odd_evens_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  216. "condition", "odds_only", "sole", "source", "type", "team"]
  217. odd_evens_value = [match_id, league_id, key, "0", 0, 'TS', value,
  218. condition, odds_only, sole, "hg3535", "0", ""]
  219. odd_evens_data = dict(zip(odd_evens_key, odd_evens_value))
  220. data_list.append(odd_evens_data)
  221. odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid",
  222. "is_stringscene", "utime", "pt"]
  223. odds_value = ["wq", "odds", match_id, league_id, data_list, "hg3535", odds_onlys, tag_number, uuid,
  224. 0, utime, pt]
  225. odds_dict = dict(zip(odds_key, odds_value))
  226. if data_list:
  227. res = Helper.async_post(ODDS_URL, odds_dict)
  228. if res:
  229. if "成功" in res:
  230. logger.info('网球滚球赔率提交成功, {}'.format(res))
  231. logger.info(odds_dict)
  232. else:
  233. logger.warning('网球滚球赔率提交失败, {}'.format(res))
  234. logger.warning(odds_dict)
  235. else:
  236. logger.warning('网球滚球赔率接口异常提交失败, {}'.format(res))
  237. logger.warning(odds_dict)
  238. else:
  239. logger.info('网球滚球详细赔率列表为空, 不提交')
  240. # 赛事结果
  241. da_list = []
  242. wq_rball = {"lg_id": league_id, "home_player_name": team_home, "guest_player_name": team_guest,
  243. "home_player_let_plate": '', "guest_player_let_plate": '', "home_player_let_inning": "",
  244. "guest_player_let_inning": "", "all_inning": "",
  245. "home_player_score": score_home,
  246. "guest_player_score": score_guest,
  247. "status": 1, "first_score_player": "", "last_score_player": "",
  248. "first_inning_score": "",
  249. "second_inning_score": "", "third_inning_score": "",
  250. "match_winer_player": "", "update_time": "",
  251. "match_time": time_game, "match_process": jijie, "tag": tag_number, "match_id": match_id,
  252. "source": "hg3535", "result_mark": score_dict, "warn_more": '', "uuid": uuid,
  253. }
  254. da_list.append(wq_rball)
  255. r_data_dict = {
  256. "game_code": "wq",
  257. "title": "match_result_r",
  258. "source": "hg3535",
  259. "data": da_list
  260. }
  261. if da_list:
  262. try:
  263. res = Helper.async_post(MATCH_RESULT, r_data_dict)
  264. if "成功" in res:
  265. logger.info('网球滚球结果记录提交成功, {}'.format(res))
  266. logger.info(r_data_dict)
  267. else:
  268. logger.warning('网球滚球结果记录提交失败, {}'.format(res))
  269. logger.warning(r_data_dict)
  270. except Exception as e:
  271. logger.warning("网球接口数据异常, 提交失败, {}".format(e))
  272. else:
  273. logger.info('网球滚球结果记录为空, 不提交')
  274. status_dict = {"game_code": 'wq', "title": "match_status", "source": "hg3535"}
  275. data_list = []
  276. data = {'match_id': match_id, 'status': 1, "is_rollball": 0, "is_today": 0, "is_morningplate": 0,
  277. "is_stringscene": 0, "is_horn": 0}
  278. data_list.append(data)
  279. status_dict['data'] = data_list
  280. res = Helper.async_post(MATCH_STATUS, status_dict)
  281. if res:
  282. if "成功" in res:
  283. self.db.match_status35.insert(status_dict)
  284. logger.info('{},赛事结果状态交成功, {}'.format('wq', res))
  285. logger.info(status_dict)
  286. else:
  287. logger.warning('{},赛事结果状态交失败, {}'.format('wq', res))
  288. logger.warning(status_dict)
  289. else:
  290. logger.warning('{},赛事结果状态接口异常提交失败, {}'.format('wq', res))
  291. logger.warning(status_dict)
  292. reactor.callFromThread(out.callback, item)