zhibo.py 1.7 KB

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