roll_zuqiu.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. ptype_id = match_all.get('ptype_id')
  57. is_horn = 0
  58. if ptype_id == '146':
  59. ptype = match_all['ptype']
  60. team_h = team_h.replace(ptype, '').strip()
  61. team_c = team_c.replace(ptype, '').strip()
  62. is_horn = 1
  63. if self.db.zq_competition070.find({'match_id': match_id, 'is_rollball': 1}).count() < 1:
  64. match_dict = {"game_code": "zq", "title": "match", "source": "hgg070"}
  65. match_kay = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  66. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  67. "half_match_id", "is_today", "is_horn", 'match_identity']
  68. match_value = [team_h, team_c, league_id, 0, match_id, match_date, match_time, tag_number,
  69. "hgg070", 1, 0, 0, us_time, uuid, 0, 0, is_horn, match_identity]
  70. match_data = dict(zip(match_kay, match_value))
  71. match_list.append(match_data)
  72. match_dict['data'] = match_list
  73. res = Helper.async_post(MATCH_URL, match_dict)
  74. if res:
  75. if res.get('status') == 1:
  76. self.db.zq_competition070.insert(match_data)
  77. logging.info('足球赛事提交, {}'.format(res))
  78. else:
  79. logger.warning('足球赛事表提交失败, {}'.format(res))
  80. # logger.warning(match_dict)
  81. else:
  82. logger.warning('足球赛事接口异常提交失败, {}'.format(res))
  83. # logger.warning(match_dict)
  84. else:
  85. logger.info('足球赛事已存在,不提交')
  86. data_list = []
  87. for i in self.hgg070[json_key]:
  88. r_code = i['prodds']
  89. p_code = i['plodds']
  90. if match_all.get(r_code) == 'Y':
  91. for y in i['items']:
  92. odd = match_all.get(y['rodds'])
  93. if odd:
  94. code = y['lodds']
  95. sole_str = p_code + code + '0' + str(match_id) + 'hgg070'
  96. sole = Helper.genearte_MD5(sole_str, pt)
  97. ratio_name = y['ratio_name']
  98. if ratio_name:
  99. condition = match_all[ratio_name]
  100. else:
  101. condition = y['latio']
  102. hash_str = p_code + code + '0' + str(odd) + "hgg070" + str(match_id)
  103. odds_only = Helper.genearte_MD5(hash_str, pt)
  104. odd_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  105. "condition", "odds_only", "sole", "source", "type", "team"]
  106. odd_value = [match_id, league_id, code, '0', '0', code, odd, condition,
  107. odds_only, sole, 'hgg070', '0', '']
  108. odd_dict = dict(zip(odd_key, odd_value))
  109. data_list.append(odd_dict)
  110. odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid",
  111. "is_stringscene", "utime", "pt", 'match_identity']
  112. odds_value = ["zq", "odds", match_id, league_id, data_list, "hgg070", '', tag_number, uuid,
  113. 0, utime, pt, match_identity]
  114. odds_dict = dict(zip(odds_key, odds_value))
  115. if data_list:
  116. res = Helper.async_post(ODDS_URL, odds_dict)
  117. if res:
  118. if res.get('status') == 1:
  119. logger.info('足球详细赔率提交成功, {}'.format(res))
  120. # logger.info(odds_dict)
  121. else:
  122. logger.warning('足球详细赔率提交失败, {}'.format(res))
  123. # logger.warning(odds_dict)
  124. else:
  125. logging.warning('足球详细赔率接口异常, {}'.format(res))
  126. else:
  127. logger.info('足球详细赔率列表为空')
  128. data_result = []
  129. process = {"HT": "半场", "1H": "上半场", "2H": "下半场"}
  130. match_process = process[re_time.split('^')[0]]
  131. time_game = re_time.split('^')[1][:-1]
  132. score_h = match_all.get('score_h')
  133. score_c = match_all.get('score_c')
  134. # all_goal = score_h + score_c
  135. # match_score = '{}:{}'.format(score_h, score_c)
  136. zq_rball = {"home_team": team_h, "guest_team": team_c,
  137. "lg_id": league_id, "home_rate": 0,
  138. "guest_rate": 0, "home_score": score_h,
  139. "guest_score": score_c, "all_goal": '', "status": 1,
  140. "first_score": "", "last_score": "", "match_score": '', "uuid": uuid,
  141. "match_winer": "", "match_time": time_game, 'match_identity': match_identity,
  142. "match_process": match_process, "tag": tag_number,
  143. "match_id": match_id, "p_code": ""}
  144. data_result.append(zq_rball)
  145. r_data_dict = {
  146. "game_code": "zq",
  147. "title": "match_result_r",
  148. "source": "hgg070",
  149. "data": data_result
  150. }
  151. if data_result:
  152. try:
  153. res = Helper.async_post(MATCH_RESULT, r_data_dict)
  154. if res:
  155. if res.get('status') == 1:
  156. logger.info('足球滚球结果记录提交成功, {}'.format(res))
  157. self.db.zq_match_result35.insert(zq_rball)
  158. # logger.info(r_data_dict)
  159. else:
  160. logger.warning('足球滚球结果记录提交失败, {}'.format(res))
  161. # logger.warning(r_data_dict)
  162. else:
  163. logger.warning('足球滚球结果记录接口异常,提交失败, {}'.format(res))
  164. # logger.warning(r_data_dict)
  165. except Exception as e:
  166. logger.warning('滚球数据接口异常,提交失败, {}'.format(e))
  167. # reactor.callFromThread(out.callback, item)