zhibo.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. date = item['date']
  24. home = item['home']
  25. away = item['away']
  26. us_date = time.strftime('%Y-%m-%d', time.localtime(date))
  27. match_identity = Helper.genearte_uuid(home + away + us_date)
  28. warns = []
  29. up_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  30. warn_dict = {"game_code": "zq", "title": "match_warn", "source": "hg3535", "match_id": mid, 'up_time': up_time, 'match_identity': match_identity}
  31. for detail_data in detail_datas:
  32. team_name, event_name, event_uts, warn_type = detail_data
  33. find_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(event_uts))
  34. detail_dict = {"find_time": find_time, "warn_name": event_name, 'warn_type': warn_type}
  35. warns.append(detail_dict)
  36. if warns:
  37. warn_dict['data'] = warns
  38. res = Helper.async_post(MATCHWARN, warn_dict)
  39. if res.get('status') == 1:
  40. # print('危险球提交成功')
  41. self.db.FT_warn35.insert(warn_dict)
  42. logger.info('足球直播危险球提交成功, {}'.format(res))
  43. else:
  44. logger.warning('足球直播危险球,提交失败, {}'.format(res))
  45. else:
  46. logger.info('足球直播危险球列表为空, 不提交')
  47. reactor.callFromThread(out.callback, item)