roll_bangqiu.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. # encoding: utf-8
  2. import datetime
  3. import pymongo
  4. import time
  5. import logging
  6. from twisted.internet import defer, reactor
  7. from ..utils.helper import Helper
  8. from ..settings import M_HOST, M_USER, M_PASSWORD, M_POST, M_DB, POST_URL
  9. class Roll_Banqiupipeline(object):
  10. def open_spider(self, spider):
  11. self.mongo = pymongo.MongoClient(host=M_HOST, username=M_USER, password=M_PASSWORD, port=M_POST, authSource=M_DB)
  12. self.db = self.mongo[M_DB]
  13. @defer.inlineCallbacks
  14. def process_item(self, item, spider):
  15. out = defer.Deferred()
  16. reactor.callInThread(self._do_calculation, item, out)
  17. yield out
  18. # defer.returnValue(item)
  19. # def process_item(self, item, spider):
  20. def _do_calculation(self, item, out):
  21. # 使用twisted将mysql插入变成异步执行
  22. logger = logging.getLogger(__name__)
  23. # 联赛id
  24. league_id = item['league_id']
  25. # 联赛名
  26. league_name = item['league_name']
  27. # 比赛id
  28. match_id = item['game_id']
  29. # 球队1
  30. team_home = item['team_home']
  31. # 球队2
  32. team_guest = item['team_guest']
  33. # 数量(97>)
  34. tag_number = item['number']
  35. # 比赛时间
  36. time_game = str(item['time_game'])
  37. # 现在时间,时间戳
  38. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() - 43200))
  39. cdate = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  40. match_date = cdate.split(' ')[0]
  41. match_time = cdate.split(' ')[1]
  42. # 队1分数
  43. score_home = item['score_home']
  44. # 队2分数
  45. score_guest = item['score_guest']
  46. # 第几节
  47. jijie = item['jijie']
  48. pt = 4
  49. # 滚球这个位置获取不到这些字段
  50. # match_date, match_time = new_time(ctime)
  51. # 让球
  52. concedes_dict = item['concedes_dict']
  53. concedes_dict_rule = item['concedes_dict_rule']
  54. # 独赢
  55. capots_dict = item['capots_dict']
  56. # 总得分大小
  57. total_size_dict = item['total_size_dict']
  58. total_size_dict_rule = item['total_size_dict_rule']
  59. # 总得分单双
  60. odd_evens_dict = item['odd_evens_dict']
  61. odd_evens_dict_rule = item['odd_evens_dict_rule']
  62. # 具体比分
  63. source_dict = item['source_dict']
  64. uuid = Helper.genearte_uuid(league_name)
  65. """联赛"""
  66. last_time = '{}-12-31 23:59:59'.format(datetime.datetime.now().year)
  67. if self.db.bq_league35.find({'lg_id': league_id}).count() < 1:
  68. league_dict = {"game_code": "bq", "title": "league", "source": "hg3535"}
  69. league_list = []
  70. league_key = ["name_chinese", "kind", "match_mode", "if_stop", "last_time", "lg_id", "source", "uuid"]
  71. league_value = [league_name, "1", "1", "0", last_time, league_id, "hg3535", uuid]
  72. league_data = dict(zip(league_key, league_value))
  73. league_list.append(league_data)
  74. league_dict['data'] = league_list
  75. res = Helper.async_post('{}/setLeague'.format(POST_URL), league_dict)
  76. if res:
  77. if res.get('status') == 1:
  78. self.db.bq_league35.insert(league_data)
  79. logger.info('棒球滚球联赛提交成功, {}'.format(res))
  80. else:
  81. logger.warning('棒球滚球联赛提交失败, {}, {}'.format(res, league_dict))
  82. else:
  83. logger.warning('棒球滚球联赛,接口异常,提交失败, {}, {}'.format(res, league_dict))
  84. else:
  85. logger.info('棒球滚球联赛, 已存在不提交')
  86. """赛事"""
  87. match_identity = Helper.genearte_uuid(team_home + team_guest + match_date)
  88. if self.db.bq_competition35.find({"match_id": match_id, 'is_rollball': 1}).count() < 1:
  89. match_list = []
  90. match_dict = {"game_code": "bq", "title": "match", "source": "hg3535"}
  91. match_key = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  92. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  93. "half_match_id", "is_today", "is_horn", 'match_identity']
  94. match_value = [team_home, team_guest, league_id, 1, match_id, match_date, match_time, tag_number,
  95. "hg3535", 1, 0, 0, utime, uuid, 0, 0, 0, match_identity]
  96. match_data = dict(zip(match_key, match_value))
  97. match_list.append(match_data)
  98. match_dict['data'] = match_list
  99. res = Helper.async_post('{}/setMatch'.format(POST_URL), match_dict)
  100. if res:
  101. if res.get('status') == 1:
  102. self.db.bq_competition35.insert(match_data)
  103. else:
  104. logger.warning('棒球滚球赛事提交失败, {}, {}'.format(res, match_dict))
  105. else:
  106. logger.warning('棒球滚球赛事,接口异常,提交失败, {}, {}'.format(res, match_dict))
  107. else:
  108. logger.info("棒球赛事已存在,棒球赛事状态时间未改变,不做修改")
  109. """赔率"""
  110. data_list = []
  111. odds_onlys = []
  112. # 让球
  113. if concedes_dict:
  114. for key, value in concedes_dict.items():
  115. if value:
  116. sole_str = 'CO' + str(key) + '0' + str(match_id) + 'hg3535'
  117. sole = Helper.genearte_MD5(sole_str, pt)
  118. odds_str = 'CO' + str(key) + '0' + str(concedes_dict_rule[key]) + str(
  119. value) + 'hg3535' + str(match_id)
  120. odds_only = Helper.genearte_MD5(odds_str, pt)
  121. condition = concedes_dict_rule[key]
  122. concedes_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  123. "condition", "odds_only", "sole", "source", "type", "team"]
  124. concedes_value = [match_id, league_id, key, "0", 0, 'CO', value, condition,
  125. odds_only, sole, "hg3535", "0", ""]
  126. concedes_data = dict(zip(concedes_key, concedes_value))
  127. data_list.append(concedes_data)
  128. # 独赢
  129. if capots_dict:
  130. for key, value in capots_dict.items():
  131. if value:
  132. sole_str = 'C' + str(key) + '0' + str(match_id) + 'hg3535'
  133. sole = Helper.genearte_MD5(sole_str, pt)
  134. odds_str = 'C' + str(key) + '0' + '0' + str(value) + 'hg3535' + str(match_id)
  135. odds_only = Helper.genearte_MD5(odds_str, pt)
  136. capots_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  137. "condition", "odds_only", "sole", "source", "type", "team"]
  138. capots_value = [match_id, league_id, key, "0", 0, 'C', value, '',
  139. odds_only, sole, "hg3535", "0", ""]
  140. capots_data = dict(zip(capots_key, capots_value))
  141. data_list.append(capots_data)
  142. # 总得分大小
  143. if total_size_dict:
  144. for key, value in total_size_dict.items():
  145. if value:
  146. sole_str = 'TN' + str(key) + '0' + str(match_id) + 'hg3535'
  147. sole = Helper.genearte_MD5(sole_str, pt)
  148. odds_str = 'TN' + str(key) + '0' + str(total_size_dict_rule[key]) + str(
  149. value) + 'hg3535' + str(match_id)
  150. odds_only = Helper.genearte_MD5(odds_str, pt)
  151. condition = total_size_dict_rule[key]
  152. total_size_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  153. "condition", "odds_only", "sole", "source", "type", "team"]
  154. total_size_value = [match_id, league_id, key, "0", 0, 'TN', value, condition,
  155. odds_only, sole, "hg3535", "0", ""]
  156. total_size_data = dict(zip(total_size_key, total_size_value))
  157. data_list.append(total_size_data)
  158. # 总得分单双
  159. if odd_evens_dict:
  160. for key, value in odd_evens_dict.items():
  161. sole_str = 'TS' + str(key) + '0' + str(match_id) + 'hg3535'
  162. sole = Helper.genearte_MD5(sole_str, pt)
  163. odds_str = 'TS' + str(key) + '0' + str(odd_evens_dict_rule[key]) + str(value) + 'hg3535' + str(
  164. match_id)
  165. odds_only = Helper.genearte_MD5(odds_str, pt)
  166. condition = odd_evens_dict_rule[key]
  167. odd_evens_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  168. "condition", "odds_only", "sole", "source", "type", "team"]
  169. odd_evens_value = [match_id, league_id, key, "0", 0, 'TS', value, condition,
  170. odds_only, sole, "hg3535", "0", ""]
  171. odd_evens_data = dict(zip(odd_evens_key, odd_evens_value))
  172. data_list.append(odd_evens_data)
  173. odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid",
  174. "is_stringscene", "utime", "pt", 'match_identity']
  175. odds_value = ["bq", "odds", match_id, league_id, data_list, "hg3535", odds_onlys, tag_number, uuid,
  176. 0, cdate, pt, match_identity]
  177. odds_dict = dict(zip(odds_key, odds_value))
  178. if data_list:
  179. res = Helper.async_post('{}/setOdds'.format(POST_URL), odds_dict)
  180. if res:
  181. if res.get('status') == 1:
  182. logger.info('棒球滚球赔率提交成功, {}'.format(res))
  183. else:
  184. logger.warning('棒球滚球赔率提交失败, {}, {}'.format(res, odds_dict))
  185. else:
  186. logger.warning('棒球滚球赔率接口异常,提交失败, {}, {}'.format(res, odds_dict))
  187. else:
  188. logger.info('棒球滚球详细赔率列表为空, 不提交')
  189. """赛事结果"""
  190. da_list = []
  191. bq_rball = {
  192. "lg_id": league_id, "home_team": team_home, "guest_team": team_guest, "home_rate": '', "guest_rate": '',
  193. "home_score": score_home, "guest_score": score_guest, "status": 1, "first_score": '',
  194. "last_score": '', "all_inning": '', 'match_identity': match_identity,
  195. "match_score": '', "match_winer": '', "updata_time": '',
  196. "match_time": time_game, "match_process": jijie,
  197. "tag": tag_number, "match_id": match_id, "source": 'hg3535',
  198. "uuid": uuid, "result_mark": source_dict, "start_time": '', 'warn_more': '',
  199. }
  200. da_list.append(bq_rball)
  201. r_data_dict = {
  202. "game_code": "bq",
  203. "title": "match_result_r",
  204. "source": "hg3535",
  205. "data": da_list,
  206. }
  207. if da_list:
  208. try:
  209. res = Helper.async_post('{}/setMatchResult'.format(POST_URL), r_data_dict)
  210. if res.get('status') == 1:
  211. logger.info('棒球滚球结果记录提交成功, {}'.format(res))
  212. self.db.bq_match_result35.insert(bq_rball)
  213. else:
  214. logger.warning('棒球滚球结果记录提交失败, {}, {}'.format(res, r_data_dict))
  215. except Exception as e:
  216. logger.warning("滚球接口数据异常, 提交失败, {}, {}".format(e, r_data_dict))
  217. else:
  218. logger.info('棒球滚球结果记录为空, 不提交')
  219. reactor.callFromThread(out.callback, item)