wangqiu.py 7.2 KB

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