zuqiu.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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
  9. class ZuqiuPipeline(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. pt = str(item['index'])
  27. team_h, team_c = match_all['team_h'], match_all['team_c']
  28. league_name, league_id = match_all['league'], match_all['gidm']
  29. us_time, re_time = match_all['datetime'], match_all['re_time']
  30. match_id = match_all['gid']
  31. tag_number = item['tag']
  32. uuid = Helper.genearte_uuid(league_name)
  33. league_list = []
  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. # if self.db.zq_league35.find({'uuid': uuid}).count() < 1:
  39. league_dict = {"game_code": "zq", "title": "league", "source": "hgg070"}
  40. league_key = ["name_chinese", "kind", "match_mode", "if_stop", "last_time", "lg_id", "source", "uuid"]
  41. league_value = [league_name, "1", "1", "0", last_time, league_id, "hgg070", uuid]
  42. league_data = dict(zip(league_key, league_value))
  43. league_list.append(league_data)
  44. league_dict['data'] = league_list
  45. res = Helper.async_post(LEAGUE_URL, league_dict)
  46. if res:
  47. if res.get('status') == 1:
  48. self.db.zq_league070.insert(league_data)
  49. logging.info('足球联赛提交, {}'.format(res))
  50. else:
  51. logging.warning('足球联赛接口异常, {}'.format(res))
  52. else:
  53. logging.info('{},联赛已存在, 不提交'.format(league_name))
  54. pt_dict = {'0': 'is_today', '1': 'is_morningplate', '2': 'is_stringscene', '3': 'is_rollball'}
  55. pt_status = pt_dict[pt]
  56. ris_stringscene = 0
  57. json_key = 'zuqiu'
  58. if pt == '0':
  59. is_rollball = 0
  60. is_today = 1
  61. is_morningplate = 0
  62. is_stringscene = 0
  63. elif pt == '2':
  64. is_rollball = 0
  65. is_today = 0
  66. is_morningplate = 1
  67. is_stringscene = 0
  68. else:
  69. json_key = 'chuanchang'
  70. is_today = 0
  71. is_rollball = 0
  72. is_morningplate = 0
  73. is_stringscene = 1
  74. ris_stringscene = 1
  75. match_list = []
  76. match_identity = Helper.genearte_uuid(team_h + team_c + match_date)
  77. if self.db.zq_competition070.find({'match_id': match_id, pt_status: 1}).count() < 1:
  78. # if self.db.zq_competition35.find({'match_identity': match_identity, pt_status: 1}).count() < 1:
  79. match_dict = {"game_code": "zq", "title": "match", "source": "hgg070"}
  80. match_kay = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  81. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  82. "half_match_id", "is_today", "is_horn", 'match_identity']
  83. match_value = [team_h, team_c, league_id, 0, match_id, match_date, match_time, tag_number,
  84. "hgg070", is_rollball, is_morningplate, is_stringscene, us_time, uuid, 0, is_today, 0,
  85. match_identity]
  86. match_data = dict(zip(match_kay, match_value))
  87. match_list.append(match_data)
  88. match_dict['data'] = match_list
  89. res = Helper.async_post(MATCH_URL, match_dict)
  90. if res:
  91. if res.get('status') == 1:
  92. self.db.zq_competition070.insert(match_data)
  93. logging.info('足球赛事提交, {}'.format(res))
  94. else:
  95. logger.warning('足球赛事表提交失败, {}'.format(res))
  96. # logger.warning(match_dict)
  97. else:
  98. logger.warning('足球赛事接口异常提交失败, {}'.format(res))
  99. # logger.warning(match_dict)
  100. else:
  101. logger.info('足球赛事已存在,不提交')
  102. data_list = []
  103. for i in self.hgg070[json_key]:
  104. r_code = i['prodds']
  105. p_code = i['plodds']
  106. if match_all.get(r_code) == 'Y':
  107. for y in i['items']:
  108. odd = match_all.get(y['rodds'])
  109. if odd:
  110. code = y['lodds']
  111. sole_str = p_code + code + '0' + str(match_id) + 'hgg070'
  112. sole = Helper.genearte_MD5(sole_str, pt)
  113. ratio_name = y['ratio_name']
  114. if ratio_name:
  115. condition = match_all[ratio_name]
  116. else:
  117. condition = y['latio']
  118. hash_str = p_code + code + '0' + str(odd) + "hgg070" + str(match_id)
  119. odds_only = Helper.genearte_MD5(hash_str, pt)
  120. odd_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds",
  121. "condition", "odds_only", "sole", "source", "type", "team"]
  122. odd_value = [match_id, league_id, code, '0', '0', code, odd, condition,
  123. odds_only, sole, 'hgg070', '0', '']
  124. odd_dict = dict(zip(odd_key, odd_value))
  125. data_list.append(odd_dict)
  126. odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid",
  127. "is_stringscene", "utime", "pt", 'match_identity']
  128. odds_value = ["zq", "odds", match_id, league_id, data_list, "hgg070", '', tag_number, uuid,
  129. ris_stringscene, utime, pt, match_identity]
  130. odds_dict = dict(zip(odds_key, odds_value))
  131. if data_list:
  132. res = Helper.async_post(ODDS_URL, odds_dict)
  133. if res:
  134. if res.get('status') == 1:
  135. logger.info('足球详细赔率提交成功, {}'.format(res))
  136. # logger.info(odds_dict)
  137. else:
  138. logger.warning('足球详细赔率提交失败, {}'.format(res))
  139. # logger.warning(odds_dict)
  140. else:
  141. logging.warning('足球详细赔率接口异常, {}'.format(res))
  142. else:
  143. logger.info('足球详细赔率列表为空')
  144. reactor.callFromThread(out.callback, item)