roll_wangqiu.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. 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. try:
  42. data_game = item['data_game'].split("/")
  43. month = str(data_game[1].strip())
  44. day = str(data_game[0]).strip()
  45. except Exception as e:
  46. logger.warning(e)
  47. data_game = item['data_game'].split(" ")
  48. months = str(data_game[1].strip())
  49. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  50. 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  51. month = month_dict[months]
  52. day = str(data_game[0]).strip()
  53. # 比赛时间
  54. time_game = str(item['time_game'])
  55. # 比赛时间,时间戳
  56. # ctime = "2019" + "-" + month + "-" + day + "" + time_game + ":00"
  57. us_date = "{}-{}-{}".format(datetime.datetime.now().year, month, day)
  58. # 现在时间,时间戳
  59. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  60. cdate = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  61. match_date = cdate.split(' ')[0]
  62. match_time = cdate.split(' ')[1]
  63. # 队1分数
  64. score_home = item['score_home']
  65. # 队2分数
  66. score_guest = item['score_guest']
  67. # 第几节
  68. jijie = item['jijie']
  69. # # 球队得分
  70. pt = 4
  71. # 取不到 暂时注掉
  72. # 每局比分
  73. score_dict = item['score_dict']
  74. # 让盘
  75. concedes_dict = item['concedes_dict']
  76. concedes_dict_rule = item['concedes_dict_rule']
  77. # 冠军 独赢
  78. kemps_dict = item['kemps_dict']
  79. # 让局
  80. bureaus_dict = item['bureaus_dict']
  81. bureaus_dict_rule = item['bureaus_dict_rule']
  82. # 总局数大小
  83. total_number_dict = item['total_number_dict']
  84. total_number_dict_rule = item['total_number_dict_rule']
  85. # 总局数单双
  86. odd_evens_dict = item['odd_evens_dict']
  87. odd_evens_dict_rule = item['odd_evens_dict_rule']
  88. # sq_dict = item['sq_dict']
  89. # new_sq_dict = json.dumps(sq_dict)
  90. uuid = Helper.genearte_uuid(league_name)
  91. """联赛"""
  92. last_time = '{}-12-31 23:59:59'.format(datetime.datetime.now().year)
  93. if self.db.wq_league35.find({'uuid': uuid}).count() < 1:
  94. league_dict = {"game_code": "wq", "title": "league", "source": "hg3535"}
  95. league_list = []
  96. league_key = ["name_chinese", "kind", "match_mode", "if_stop", "last_time", "lg_id", "source", "uuid"]
  97. league_value = [league_name, "1", "1", "0", last_time, league_id, "hg3535", uuid]
  98. league_data = dict(zip(league_key, league_value))
  99. league_list.append(league_data)
  100. league_dict['data'] = league_list
  101. res = Helper.async_post(LEAGUE_URL, league_dict)
  102. if res:
  103. if res.get('status') == 1:
  104. self.db.wq_league35.insert(league_data)
  105. # self.db.wq_league35.update({'lg_id': league_id}, {'$set': league_data},
  106. # upsert=True)
  107. logger.info('网球联赛提交成功, {}'.format(res))
  108. else:
  109. logger.warning('网球联赛提交失败, {}'.format(res))
  110. logger.warning(league_dict)
  111. else:
  112. logger.warning('网球滚球联赛接口异常提交失败, {}'.format(res))
  113. logger.warning(league_dict)
  114. else:
  115. logger.info('网球联赛已存在, 不提交')
  116. """赛事"""
  117. match_identity = Helper.genearte_uuid(team_home + team_guest + us_date)
  118. if self.db.wq_competition35.find({"match_identity": match_identity, '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", 'match_identity']
  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, match_identity]
  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", 'match_identity']
  227. odds_value = ["wq", "odds", match_id, league_id, data_list, "hg3535", odds_onlys, tag_number, uuid,
  228. 0, utime, pt, match_identity]
  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, 'match_identity': match_identity,
  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. reactor.callFromThread(out.callback, item)