bangqiu.py 15 KB

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