zhibo.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import pymongo
  2. import time
  3. import logging
  4. from twisted.internet import defer, reactor
  5. from ..utils.helper import Helper
  6. from ..settings import M_HOST, M_USER, M_PASSWORD, M_POST, M_DB, MATCHWARN
  7. # 滚球足球 插入
  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. self.db = self.mongo[M_DB]
  12. @defer.inlineCallbacks
  13. def process_item(self, item, spider):
  14. out = defer.Deferred()
  15. reactor.callInThread(self._do_calculation, item, out)
  16. yield out
  17. defer.returnValue(item)
  18. # def process_item(self, item, spider):
  19. def _do_calculation(self, item, out):
  20. logger = logging.getLogger(__name__)
  21. detail_datas = item['detail']
  22. mid = item['mid']
  23. warns = []
  24. up_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  25. warn_dict = {"game_code": "zq", "title": "match_warn", "source": "hg3535", "match_id": mid, 'up_time': up_time}
  26. for detail_data in detail_datas:
  27. team_name, event_name, event_uts = detail_data
  28. find_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(event_uts))
  29. detail_dict = {"find_time": find_time, "warn_name": event_name}
  30. warns.append(detail_dict)
  31. if warns:
  32. warn_dict['data'] = warns
  33. res = Helper.async_post(MATCHWARN, warn_dict)
  34. if res.get('status') == 1:
  35. print('危险球提交成功')
  36. self.db.FT_warn35.insert(warn_dict)
  37. logger.info('足球直播危险球提交成功, {}'.format(res))
  38. else:
  39. logger.warning('足球直播危险球,提交失败, {}'.format(res))
  40. else:
  41. logger.info('足球直播危险球列表为空, 不提交')
  42. reactor.callFromThread(out.callback, item)