zuqiu.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import datetime
  2. import time
  3. import logging
  4. import pymongo
  5. from twisted.internet import defer, reactor
  6. from ..utils.helper import Helper
  7. from ..settings import M_HOST, M_USER, M_PASSWORD, M_POST, M_DB, LEAGUE_URL, ODDS_URL, MATCH_URL
  8. class ZuqiuPipeline(object):
  9. def open_spider(self, spider):
  10. self.mongo = pymongo.MongoClient(host=M_HOST, username=M_USER, password=M_PASSWORD, port=M_POST,
  11. authSource=M_DB)
  12. self.db = self.mongo[M_DB]
  13. # @defer.inlineCallbacks
  14. # def process_item(self,item,spider):
  15. # out=defer.Deferred()
  16. # reactor.callInThread(self._do_calculation,item,out)
  17. # yield out
  18. # def _do_calculation(self,item,out):
  19. # pass
  20. def process_item(self, item, spider):
  21. logger = logging.getLogger(__name__)
  22. match_all = item['data']
  23. pt = str(item['index'])
  24. team_h, team_c = match_all['team_h'], match_all['team_c']
  25. league_name, league_id = match_all['league'], match_all['gidm']
  26. us_time, re_time = match_all['datetime'], match_all['re_time']
  27. match_id = match_all['gid']
  28. tag_number = item['tag']
  29. uuid = Helper.genearte_uuid(league_name)
  30. league_list = []
  31. last_time = '{}-12-31 23:59:59'.format(datetime.datetime.now().year)
  32. match_date, match_time, time3 = Helper.change_time(us_time)
  33. if self.db.zq_league070.find({'lg_id': league_id}).count() < 1:
  34. # if self.db.zq_league35.find({'uuid': uuid}).count() < 1:
  35. league_dict = {"game_code": "zq", "title": "league", "source": "hgg070"}
  36. league_key = ["name_chinese", "kind", "match_mode", "if_stop", "last_time", "lg_id", "source", "uuid"]
  37. league_value = [league_name, "1", "1", "0", last_time, league_id, "hgg070", uuid]
  38. league_data = dict(zip(league_key, league_value))
  39. league_list.append(league_data)
  40. league_dict['data'] = league_list
  41. res = Helper.async_post(LEAGUE_URL, league_dict)
  42. if res:
  43. if res.get('status') == 1:
  44. self.db.zq_league070.insert(league_data)
  45. logging.info('足球联赛提交, {}'.format(res))
  46. else:
  47. logging.warning('足球联赛接口异常, {}'.format(res))
  48. else:
  49. logging.info('{},联赛已存在, 不提交'.format(league_name))
  50. pt_dict = {'0': 'is_today', '1': 'is_morningplate', '2': 'is_stringscene', '3': 'is_rollball'}
  51. pt_status = pt_dict[pt]
  52. if pt == '0':
  53. is_rollball = 0
  54. is_today = 1
  55. is_morningplate = 0
  56. is_stringscene = 0
  57. elif pt == '2':
  58. is_rollball = 0
  59. is_today = 0
  60. is_morningplate = 1
  61. is_stringscene = 0
  62. else:
  63. is_today = 0
  64. is_rollball = 0
  65. is_morningplate = 0
  66. is_stringscene = 1
  67. match_list = []
  68. match_identity = Helper.genearte_uuid(team_h + team_c + match_date)
  69. if self.db.zq_competition070.find({'match_id': match_id, pt_status: 1}).count() < 1:
  70. # if self.db.zq_competition35.find({'match_identity': match_identity, pt_status: 1}).count() < 1:
  71. match_dict = {"game_code": "zq", "title": "match", "source": "hgg070"}
  72. match_kay = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time",
  73. "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid",
  74. "half_match_id", "is_today", "is_horn", 'match_identity']
  75. match_value = [team_h, team_c, league_id, 0, match_id, match_date, match_time, tag_number,
  76. "hg3535", is_rollball, is_morningplate, is_stringscene, us_time, uuid, 0, is_today, 0,
  77. match_identity]
  78. match_data = dict(zip(match_kay, match_value))
  79. match_list.append(match_data)
  80. match_dict['data'] = match_list
  81. res = Helper.async_post(MATCH_URL, match_dict)
  82. if res:
  83. if res.get('status') == 1:
  84. self.db.zq_competition070.insert(match_data)
  85. logging.info('足球赛事提交, {}'.format(res))
  86. else:
  87. logger.warning('足球赛事表提交失败, {}'.format(res))
  88. # logger.warning(match_dict)
  89. else:
  90. logger.warning('足球赛事接口异常提交失败, {}'.format(res))
  91. # logger.warning(match_dict)
  92. else:
  93. logger.info('足球赛事已存在,不提交')
  94. # reactor.callFromThread(out.callback, item)