jieshu.py 2.8 KB

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