zhibo.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # encoding: utf-8
  2. import pymongo
  3. import time
  4. import logging
  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, MATCHWARN
  8. # 滚球足球 插入
  9. class Zuqiupipeline(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. 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. # defer.returnValue(item)
  19. # def process_item(self, item, spider):
  20. def _do_calculation(self, item, out):
  21. logger = logging.getLogger(__name__)
  22. detail_datas = item['detail']
  23. mid = item['mid']
  24. date = item['date']
  25. home = item['home']
  26. away = item['away']
  27. us_date = time.strftime('%Y-%m-%d', time.localtime(date))
  28. match_identity = Helper.genearte_uuid(home + away + us_date)
  29. warns = []
  30. up_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  31. warn_dict = {"game_code": "zq", "title": "match_warn", "source": "hg3535", "match_id": mid, 'up_time': up_time, 'match_identity': match_identity}
  32. for detail_data in detail_datas:
  33. team_name, event_name, event_uts, warn_type = detail_data
  34. find_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(event_uts))
  35. detail_dict = {"find_time": find_time, "warn_name": event_name, 'warn_type': warn_type}
  36. warns.append(detail_dict)
  37. if warns:
  38. warn_dict['data'] = warns
  39. res = Helper.async_post(MATCHWARN, warn_dict)
  40. if res.get('status') == 1:
  41. # print('危险球提交成功')
  42. self.db.FT_warn35.insert(warn_dict)
  43. logger.info('足球直播危险球提交成功, {}'.format(res))
  44. else:
  45. logger.warning('足球直播危险球,提交失败, {}'.format(res))
  46. else:
  47. logger.info('足球直播危险球列表为空, 不提交')
  48. reactor.callFromThread(out.callback, item)