roll_zuqiu.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import datetime
  2. import json
  3. import time
  4. import logging
  5. import pymongo
  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, LEAGUE_URL, ODDS_URL, MATCH_URL, MATCH_RESULT
  9. class RollPipeline(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=M_DB)
  13. self.db = self.mongo[M_DB]
  14. with open('./conf/zuqiu.json', 'r', encoding='utf8') as zq:
  15. hgg070 = json.load(zq)
  16. self.hgg070 = hgg070
  17. # @defer.inlineCallbacks
  18. # def process_item(self,item,spider):
  19. # out=defer.Deferred()
  20. # reactor.callInThread(self._do_calculation,item,out)
  21. # yield out
  22. # def _do_calculation(self,item,out):
  23. def process_item(self, item, spider):
  24. logger = logging.getLogger(__name__)
  25. match_all = item['data']
  26. team_h, team_c = match_all['team_h'], match_all['team_c']
  27. league_name, league_id = match_all['league'], match_all['gidm']
  28. us_time, re_time = match_all['datetime'], match_all['re_time']
  29. match_id = match_all['gid']
  30. tag_number = item['tag']
  31. uuid = Helper.genearte_uuid(league_name)
  32. league_list = []
  33. pt = 4
  34. last_time = '{}-12-31 23:59:59'.format(datetime.datetime.now().year)
  35. match_date, match_time, time3 = Helper.change_time(us_time)
  36. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  37. if self.db.zq_league070.find({'lg_id': league_id}).count() < 1:
  38. league_dict = {"game_code": "zq", "title": "league", "source": "hgg070"}
  39. league_key = ["name_chinese", "kind", "match_mode", "if_stop", "last_time", "lg_id", "source", "uuid"]
  40. league_value = [league_name, "1", "1", "0", last_time, league_id, "hgg070", uuid]
  41. league_data = dict(zip(league_key, league_value))
  42. league_list.append(league_data)
  43. league_dict['data'] = league_list
  44. res = Helper.async_post(LEAGUE_URL, league_dict)
  45. if res:
  46. if res.get('status') == 1:
  47. self.db.zq_league070.insert(league_data)
  48. logging.info('足球联赛提交, {}'.format(res))
  49. else:
  50. logging.warning('足球联赛接口异常, {}'.format(res))
  51. else:
  52. logging.info('{},联赛已存在, 不提交'.format(league_name))
  53. json_key = 'zq_rollball'
  54. match_list = []
  55. match_identity = Helper.genearte_uuid(team_h + team_c + match_date)
  56. if self.db.zq_competition070.find({'match_id': match_id, 'is_rollball': 1}).count() < 1:
  57. match_dict = {"game_code": "zq", "title": "match", "source": "hgg070"}
  58. match_kay = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  59. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  60. "half_match_id", "is_today", "is_horn", 'match_identity']
  61. match_value = [team_h, team_c, league_id, 0, match_id, match_date, match_time, tag_number,
  62. "hgg070", 1, 0, 0, us_time, uuid, 0, 0, 0,
  63. match_identity]
  64. match_data = dict(zip(match_kay, match_value))
  65. match_list.append(match_data)
  66. match_dict['data'] = match_list
  67. res = Helper.async_post(MATCH_URL, match_dict)
  68. if res:
  69. if res.get('status') == 1:
  70. self.db.zq_competition070.insert(match_data)
  71. logging.info('足球赛事提交, {}'.format(res))
  72. else:
  73. logger.warning('足球赛事表提交失败, {}'.format(res))
  74. # logger.warning(match_dict)
  75. else:
  76. logger.warning('足球赛事接口异常提交失败, {}'.format(res))
  77. # logger.warning(match_dict)
  78. else:
  79. logger.info('足球赛事已存在,不提交')
  80. data_list = []
  81. for i in self.hgg070[json_key]:
  82. r_code = i['prodds']
  83. p_code = i['plodds']
  84. if match_all.get(r_code) == 'Y':
  85. for y in i['items']:
  86. odd = match_all.get(y['rodds'])
  87. if odd:
  88. code = y['lodds']
  89. sole_str = p_code + code + '0' + str(match_id) + 'hgg070'
  90. sole = Helper.genearte_MD5(sole_str, pt)
  91. ratio_name = y['ratio_name']
  92. if ratio_name:
  93. condition = match_all[ratio_name]
  94. else:
  95. condition = y['latio']
  96. hash_str = p_code + code + '0' + str(odd) + "hgg070" + str(match_id)
  97. odds_only = Helper.genearte_MD5(hash_str, pt)
  98. odd_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  99. "condition", "odds_only", "sole", "source", "type", "team"]
  100. odd_value = [match_id, league_id, code, '0', '0', code, odd, condition,
  101. odds_only, sole, 'hgg070', '0', '']
  102. odd_dict = dict(zip(odd_key, odd_value))
  103. data_list.append(odd_dict)
  104. odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid",
  105. "is_stringscene", "utime", "pt", 'match_identity']
  106. odds_value = ["zq", "odds", match_id, league_id, data_list, "hgg070", '', tag_number, uuid,
  107. 0, utime, pt, match_identity]
  108. odds_dict = dict(zip(odds_key, odds_value))
  109. if data_list:
  110. res = Helper.async_post(ODDS_URL, odds_dict)
  111. if res:
  112. if res.get('status') == 1:
  113. logger.info('足球详细赔率提交成功, {}'.format(res))
  114. # logger.info(odds_dict)
  115. else:
  116. logger.warning('足球详细赔率提交失败, {}'.format(res))
  117. # logger.warning(odds_dict)
  118. else:
  119. logging.warning('足球详细赔率接口异常, {}'.format(res))
  120. else:
  121. logger.info('足球详细赔率列表为空')
  122. data_list = []
  123. zq_rball = {"home_team": team_h, "guest_team": team_c,
  124. "lg_id": league_id, "home_rate": 0,
  125. "guest_rate": 0, "home_score": score_home,
  126. "guest_score": score_guest, "all_goal": all_goal, "status": 1,
  127. "first_score": "", "last_score": "", "match_score": match_score, "uuid": uuid,
  128. "match_winer": "", "match_time": time_game, 'match_identity': match_identity,
  129. "match_process": half_way, "tag": number,
  130. "match_id": match_id, "p_code": ""}
  131. data_list.append(zq_rball)
  132. r_data_dict = {
  133. "game_code": "zq",
  134. "title": "match_result_r",
  135. "source": "hg3535",
  136. "data": data_list
  137. }
  138. if data_list:
  139. try:
  140. res = Helper.async_post(MATCH_RESULT, r_data_dict)
  141. if res:
  142. if res.get('status') == 1:
  143. logger.info('足球滚球结果记录提交成功, {}'.format(res))
  144. self.db.zq_match_result35.insert(zq_rball)
  145. # logger.info(r_data_dict)
  146. else:
  147. logger.warning('足球滚球结果记录提交失败, {}'.format(res))
  148. # logger.warning(r_data_dict)
  149. else:
  150. logger.warning('足球滚球结果记录接口异常,提交失败, {}'.format(res))
  151. # logger.warning(r_data_dict)
  152. except Exception as e:
  153. logger.warning('滚球数据接口异常,提交失败, {}'.format(e))
  154. # reactor.callFromThread(out.callback, item)