jieshu.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. home = item['home']
  28. away = item['away']
  29. date = item['date']
  30. if ball == 1:
  31. game_code = "zq"
  32. elif ball == 2:
  33. game_code = "lq"
  34. elif ball == 3:
  35. game_code = "wq"
  36. else:
  37. game_code = "bq"
  38. if status:
  39. ostatus = 1
  40. else:
  41. ostatus = 2
  42. status_dict = {"game_code": game_code, "title": "match_status", "source": "hg3535"}
  43. update = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  44. data_list = []
  45. # us_time = '{}-{}-{}'.format(datetime.datetime.now().year, month, day)
  46. match_identity = Helper.genearte_uuid(home + away + date)
  47. data = {'match_id': match_id, 'status': ostatus, "is_rollball": 0, "is_today": 0, "is_morningplate": 0,
  48. "is_stringscene": 0, "is_horn": 0, 'game_code': game_code, 'update': update,
  49. "match_identity": match_identity}
  50. data_list.append(data)
  51. status_dict['data'] = data_list
  52. res = Helper.async_post(MATCH_STATUS, status_dict)
  53. if res:
  54. if res.get('status') == 1:
  55. self.db.match_status35.insert(data)
  56. if ostatus == 2:
  57. self.rls.hdel('hg3535.GunQiu.ids', match_id)
  58. logger.info('{},赛事结果状态交成功, {}'.format(game_code, res))
  59. # logger.info(status_dict)
  60. print(status_dict)
  61. else:
  62. logger.warning('{},赛事结果状态交失败, {}'.format(game_code, res))
  63. # logger.warning(status_dict)
  64. else:
  65. logger.warning('{},赛事结果状态接口异常提交失败, {}'.format(game_code, res))
  66. # logger.warning(status_dict)
  67. reactor.callFromThread(out.callback, item)