roll_bangqiu.py 12 KB

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