jieshu.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # import datetime
  2. import pymongo
  3. # import time
  4. import logging
  5. from twisted.internet import defer, reactor
  6. import redis
  7. from ..settings import M_HOST, M_USER, M_PASSWORD, M_POST, M_DB, MATCH_STATUS
  8. from ..utils.helper import Helper
  9. from ..settings import R_HOST, R_PASSWORD, R_POST, R_DB
  10. # 滚球足球 插入
  11. class Jieshuqiupipeline(object):
  12. def open_spider(self, spider):
  13. self.mongo = pymongo.MongoClient(host=M_HOST, username=M_USER, password=M_PASSWORD, port=M_POST)
  14. self.db = self.mongo[M_DB]
  15. self.rls = redis.Redis(host=R_HOST, port=R_POST, db=R_DB, password=R_PASSWORD)
  16. @defer.inlineCallbacks
  17. def process_item(self, item, spider):
  18. out = defer.Deferred()
  19. reactor.callInThread(self._do_calculation, item, out)
  20. yield out
  21. defer.returnValue(item)
  22. def _do_calculation(self, item, out):
  23. # def process_item(self, item, spider):
  24. logger = logging.getLogger(__name__)
  25. ball = item['ball']
  26. match_id = item['match_id']
  27. # status = item['status']
  28. if ball == '足球':
  29. game_code = "zq"
  30. elif ball == '篮球':
  31. game_code = "lq"
  32. elif ball == '网球':
  33. game_code = "wq"
  34. else:
  35. game_code = "bq"
  36. status_dict = {"game_code": game_code, "title": "match_status", "source": "hg3535"}
  37. data_list = []
  38. data = {'match_id': match_id, 'status': 2, "is_rollball": 0, "is_today": 0, "is_morningplate": 0, "is_stringscene": 0, "is_horn": 0}
  39. data_list.append(data)
  40. status_dict['data'] = data_list
  41. res = Helper.async_post(MATCH_STATUS, status_dict)
  42. if res:
  43. if "成功" in res:
  44. self.db.match_status35.insert(data)
  45. self.rls.srem('hg3535.gunqiu.ids', match_id)
  46. logger.info('{},赛事结果状态交成功, {}'.format(game_code, res))
  47. logger.info(status_dict)
  48. else:
  49. logger.warning('{},赛事结果状态交失败, {}'.format(game_code, res))
  50. logger.warning(status_dict)
  51. else:
  52. logger.warning('{},赛事结果状态接口异常提交失败, {}'.format(game_code, res))
  53. logger.warning(status_dict)
  54. reactor.callFromThread(out.callback, item)