bangqiu.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import datetime
  2. import pymongo
  3. import time
  4. import logging
  5. from twisted.internet import defer, reactor
  6. from .ball_func import fuhao
  7. from ..utils.helper import Helper
  8. from .ball_func import new_time
  9. from ..settings import M_HOST, M_USER, M_PASSWORD, M_POST, M_DB, LEAGUE_URL, ODDS_URL, MATCH_URL, MATCH_RESULT
  10. class Bangqiupipeline(object):
  11. def open_spider(self, spider):
  12. self.mongo = pymongo.MongoClient(host=M_HOST, username=M_USER, password=M_PASSWORD, port=M_POST,
  13. authSource='kaiyou')
  14. self.db = self.mongo[M_DB]
  15. @defer.inlineCallbacks
  16. def process_item(self, item, spider):
  17. out = defer.Deferred()
  18. reactor.callInThread(self._do_calculation, item, out)
  19. yield out
  20. defer.returnValue(item)
  21. def _do_calculation(self, item, out):
  22. # def process_item(self, item, spider):
  23. logger = logging.getLogger(__name__)
  24. # logger.warning(query.addErrback(self.handle_error, item, spider))
  25. # 联赛id
  26. league_id = item['league_id']
  27. # 联赛名
  28. league_name = item['league_name']
  29. # result = item['result']
  30. # 比赛id
  31. match_id = item['game_id']
  32. # 球队1
  33. team_home = item['team_home']
  34. # 球队2
  35. team_guest = item['team_guest']
  36. # 数量(97>)
  37. tag_number = item['number']
  38. # 比赛状态
  39. zhuangtai = item['zhuangtai']
  40. # 日期
  41. # data_game = item['data_game']
  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. 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])
  53. # 比赛时间
  54. time_game = str(item['time_game'])
  55. # 比赛时间,时间戳
  56. ctime = "2019" + "-" + month + "-" + day + " " + time_game + ":00"
  57. r_ctime = "2019" + "-" + month + "-" + day
  58. # 现在时间,时间戳
  59. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  60. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))
  61. # 队1分数
  62. score_home = item['score_home']
  63. # 队2分数
  64. score_guest = item['score_guest']
  65. # 第几节
  66. jijie = item['jijie']
  67. # 球队得分
  68. qiudui = item['qiudui']
  69. pt = str(item['pt'])
  70. # 让球
  71. concedes_dict = item['concedes_dict']
  72. concedes_dict_rule = item['concedes_dict_rule']
  73. # 独赢
  74. capots_dict = item['capots_dict']
  75. # 总得分大小
  76. total_size_dict = item['total_size_dict']
  77. total_size_dict_rule = item['total_size_dict_rule']
  78. # 总得分单双
  79. odd_evens_dict = item['odd_evens_dict']
  80. odd_evens_dict_rule = item['odd_evens_dict_rule']
  81. # # 球队得分 主队
  82. # try:
  83. # team_score_home_dict = item['team_score_home_dict']
  84. # team_score_home_rule_dict = item['team_score_home_rule_dict']
  85. # except:
  86. # team_score_home_dict = ""
  87. # team_score_home_rule_dict = ""
  88. # # 球队得分 客队
  89. # try:
  90. # team_score_guest_dict = item['team_score_guest_dict']
  91. # team_score_guest_rule_dict = item['team_score_guest_rule_dict']
  92. # except:
  93. # team_score_guest_dict = ""
  94. # team_score_guest_rule_dict = ""
  95. bangqiu = item['bangqiu']
  96. match_date, match_time, time3 = new_time(ctime)
  97. us_time = ctime
  98. uuid = Helper.genearte_uuid(league_name + 'hg3535')
  99. """联赛表"""
  100. last_time = '{}-12-31 23:59:59'.format(datetime.datetime.now().year)
  101. # print(league_list)
  102. # print(league_dict)
  103. if self.db.bq_league35.find({'lg_id': league_id}).count() < 1:
  104. league_dict = {"game_code": "bq", "title": "league", "source": "hg3535"}
  105. league_list = []
  106. league_key = ["name_chinese", "kind", "match_mode", "if_stop", "last_time", "lg_id", "source", "uuid"]
  107. league_value = [league_name, "1", "1", "0", last_time, league_id, "hg3535", uuid]
  108. league_data = dict(zip(league_key, league_value))
  109. league_list.append(league_data)
  110. league_dict['data'] = league_list
  111. res = Helper.async_post(LEAGUE_URL, league_dict)
  112. if '成功' in res:
  113. self.db.bq_league35.insert(league_data)
  114. logger.info('棒球联赛提交成功, {}'.format(res))
  115. else:
  116. logger.warning('棒球联赛提交失败, {}'.format(res))
  117. logger.warning(league_dict)
  118. pt_dict = {'1': 'is_today', '2': 'is_morningplate', '3': 'is_stringscene', '4': 'is_rollball'}
  119. pt_status = pt_dict[pt]
  120. if pt == '3':
  121. is_rollball = 0
  122. is_today = 0
  123. is_morningplate = 0
  124. is_stringscene = 1
  125. elif pt == '2':
  126. is_rollball = 0
  127. is_today = 0
  128. is_morningplate = 1
  129. is_stringscene = 0
  130. else:
  131. is_today = 1
  132. is_rollball = 0
  133. is_morningplate = 0
  134. is_stringscene = 0
  135. """插入赛事表"""
  136. if self.db.bq_competition35.find({"match_id": match_id, pt_status: 1}).count() < 1:
  137. match_list = []
  138. match_dict = {"game_code": "bq", "title": "match", "source": "hg3535"}
  139. match_key = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  140. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  141. "half_match_id", "is_today"]
  142. match_value = [team_home, team_guest, league_id, 0, match_id, match_date, match_time, tag_number,
  143. "hg3535", is_rollball, is_morningplate, is_stringscene, us_time, uuid, 0, is_today]
  144. match_data = dict(zip(match_key, match_value))
  145. match_list.append(match_data)
  146. match_dict['data'] = match_list
  147. res = Helper.async_post(MATCH_URL, match_dict)
  148. if '成功' in res:
  149. self.db.bq_competition35.insert(match_data)
  150. logger.info('棒球赛事提交成功, {}'.format(res))
  151. else:
  152. logger.warning('棒球赛事提交失败, {}'.format(res))
  153. logger.warning(match_data)
  154. else:
  155. logger.info("棒球赛事已存在,不做修改")
  156. """赔率"""
  157. data_list = []
  158. odds_onlys = []
  159. # 让球
  160. if concedes_dict:
  161. for key, value in concedes_dict.items():
  162. sole_str = 'CO' + str(key) + str(match_id) + 'hg3535'
  163. sole = Helper.genearte_MD5(sole_str, pt)
  164. odds_str = 'CO' + str(key) + str(match_id) + 'hg3535' + str(concedes_dict_rule[key]) + str(
  165. value)
  166. odds_only = Helper.genearte_MD5(odds_str, pt)
  167. condition = fuhao(str(concedes_dict_rule[key]))
  168. concedes_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition",
  169. "odds_only", "sole", "source", "type", "team"]
  170. concedes_dict_value = [match_id, league_id, key, '0', 0, 'CO', value, condition,
  171. odds_only, sole, 'hg3535', '0', '']
  172. concedes_dict_data = dict(zip(concedes_dict_key, concedes_dict_value))
  173. data_list.append(concedes_dict_data)
  174. # 总得分大/小
  175. if total_size_dict:
  176. for key, value in total_size_dict.items():
  177. sole_str = 'TN' + str(key) + str(match_id) + 'hg3535'
  178. sole = Helper.genearte_MD5(sole_str, pt)
  179. odds_str = 'TN' + str(key) + str(match_id) + 'hg3535' + str(
  180. total_size_dict_rule[key]) + str(value)
  181. odds_only = Helper.genearte_MD5(odds_str, pt)
  182. condition = fuhao(str(total_size_dict_rule[key]))
  183. total_size_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  184. "condition", "odds_only", "sole", "source", "type", "team"]
  185. total_size_dict_value = [match_id, league_id, key, '0', 0, 'TN', value, condition,
  186. odds_only, sole, 'hg3535', '0', '']
  187. total_size_dict_data = dict(zip(total_size_dict_key, total_size_dict_value))
  188. data_list.append(total_size_dict_data)
  189. # 球队得分大/小 主队
  190. # if team_score_home_dict:
  191. # for key, value in team_score_home_dict.items():
  192. # sole_str = 'team_score_home' + str(key) + str(match_id) + 'hg3535'
  193. # sole = Helper.genearte_MD5(sole_str)
  194. # odds_str = 'team_score_home' + str(key) + str(match_id) + 'hg3535' + str(
  195. # team_score_home_rule_dict[key]) + str(value)
  196. # odds_only = Helper.genearte_MD5(odds_str)
  197. # condition = fuhao(str(team_score_home_rule_dict[key]))
  198. # team_score_home_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  199. # "condition", "odds_only", "sole", "source", "type", "team"]
  200. # team_score_home_dict_value = [match_id, league_id, key, '0', '0', 'team_score_home', value, condition,
  201. # odds_only, sole, 'hg3535', '0', '']
  202. # team_score_home_dict_data = dict(zip(team_score_home_dict_key, team_score_home_dict_value))
  203. # odds_onlys.append(odds_only)
  204. # if odds_only not in odds_only_list:
  205. # data_list.append(team_score_home_dict_data)
  206. # # 球队得分大/小 客队
  207. # if team_score_guest_dict:
  208. # for key, value in team_score_guest_dict.items():
  209. # sole_str = 'team_score_guest' + str(key) + str(match_id) + 'hg3535'
  210. # sole = Helper.genearte_MD5(sole_str)
  211. # odds_str = 'team_score_guest' + str(key) + str(match_id) + 'hg3535' + str(
  212. # team_score_guest_rule_dict[key]) + str(value)
  213. # odds_only = Helper.genearte_MD5(odds_str)
  214. # condition = fuhao(str(team_score_guest_rule_dict[key]))
  215. # team_score_guest_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  216. # "condition", "odds_only", "sole", "source", "type", "team"]
  217. # team_score_guest_dict_value = [match_id, league_id, key, '0', '0', 'team_score_guest', value, condition,
  218. # odds_only, sole, 'hg3535', '0', '']
  219. # team_score_guest_dict_data = dict(zip(team_score_guest_dict_key, team_score_guest_dict_value))
  220. # odds_onlys.append(odds_only)
  221. # if odds_only not in odds_only_list:
  222. # data_list.append(team_score_guest_dict_data)
  223. # 独赢
  224. if capots_dict:
  225. for key, value in capots_dict.items():
  226. sole_str = 'C' + str(key) + str(match_id) + 'hg3535'
  227. sole = Helper.genearte_MD5(sole_str, pt)
  228. odds_str = 'C' + str(key) + str(match_id) + 'hg3535' + str(value)
  229. odds_only = Helper.genearte_MD5(odds_str, pt)
  230. capots_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  231. "condition", "odds_only", "sole", "source", "type", "team"]
  232. capots_dict_value = [match_id, league_id, key, '0', 0, 'C', value, '',
  233. odds_only, sole, 'hg3535', '0', '']
  234. capots_dict_data = dict(zip(capots_dict_key, capots_dict_value))
  235. data_list.append(capots_dict_data)
  236. # 总得分单双
  237. if odd_evens_dict:
  238. for key, value in odd_evens_dict.items():
  239. sole_str = 'TS' + str(key) + str(match_id) + 'hg3535'
  240. sole = Helper.genearte_MD5(sole_str, pt)
  241. odds_str = 'TS' + str(key) + str(match_id) + 'hg3535' + str(
  242. odd_evens_dict_rule[key]) + str(value)
  243. odds_only = Helper.genearte_MD5(odds_str, pt)
  244. condition = fuhao(str(odd_evens_dict_rule[key]))
  245. odd_evens_dict_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  246. "condition", "odds_only", "sole", "source", "type", "team"]
  247. odd_evens_dict_value = [match_id, league_id, key, '0', 0, 'TS', value, condition,
  248. odds_only, sole, 'hg3535', '0', '']
  249. odd_evens_dict_data = dict(zip(odd_evens_dict_key, odd_evens_dict_value))
  250. data_list.append(odd_evens_dict_data)
  251. # 让球 前五局
  252. # 总得分大/小 前五局
  253. if pt == '3':
  254. ris_stringscene = 1
  255. else:
  256. ris_stringscene = 0
  257. odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid",
  258. "is_stringscene", "utime", "pt"]
  259. odds_value = ["bq", "odds", match_id, league_id, data_list, "hg3535", odds_onlys, tag_number, uuid,
  260. ris_stringscene, utime, pt]
  261. odds_dict = dict(zip(odds_key, odds_value))
  262. if data_list:
  263. res = Helper.async_post(ODDS_URL, odds_dict)
  264. if res:
  265. if "成功" in res:
  266. logger.info('棒球赔率提交成功, {}'.format(res))
  267. logger.info(odds_dict)
  268. else:
  269. logger.warning('棒球赔率提交失败, {}'.format(res))
  270. logger.warning(odds_dict)
  271. else:
  272. logger.warning('详细接口赔率返回异常, {}'.format(res))
  273. logger.warning(odds_dict)
  274. else:
  275. logger.info('棒球详细赔率列表为空, 不提交')
  276. reactor.callFromThread(out.callback, item)