roll_wangqiu.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import datetime
  2. import pymongo
  3. import time
  4. import logging
  5. import json
  6. from .ball_func import fuhao
  7. from ..utils.helper import Helper
  8. from .ball_func import get_pcode, new_time, out_time
  9. from concurrent.futures import ThreadPoolExecutor
  10. from requests_futures.sessions import FuturesSession
  11. class Roll_Wangqiupipeline(object):
  12. def open_spider(self, spider):
  13. self.mongo = pymongo.MongoClient(host="192.168.2.200", username="kaiyou", password="kaiyou", port=27017)
  14. self.db = self.mongo['kaiyou']
  15. self.session = FuturesSession(executor=ThreadPoolExecutor(max_workers=10))
  16. def process_item(self, item, spider):
  17. url = 'http://admin.5gogo.com'
  18. logger = logging.getLogger(__name__)
  19. # 联赛id
  20. league_id = item['league_id']
  21. # 联赛名
  22. league_name = item['league_name']
  23. # result = item['result']
  24. # 比赛id
  25. match_id = item['game_id']
  26. # 球队1
  27. team_home = item['team_home']
  28. # 球队2
  29. team_guest = item['team_guest']
  30. # 数量(97>)
  31. tag_number = item['number']
  32. # 比赛状态
  33. zhuangtai = item['zhuangtai']
  34. # 日期
  35. # data_game = item['data_game']
  36. try:
  37. data_game = item['data_game'].split("/")
  38. month = str(data_game[1].strip())
  39. day = str(data_game[0])
  40. except Exception as e:
  41. logger.warning(e)
  42. data_game = item['data_game'].split(" ")
  43. months = str(data_game[1].strip())
  44. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  45. 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  46. month = month_dict[months]
  47. day = str(data_game[0])
  48. # 比赛时间
  49. time_game = str(item['time_game'])
  50. # 比赛时间,时间戳
  51. ctime = "2019" + "-" + month + "-" + day + "" + time_game + ":00"
  52. r_ctime = "2019" + "-" + month + "-" + day
  53. # 现在时间,时间戳
  54. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  55. # 队1分数
  56. score_home = item['score_home']
  57. # 队2分数
  58. score_guest = item['score_guest']
  59. # 第几节
  60. jijie = item['jijie']
  61. # 球队得分
  62. qiudui = item['qiudui']
  63. pt = 4
  64. # 取不到 暂时注掉
  65. # match_date, match_time = new_times(ctime)
  66. # 每局比分
  67. score_dict = item['score_dict']
  68. # 让盘
  69. concedes_dict = item['concedes_dict']
  70. concedes_dict_rule = item['concedes_dict_rule']
  71. # 冠军 独赢
  72. kemps_dict = item['kemps_dict']
  73. # 让局
  74. bureaus_dict = item['bureaus_dict']
  75. bureaus_dict_rule = item['bureaus_dict_rule']
  76. # 总局数大小
  77. total_number_dict = item['total_number_dict']
  78. total_number_dict_rule = item['total_number_dict_rule']
  79. # 总局数单双
  80. odd_evens_dict = item['odd_evens_dict']
  81. odd_evens_dict_rule = item['odd_evens_dict_rule']
  82. sq_dict = item['sq_dict']
  83. new_sq_dict = json.dumps(sq_dict)
  84. uuid = Helper.genearte_MD5(league_name + 'hg3535')
  85. """联赛"""
  86. last_time = '2019-12-31 23:59:59'
  87. league_dict = {"game_code": "wq", "title": "league", "source": "hg3535"}
  88. league_list = []
  89. league_key = ["name_chinese", "kind", "match_mode", "if_stop", "last_time", "lg_id", "source", "uuid"]
  90. league_value = [league_name, "1", "1", "0", last_time, league_id, "hg3535", uuid]
  91. league_data = dict(zip(league_key, league_value))
  92. league_list.append(league_data)
  93. league_dict['data'] = league_list
  94. if self.db.hg3535_wq_league.find({'lg_id': league_id}).count() < 1:
  95. res = Helper.async_post(url + '/setLeague', league_dict)
  96. print(res)
  97. self.db.hg3535_wq_league.insert(league_list)
  98. """赛事"""
  99. competition = self.db.hg3535_wq_competition.find_one({"match_id": match_id})
  100. match_list = []
  101. if competition:
  102. print('网球赛事已存在,修改网球赛事四状态')
  103. is_morningplate = competition['is_morningplate']
  104. is_rollball = competition['is_rollball']
  105. is_stringscene = competition['is_stringscene']
  106. is_today = competition['is_today']
  107. status = competition['status']
  108. match_date = competition['match_date']
  109. match_time = competition['match_time']
  110. us_time = competition['us_time']
  111. if is_rollball != 1 or status != 1:
  112. match_dict = {"game_code": "wq", "title": "match", "source": "hg3535"}
  113. match_key = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  114. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  115. "half_match_id", "is_today", 'rule']
  116. match_value = [team_home, team_guest, league_id, 1, match_id, match_date, match_time, tag_number,
  117. "hg3535", 1, is_morningplate, is_stringscene, us_time, uuid, 0, is_today, '']
  118. match_data = dict(zip(match_key, match_value))
  119. match_list.append(match_data)
  120. match_dict['data'] = match_list
  121. res = Helper.async_post(url + '/setMatch', match_dict)
  122. print(res)
  123. self.db.hg3535_wq_competition.insert(match_list)
  124. self.db.hg3535_wq_competition.update({'match_id': str(match_id)},
  125. {"$set": {"is_rollball": 1, "status": 1}},
  126. upsert=True)
  127. else:
  128. print("网球赛事已存在,网球赛事状态时间未改变,不做修改")
  129. else:
  130. is_rollball = 1
  131. is_today = 0
  132. is_morningplate = 0
  133. is_stringscene = 0
  134. match_dict = {"game_code": "wq", "title": "match", "source": "hg3535"}
  135. match_key = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  136. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  137. "half_match_id", "is_today", 'rule']
  138. match_value = [team_home, team_guest, league_id, 1, match_id, r_ctime, "00:00", tag_number,
  139. "hg3535", is_rollball, is_morningplate, is_stringscene, utime, uuid, 0, is_today, '']
  140. match_data = dict(zip(match_key, match_value))
  141. match_list.append(match_data)
  142. match_dict['data'] = match_list
  143. res = Helper.async_post(url + '/setMatch', match_dict)
  144. print(res)
  145. self.db.hg3535_wq_competition.insert(match_list)
  146. """赔率"""
  147. try:
  148. res = self.db.hg3535_wq_odds.find_one({'match_id': str(match_id), 'pt': pt})
  149. odds_only_list = res['odds_only']
  150. except:
  151. res = ''
  152. odds_only_list = []
  153. data_list = []
  154. odds_onlys = []
  155. # 滚球独赢
  156. if kemps_dict:
  157. for key, value in kemps_dict.items():
  158. if value:
  159. sole_str = 'C' + str(key) + '0' + str(match_id) + 'hg3535'
  160. sole = Helper.genearte_MD5(sole_str)
  161. odds_str = 'C' + str(key) + '0' + '0' + str(value) + 'hg3535' + str(match_id)
  162. odds_only = Helper.genearte_MD5(odds_str)
  163. kemps_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  164. "condition", "odds_only", "sole", "source", "type", "team"]
  165. kemps_value = [match_id, league_id, key, "0", 0, 'C', value, '',
  166. odds_only, sole, "hg3535", "0", ""]
  167. kemps_data = dict(zip(kemps_key, kemps_value))
  168. odds_onlys.append(odds_only)
  169. if odds_only not in odds_only_list:
  170. data_list.append(kemps_data)
  171. # 滚球让盘
  172. if concedes_dict:
  173. for key, value in concedes_dict.items():
  174. if value:
  175. sole_str = 'LD' + str(key) + '0' + str(match_id) + 'hg3535'
  176. sole = Helper.genearte_MD5(sole_str)
  177. odds_str = 'LD' + str(key) + '0' + str(concedes_dict_rule[key]) + str(
  178. value) + 'hg3535' + str(match_id)
  179. odds_only = Helper.genearte_MD5(odds_str)
  180. condition = fuhao(concedes_dict_rule[key])
  181. concedes_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  182. "condition", "odds_only", "sole", "source", "type", "team"]
  183. concedes_value = [match_id, league_id, key, "0", 0, 'LD', value, condition,
  184. odds_only, sole, "hg3535", "0", ""]
  185. concedes_data = dict(zip(concedes_key, concedes_value))
  186. odds_onlys.append(odds_only)
  187. if odds_only not in odds_only_list:
  188. data_list.append(concedes_data)
  189. # 滚球让局
  190. if bureaus_dict:
  191. for key, value in bureaus_dict.items():
  192. if value:
  193. sole_str = 'LB' + str(key) + '0' + str(match_id) + 'hg3535'
  194. sole = Helper.genearte_MD5(sole_str)
  195. odds_str = 'LB' + str(key) + '0' + str(bureaus_dict_rule[key]) + str(
  196. value) + 'hg3535' + str(match_id)
  197. odds_only = Helper.genearte_MD5(odds_str)
  198. condition = fuhao(bureaus_dict_rule[key])
  199. bureaus_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  200. "condition", "odds_only", "sole", "source", "type", "team"]
  201. bureaus_value = [match_id, league_id, key, "0", 0, 'LB', value, condition,
  202. odds_only, sole, "hg3535", "0", ""]
  203. bureaus_data = dict(zip(bureaus_key, bureaus_value))
  204. odds_onlys.append(odds_only)
  205. if odds_only not in odds_only_list:
  206. data_list.append(bureaus_data)
  207. # 滚球总局数大小
  208. if total_number_dict:
  209. for key, value in total_number_dict.items():
  210. if value:
  211. sole_str = 'TN' + str(key) + '0' + str(match_id) + 'hg3535'
  212. sole = Helper.genearte_MD5(sole_str)
  213. odds_str = 'TN' + str(key) + '0' + str(
  214. total_number_dict_rule[key]) + str(value) + 'hg3535' + str(match_id)
  215. odds_only = Helper.genearte_MD5(odds_str)
  216. condition = fuhao(total_number_dict_rule[key])
  217. total_number_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  218. "condition", "odds_only", "sole", "source", "type", "team"]
  219. total_number_value = [match_id, league_id, key, "0", 0, 'TN', value,
  220. condition, odds_only, sole, "hg3535", "0", ""]
  221. total_number_data = dict(zip(total_number_key, total_number_value))
  222. odds_onlys.append(odds_only)
  223. if odds_only not in odds_only_list:
  224. data_list.append(total_number_data)
  225. # 滚球总局数单双
  226. if odd_evens_dict:
  227. for key, value in odd_evens_dict.items():
  228. if value:
  229. condition = odd_evens_dict_rule[key]
  230. odds_str = "TS" + str(key) + '0' + str(odd_evens_dict_rule[key]) + str(
  231. value) + "hg3535" + str(match_id)
  232. sole_str = "TS" + str(key) + '0' + str(match_id) + "hg3535"
  233. odds_only = Helper.genearte_MD5(odds_str)
  234. sole = Helper.genearte_MD5(sole_str)
  235. odds_onlys.append(odds_only)
  236. odd_evens_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  237. "condition", "odds_only", "sole", "source", "type", "team"]
  238. odd_evens_value = [match_id, league_id, key, "0", 0, 'TS', value,
  239. condition, odds_only, sole, "hg3535", "0", ""]
  240. odd_evens_data = dict(zip(odd_evens_key, odd_evens_value))
  241. if odds_only not in odds_only_list:
  242. data_list.append(odd_evens_data)
  243. odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid",
  244. "is_stringscene", "utime", "pt"]
  245. odds_value = ["wq", "odds", match_id, league_id, data_list, "hg3535", odds_onlys, tag_number, uuid,
  246. 0, utime, pt]
  247. odds_dict = dict(zip(odds_key, odds_value))
  248. # print(odds_dict)
  249. if data_list:
  250. res_data = Helper.async_post(url + '/setOdds', odds_dict)
  251. print(res_data)
  252. if "成功" in res_data:
  253. if res:
  254. self.db.hg3535_wq_odds.update({'match_id': match_id, 'pt': pt}, {
  255. '$set': {"data": data_list, "odds_only": odds_onlys, "utime": utime}}, upsert=True)
  256. else:
  257. self.db.hg3535_wq_odds.insert(odds_dict)
  258. # 赛事结果
  259. da_list = []
  260. wq_rball = {"lg_id": league_id, "home_player_name": team_home, "guest_player_name": team_guest,
  261. "home_player_let_plate": '', "guest_player_let_plate": '', "home_player_let_inning": "",
  262. "guest_player_let_inning": "", "all_inning": score_dict['total_inning'],
  263. "home_player_score": score_home,
  264. "guest_player_score": score_guest,
  265. "status": 1, "first_score_player": "", "last_score_player": "",
  266. "first_inning_score": score_dict['one_score'],
  267. "second_inning_score": score_dict['second_score'], "third_inning_score": score_dict['third_score'],
  268. "match_winer_player": "", "update_time": "",
  269. "match_time": time_game, "match_process": jijie, "tag": tag_number, "match_id": match_id,
  270. "source": "hg3535", "result_mark": score_dict, "warn_more": '', "uuid": uuid,
  271. # "match_score": score_dict['current_point_home_guest'], "ctime": ctime, "inning": "",
  272. # "strat_time": ""
  273. }
  274. da_list.append(wq_rball)
  275. r_data_dict = {
  276. "game_code": "wq",
  277. "title": "match_result_r",
  278. "source": "hg3535",
  279. "data": da_list
  280. }
  281. if da_list:
  282. try:
  283. res = Helper.async_post(url + '/setMatchResult', r_data_dict)
  284. print(res)
  285. except Exception as e:
  286. logger.warning('网球滚球接口数据异常, 提交失败')
  287. logger.warning(e)