zhibo.py 2.2 KB

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