roll_wangqiu.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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
  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. pt = 4
  70. # 取不到 暂时注掉
  71. # 每局比分
  72. score_dict = item['score_dict']
  73. # 让盘
  74. concedes_dict = item['concedes_dict']
  75. concedes_dict_rule = item['concedes_dict_rule']
  76. # 冠军 独赢
  77. kemps_dict = item['kemps_dict']
  78. # 让局
  79. bureaus_dict = item['bureaus_dict']
  80. bureaus_dict_rule = item['bureaus_dict_rule']
  81. # 总局数大小
  82. total_number_dict = item['total_number_dict']
  83. total_number_dict_rule = item['total_number_dict_rule']
  84. # 总局数单双
  85. odd_evens_dict = item['odd_evens_dict']
  86. odd_evens_dict_rule = item['odd_evens_dict_rule']
  87. sq_dict = item['sq_dict']
  88. new_sq_dict = json.dumps(sq_dict)
  89. uuid = Helper.genearte_uuid(league_name + 'hg3535')
  90. """联赛"""
  91. last_time = '{}-12-31 23:59:59'.format(datetime.datetime.now().year)
  92. if self.db.wq_league35.find({'lg_id': league_id}).count() < 1:
  93. league_dict = {"game_code": "wq", "title": "league", "source": "hg3535"}
  94. league_list = []
  95. league_key = ["name_chinese", "kind", "match_mode", "if_stop", "last_time", "lg_id", "source", "uuid"]
  96. league_value = [league_name, "1", "1", "0", last_time, league_id, "hg3535", uuid]
  97. league_data = dict(zip(league_key, league_value))
  98. league_list.append(league_data)
  99. league_dict['data'] = league_list
  100. res = Helper.async_post(LEAGUE_URL, league_dict)
  101. if res:
  102. if '成功' in res:
  103. self.db.wq_league35.insert(league_data)
  104. logger.info('网球联赛提交成功, {}'.format(res))
  105. else:
  106. logger.warning('网球联赛提交失败, {}'.format(res))
  107. logger.warning(league_dict)
  108. else:
  109. logger.warning('网球滚球联赛接口异常提交失败, {}'.format(res))
  110. logger.warning(league_dict)
  111. else:
  112. logger.info('网球联赛已存在, 不提交')
  113. """赛事"""
  114. if self.db.hg3535_wq_competition35.find({"match_id": match_id, 'is_rollball': 1}).count() < 1:
  115. match_list = []
  116. match_dict = {"game_code": "wq", "title": "match", "source": "hg3535"}
  117. match_key = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  118. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  119. "half_match_id", "is_today", 'rule', "is_horn"]
  120. match_value = [team_home, team_guest, league_id, 1, match_id, match_date, '00:00', tag_number,
  121. "hg3535", 1, 0, 0, utime, uuid, 0, 0, rule, 0]
  122. match_data = dict(zip(match_key, match_value))
  123. match_list.append(match_data)
  124. match_dict['data'] = match_list
  125. res = Helper.async_post(MATCH_URL, match_dict)
  126. if res:
  127. if '成功' in res:
  128. self.db.wq_competition35.insert(match_data)
  129. logger.info('网球赛事提交成功, {}'.format(res))
  130. else:
  131. logger.warning('网球赛事提交失败, {}'.format(res))
  132. logger.warning(match_data)
  133. else:
  134. logger.warning('网球滚球赛事接口异常提交失败, {}'.format(res))
  135. logger.warning(match_data)
  136. else:
  137. logging.info('网球赛事已存在, 不提交')
  138. """赔率"""
  139. data_list = []
  140. odds_onlys = []
  141. # 滚球独赢
  142. if kemps_dict:
  143. for key, value in kemps_dict.items():
  144. if value:
  145. sole_str = 'C' + str(key) + '0' + str(match_id) + 'hg3535'
  146. sole = Helper.genearte_MD5(sole_str, pt)
  147. odds_str = 'C' + str(key) + '0' + '0' + str(value) + 'hg3535' + str(match_id)
  148. odds_only = Helper.genearte_MD5(odds_str, pt)
  149. kemps_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  150. "condition", "odds_only", "sole", "source", "type", "team"]
  151. kemps_value = [match_id, league_id, key, "0", 0, 'C', value, '',
  152. odds_only, sole, "hg3535", "0", ""]
  153. kemps_data = dict(zip(kemps_key, kemps_value))
  154. data_list.append(kemps_data)
  155. # 滚球让盘
  156. if concedes_dict:
  157. for key, value in concedes_dict.items():
  158. if value:
  159. sole_str = 'LD' + str(key) + '0' + str(match_id) + 'hg3535'
  160. sole = Helper.genearte_MD5(sole_str, pt)
  161. odds_str = 'LD' + str(key) + '0' + str(concedes_dict_rule[key]) + str(
  162. value) + 'hg3535' + str(match_id)
  163. odds_only = Helper.genearte_MD5(odds_str, pt)
  164. condition = concedes_dict_rule[key]
  165. concedes_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  166. "condition", "odds_only", "sole", "source", "type", "team"]
  167. concedes_value = [match_id, league_id, key, "0", 0, 'LD', value, condition,
  168. odds_only, sole, "hg3535", "0", ""]
  169. concedes_data = dict(zip(concedes_key, concedes_value))
  170. data_list.append(concedes_data)
  171. # 滚球让局
  172. if bureaus_dict:
  173. for key, value in bureaus_dict.items():
  174. if value:
  175. sole_str = 'LB' + str(key) + '0' + str(match_id) + 'hg3535'
  176. sole = Helper.genearte_MD5(sole_str, pt)
  177. odds_str = 'LB' + str(key) + '0' + str(bureaus_dict_rule[key]) + str(
  178. value) + 'hg3535' + str(match_id)
  179. odds_only = Helper.genearte_MD5(odds_str, pt)
  180. condition = bureaus_dict_rule[key]
  181. bureaus_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  182. "condition", "odds_only", "sole", "source", "type", "team"]
  183. bureaus_value = [match_id, league_id, key, "0", 0, 'LB', value, condition,
  184. odds_only, sole, "hg3535", "0", ""]
  185. bureaus_data = dict(zip(bureaus_key, bureaus_value))
  186. data_list.append(bureaus_data)
  187. # 滚球总局数大小
  188. if total_number_dict:
  189. for key, value in total_number_dict.items():
  190. if value:
  191. sole_str = 'TN' + str(key) + '0' + str(match_id) + 'hg3535'
  192. sole = Helper.genearte_MD5(sole_str, pt)
  193. odds_str = 'TN' + str(key) + '0' + str(
  194. total_number_dict_rule[key]) + str(value) + 'hg3535' + str(match_id)
  195. odds_only = Helper.genearte_MD5(odds_str, pt)
  196. condition = total_number_dict_rule[key]
  197. total_number_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  198. "condition", "odds_only", "sole", "source", "type", "team"]
  199. total_number_value = [match_id, league_id, key, "0", 0, 'TN', value,
  200. condition, odds_only, sole, "hg3535", "0", ""]
  201. total_number_data = dict(zip(total_number_key, total_number_value))
  202. data_list.append(total_number_data)
  203. # 滚球总局数单双
  204. if odd_evens_dict:
  205. for key, value in odd_evens_dict.items():
  206. if value:
  207. condition = odd_evens_dict_rule[key]
  208. odds_str = "TS" + str(key) + '0' + str(odd_evens_dict_rule[key]) + str(
  209. value) + "hg3535" + str(match_id)
  210. sole_str = "TS" + str(key) + '0' + str(match_id) + "hg3535"
  211. odds_only = Helper.genearte_MD5(odds_str, pt)
  212. sole = Helper.genearte_MD5(sole_str, pt)
  213. odd_evens_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  214. "condition", "odds_only", "sole", "source", "type", "team"]
  215. odd_evens_value = [match_id, league_id, key, "0", 0, 'TS', value,
  216. condition, odds_only, sole, "hg3535", "0", ""]
  217. odd_evens_data = dict(zip(odd_evens_key, odd_evens_value))
  218. data_list.append(odd_evens_data)
  219. odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid",
  220. "is_stringscene", "utime", "pt"]
  221. odds_value = ["wq", "odds", match_id, league_id, data_list, "hg3535", odds_onlys, tag_number, uuid,
  222. 0, utime, pt]
  223. odds_dict = dict(zip(odds_key, odds_value))
  224. if data_list:
  225. res = Helper.async_post(ODDS_URL, odds_dict)
  226. if res:
  227. if "成功" in res:
  228. logger.info('网球滚球赔率提交成功, {}'.format(res))
  229. logger.info(odds_dict)
  230. else:
  231. logger.warning('网球滚球赔率提交失败, {}'.format(res))
  232. logger.warning(odds_dict)
  233. else:
  234. logger.warning('网球滚球赔率接口异常提交失败, {}'.format(res))
  235. logger.warning(odds_dict)
  236. else:
  237. logger.info('网球滚球详细赔率列表为空, 不提交')
  238. # 赛事结果
  239. da_list = []
  240. wq_rball = {"lg_id": league_id, "home_player_name": team_home, "guest_player_name": team_guest,
  241. "home_player_let_plate": '', "guest_player_let_plate": '', "home_player_let_inning": "",
  242. "guest_player_let_inning": "", "all_inning": "",
  243. "home_player_score": score_home,
  244. "guest_player_score": score_guest,
  245. "status": 1, "first_score_player": "", "last_score_player": "",
  246. "first_inning_score": "",
  247. "second_inning_score": "", "third_inning_score": "",
  248. "match_winer_player": "", "update_time": "",
  249. "match_time": time_game, "match_process": jijie, "tag": tag_number, "match_id": match_id,
  250. "source": "hg3535", "result_mark": score_dict, "warn_more": '', "uuid": uuid,
  251. }
  252. da_list.append(wq_rball)
  253. r_data_dict = {
  254. "game_code": "wq",
  255. "title": "match_result_r",
  256. "source": "hg3535",
  257. "data": da_list
  258. }
  259. if da_list:
  260. try:
  261. res = Helper.async_post(MATCH_RESULT, r_data_dict)
  262. if "成功" in res:
  263. logger.info('网球滚球结果记录提交成功, {}'.format(res))
  264. logger.info(r_data_dict)
  265. else:
  266. logger.warning('网球滚球结果记录提交失败, {}'.format(res))
  267. logger.warning(r_data_dict)
  268. except Exception as e:
  269. logger.warning("网球接口数据异常, 提交失败, {}".format(e))
  270. else:
  271. logger.info('网球滚球结果记录为空, 不提交')
  272. # status_dict = {"game_code": 'wq', "title": "match_status", "source": "hg3535"}
  273. # data_list = []
  274. # data = {'match_id': match_id, 'status': 1, "is_rollball": 0, "is_today": 0, "is_morningplate": 0,
  275. # "is_stringscene": 0, "is_horn": 0}
  276. # data_list.append(data)
  277. # status_dict['data'] = data_list
  278. # res = Helper.async_post(MATCH_STATUS, status_dict)
  279. # if res:
  280. # if "成功" in res:
  281. # self.db.match_status35.insert(status_dict)
  282. # logger.info('{},赛事结果状态交成功, {}'.format('wq', res))
  283. # logger.info(status_dict)
  284. # else:
  285. # logger.warning('{},赛事结果状态交失败, {}'.format('wq', res))
  286. # logger.warning(status_dict)
  287. # else:
  288. # logger.warning('{},赛事结果状态接口异常提交失败, {}'.format('wq', res))
  289. # logger.warning(status_dict)
  290. reactor.callFromThread(out.callback, item)