roll_wangqiu.py 15 KB

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