jieshu.py 2.5 KB

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