jieshu.py 2.6 KB

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