bangqiu.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. import datetime
  2. import pymongo
  3. import time
  4. import logging
  5. from .ball_func import fuhao
  6. from ..utils.helper import Helper
  7. from .ball_func import get_pcode, new_time, out_time
  8. from concurrent.futures import ThreadPoolExecutor
  9. from requests_futures.sessions import FuturesSession
  10. class Bangqiupipeline(object):
  11. def open_spider(self, spider):
  12. self.mongo = pymongo.MongoClient(host="192.168.2.200", username="kaiyou", password="kaiyou", port=27017)
  13. self.db = self.mongo['kaiyou']
  14. self.session = FuturesSession(executor=ThreadPoolExecutor(max_workers=10))
  15. def process_item(self, item, spider):
  16. # print(item)
  17. logger = logging.getLogger(__name__)
  18. url = 'http://admin.5gogo.com'
  19. # logger.warning(query.addErrback(self.handle_error, item, spider))
  20. # 联赛id
  21. league_id = item['league_id']
  22. # 联赛名
  23. league_name = item['league_name']
  24. # result = item['result']
  25. # 比赛id
  26. match_id = item['game_id']
  27. # 球队1
  28. team_home = item['team_home']
  29. # 球队2
  30. team_guest = item['team_guest']
  31. # 数量(97>)
  32. tag_number = item['number']
  33. # 比赛状态
  34. zhuangtai = item['zhuangtai']
  35. # 日期
  36. # data_game = item['data_game']
  37. try:
  38. data_game = item['data_game'].split("/")
  39. month = str(data_game[1].strip())
  40. day = str(data_game[0])
  41. except Exception as e:
  42. # logger.warning('data_game字段分割错误')
  43. # logger.warning(e)
  44. data_game = item['data_game'].split(" ")
  45. months = str(data_game[1].strip())
  46. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  47. 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  48. month = month_dict[months]
  49. day = str(data_game[0])
  50. # 比赛时间
  51. time_game = str(item['time_game'])
  52. # 比赛时间,时间戳
  53. ctime = "2019" + "-" + month + "-" + day + " " + time_game + ":00"
  54. r_ctime = "2019" + "-" + month + "-" + day
  55. # 现在时间,时间戳
  56. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  57. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))
  58. # 队1分数
  59. score_home = item['score_home']
  60. # 队2分数
  61. score_guest = item['score_guest']
  62. # 第几节
  63. jijie = item['jijie']
  64. # 球队得分
  65. qiudui = item['qiudui']
  66. pt = item['pt']
  67. # 让球
  68. concedes_dict = item['concedes_dict']
  69. concedes_dict_rule = item['concedes_dict_rule']
  70. # 独赢
  71. capots_dict = item['capots_dict']
  72. # 总得分大小
  73. total_size_dict = item['total_size_dict']
  74. total_size_dict_rule = item['total_size_dict_rule']
  75. # 总得分单双
  76. odd_evens_dict = item['odd_evens_dict']
  77. odd_evens_dict_rule = item['odd_evens_dict_rule']
  78. # # 球队得分 主队
  79. # try:
  80. # team_score_home_dict = item['team_score_home_dict']
  81. # team_score_home_rule_dict = item['team_score_home_rule_dict']
  82. # except:
  83. # team_score_home_dict = ""
  84. # team_score_home_rule_dict = ""
  85. # # 球队得分 客队
  86. # try:
  87. # team_score_guest_dict = item['team_score_guest_dict']
  88. # team_score_guest_rule_dict = item['team_score_guest_rule_dict']
  89. # except:
  90. # team_score_guest_dict = ""
  91. # team_score_guest_rule_dict = ""
  92. bangqiu = item['bangqiu']
  93. match_date, match_time, time3 = new_time(ctime)
  94. us_time = ctime
  95. uuid = Helper.genearte_MD5(league_name + 'hg3535')
  96. """联赛表"""
  97. last_time = '2019-12-31 23:59:59'
  98. league_dict = {"game_code": "bq", "title": "league", "source": "hg3535"}
  99. league_list = []
  100. league_key = ["name_chinese", "kind", "match_mode", "if_stop", "last_time", "lg_id", "source", "uuid"]
  101. league_value = [league_name, "1", "1", "0", last_time, league_id, "hg3535", uuid]
  102. league_data = dict(zip(league_key, league_value))
  103. league_list.append(league_data)
  104. league_dict['data'] = league_list
  105. # print(league_list)
  106. # print(league_dict)
  107. if self.db.hg3535_bq_league.find({'lg_id': league_id}).count() < 1:
  108. res = Helper.async_post(url + '/setLeague', league_dict)
  109. print(res)
  110. self.db.hg3535_bq_league.insert(league_list)
  111. """插入赛事表"""
  112. competition = self.db.hg3535_bq_competition.find_one({"match_id": str(match_id)})
  113. match_list = []
  114. if competition:
  115. print('赛事已存在,修改赛事四状态')
  116. is_morningplate = competition['is_morningplate']
  117. is_rollball = competition['is_rollball']
  118. is_stringscene = competition['is_stringscene']
  119. is_today = competition['is_today']
  120. lmtime = competition['match_time']
  121. if pt == 3:
  122. if is_stringscene != 1 or lmtime != match_time:
  123. match_dict = {"game_code": "bq", "title": "match", "source": "hg3535"}
  124. match_key = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  125. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  126. "half_match_id", "is_today"]
  127. match_value = [team_home, team_guest, league_id, 0, match_id, match_date, match_time, tag_number,
  128. "hg3535", is_rollball, is_morningplate, 1, us_time, uuid, 0, is_today]
  129. match_data = dict(zip(match_key, match_value))
  130. match_list.append(match_data)
  131. match_dict['data'] = match_list
  132. # print(match_list)
  133. # print(match_dict)
  134. res = Helper.async_post(url + '/setMatch', match_dict)
  135. print(res)
  136. self.db.hg3535_bq_competition.update({'match_id': match_id},
  137. {"$set": {"is_stringscene": 1, 'match_time': match_time}},
  138. upsert=True)
  139. if pt == 2:
  140. if is_morningplate != 1 or lmtime != match_time:
  141. match_dict = {"game_code": "bq", "title": "match", "source": "hg3535"}
  142. match_key = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  143. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  144. "half_match_id", "is_today"]
  145. match_value = [team_home, team_guest, league_id, 0, match_id, match_date, match_time, tag_number,
  146. "hg3535", is_rollball, 1, is_stringscene, us_time, uuid, 0, is_today]
  147. match_data = dict(zip(match_key, match_value))
  148. match_list.append(match_data)
  149. match_dict['data'] = match_list
  150. # print(match_list)
  151. # print(match_dict)
  152. res = Helper.async_post(url + '/setMatch', match_dict)
  153. print(res)
  154. self.db.hg3535_bq_competition.update({'match_id': match_id},
  155. {"$set": {"is_morningplate": 1, 'match_time': match_time}},
  156. upsert=True)
  157. if pt == 1:
  158. if is_today != 1 or lmtime != match_time:
  159. match_dict = {"game_code": "bq", "title": "match", "source": "hg3535"}
  160. match_kay = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  161. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  162. "half_match_id", "is_today"]
  163. match_value = [team_home, team_guest, league_id, 0, match_id, match_date, match_time, tag_number,
  164. "hg3535", is_rollball, is_morningplate, is_stringscene, us_time, uuid, 0, 1]
  165. match_data = dict(zip(match_kay, match_value))
  166. match_list.append(match_data)
  167. match_dict['data'] = match_list
  168. # print(match_list)
  169. # print(match_dict)
  170. res = Helper.async_post(url + '/setMatch', match_dict)
  171. print(res)
  172. self.db.hg3535_bq_competition.update({'match_id': match_id},
  173. {"$set": {"is_today": 1, 'match_time': match_time}},
  174. upsert=True)
  175. else:
  176. print("赛事已存在,赛事状态时间未改变,不做修改")
  177. else:
  178. print('赛事不存在, 插入新数据')
  179. if pt == 3:
  180. is_rollball = 0
  181. is_today = 0
  182. is_morningplate = 0
  183. is_stringscene = 1
  184. elif pt == 2:
  185. is_rollball = 0
  186. is_today = 0
  187. is_morningplate = 1
  188. is_stringscene = 0
  189. else:
  190. is_today = 1
  191. is_rollball = 0
  192. is_morningplate = 0
  193. is_stringscene = 0
  194. match_dict = {"game_code": "bq", "title": "match", "source": "hg3535"}
  195. match_kay = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time", "tag",
  196. "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  197. "half_match_id", "is_today"]
  198. match_value = [team_home, team_guest, league_id, 0, match_id, match_date, match_time, tag_number, "hg3535",
  199. is_rollball, is_morningplate, is_stringscene, us_time, uuid, 0, is_today]
  200. match_data = dict(zip(match_kay, match_value))
  201. match_list.append(match_data)
  202. match_dict['data'] = match_list
  203. res = Helper.async_post(url + '/setMatch', match_dict)
  204. print(res)
  205. self.db.hg3535_bq_competition.insert(match_list)
  206. """赔率"""
  207. try:
  208. result = self.db.hg3535_bq_odds.find_one({'match_id': str(match_id), 'pt': pt})
  209. odds_only_list = result['odds_only']
  210. except:
  211. result = ''
  212. odds_only_list = []
  213. data_list = []
  214. odds_onlys = []
  215. # 让球
  216. if concedes_dict:
  217. for key, value in concedes_dict.items():
  218. sole_str = 'CO' + str(key) + str(match_id) + 'hg3535'
  219. sole = Helper.genearte_MD5(sole_str)
  220. odds_str = 'CO' + str(key) + str(match_id) + 'hg3535' + str(concedes_dict_rule[key]) + str(
  221. value)
  222. odds_only = Helper.genearte_MD5(odds_str)
  223. condition = fuhao(str(concedes_dict_rule[key]))
  224. concedes_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition",
  225. "odds_only", "sole", "source", "type", "team"]
  226. concedes_dict_value = [match_id, league_id, key, '0', 0, 'CO', value, condition,
  227. odds_only, sole, 'hg3535', '0', '']
  228. concedes_dict_data = dict(zip(concedes_dict_key, concedes_dict_value))
  229. odds_onlys.append(odds_only)
  230. if odds_only not in odds_only_list:
  231. data_list.append(concedes_dict_data)
  232. # 总得分大/小
  233. if total_size_dict:
  234. for key, value in total_size_dict.items():
  235. sole_str = 'TN' + str(key) + str(match_id) + 'hg3535'
  236. sole = Helper.genearte_MD5(sole_str)
  237. odds_str = 'TN' + str(key) + str(match_id) + 'hg3535' + str(
  238. total_size_dict_rule[key]) + str(value)
  239. odds_only = Helper.genearte_MD5(odds_str)
  240. condition = fuhao(str(total_size_dict_rule[key]))
  241. total_size_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  242. "condition", "odds_only", "sole", "source", "type", "team"]
  243. total_size_dict_value = [match_id, league_id, key, '0', 0, 'TN', value, condition,
  244. odds_only, sole, 'hg3535', '0', '']
  245. total_size_dict_data = dict(zip(total_size_dict_key, total_size_dict_value))
  246. odds_onlys.append(odds_only)
  247. if odds_only not in odds_only_list:
  248. data_list.append(total_size_dict_data)
  249. # 球队得分大/小 主队
  250. # if team_score_home_dict:
  251. # for key, value in team_score_home_dict.items():
  252. # sole_str = 'team_score_home' + str(key) + str(match_id) + 'hg3535'
  253. # sole = Helper.genearte_MD5(sole_str)
  254. # odds_str = 'team_score_home' + str(key) + str(match_id) + 'hg3535' + str(
  255. # team_score_home_rule_dict[key]) + str(value)
  256. # odds_only = Helper.genearte_MD5(odds_str)
  257. # condition = fuhao(str(team_score_home_rule_dict[key]))
  258. # team_score_home_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  259. # "condition", "odds_only", "sole", "source", "type", "team"]
  260. # team_score_home_dict_value = [match_id, league_id, key, '0', '0', 'team_score_home', value, condition,
  261. # odds_only, sole, 'hg3535', '0', '']
  262. # team_score_home_dict_data = dict(zip(team_score_home_dict_key, team_score_home_dict_value))
  263. # odds_onlys.append(odds_only)
  264. # if odds_only not in odds_only_list:
  265. # data_list.append(team_score_home_dict_data)
  266. # # 球队得分大/小 客队
  267. # if team_score_guest_dict:
  268. # for key, value in team_score_guest_dict.items():
  269. # sole_str = 'team_score_guest' + str(key) + str(match_id) + 'hg3535'
  270. # sole = Helper.genearte_MD5(sole_str)
  271. # odds_str = 'team_score_guest' + str(key) + str(match_id) + 'hg3535' + str(
  272. # team_score_guest_rule_dict[key]) + str(value)
  273. # odds_only = Helper.genearte_MD5(odds_str)
  274. # condition = fuhao(str(team_score_guest_rule_dict[key]))
  275. # team_score_guest_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  276. # "condition", "odds_only", "sole", "source", "type", "team"]
  277. # team_score_guest_dict_value = [match_id, league_id, key, '0', '0', 'team_score_guest', value, condition,
  278. # odds_only, sole, 'hg3535', '0', '']
  279. # team_score_guest_dict_data = dict(zip(team_score_guest_dict_key, team_score_guest_dict_value))
  280. # odds_onlys.append(odds_only)
  281. # if odds_only not in odds_only_list:
  282. # data_list.append(team_score_guest_dict_data)
  283. # 独赢
  284. if capots_dict:
  285. for key, value in capots_dict.items():
  286. sole_str = 'C' + str(key) + str(match_id) + 'hg3535'
  287. sole = Helper.genearte_MD5(sole_str)
  288. odds_str = 'C' + str(key) + str(match_id) + 'hg3535' + str(value)
  289. odds_only = Helper.genearte_MD5(odds_str)
  290. capots_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  291. "condition", "odds_only", "sole", "source", "type", "team"]
  292. capots_dict_value = [match_id, league_id, key, '0', 0, 'C', value, '',
  293. odds_only, sole, 'hg3535', '0', '']
  294. capots_dict_data = dict(zip(capots_dict_key, capots_dict_value))
  295. odds_onlys.append(odds_only)
  296. if odds_only not in odds_only_list:
  297. data_list.append(capots_dict_data)
  298. # 总得分单双
  299. if odd_evens_dict:
  300. for key, value in odd_evens_dict.items():
  301. sole_str = 'TS' + str(key) + str(match_id) + 'hg3535'
  302. sole = Helper.genearte_MD5(sole_str)
  303. odds_str = 'TS' + str(key) + str(match_id) + 'hg3535' + str(
  304. odd_evens_dict_rule[key]) + str(value)
  305. odds_only = Helper.genearte_MD5(odds_str)
  306. condition = fuhao(str(odd_evens_dict_rule[key]))
  307. odd_evens_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  308. "condition", "odds_only", "sole", "source", "type", "team"]
  309. odd_evens_dict_value = [match_id, league_id, key, '0', 0, 'TS', value, condition,
  310. odds_only, sole, 'hg3535', '0', '']
  311. odd_evens_dict_data = dict(zip(odd_evens_dict_key, odd_evens_dict_value))
  312. odds_onlys.append(odds_only)
  313. if odds_only not in odds_only_list:
  314. data_list.append(odd_evens_dict_data)
  315. # 让球 前五局
  316. # 总得分大/小 前五局
  317. if pt == 3:
  318. ris_stringscene = 1
  319. else:
  320. ris_stringscene = 0
  321. odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid",
  322. "is_stringscene", "utime", "pt"]
  323. odds_value = ["bq", "odds", match_id, league_id, data_list, "hg3535", odds_onlys, tag_number, uuid,
  324. ris_stringscene, utime, pt]
  325. odds_dict = dict(zip(odds_key, odds_value))
  326. # print(odds_dict)
  327. # print(123)
  328. if data_list:
  329. res = Helper.async_post(url + '/setOdds', odds_dict)
  330. print(res)
  331. if '成功' in res:
  332. if result:
  333. self.db.hg3535_bq_odds.update({'match_id': match_id, 'pt': pt}, {
  334. '$set': {"data": data_list, "odds_only": odds_onlys, "utime": utime}}, upsert=True)
  335. else:
  336. self.db.hg3535_bq_odds.insert(odds_dict)