roll_wangqiu.py 15 KB

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