lanqiu.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import logging
  2. from twisted.internet import defer, reactor
  3. from ..utils.helper import Helper
  4. from ..settings import LEAGUE_URL, MATCH_URL
  5. import pymongo
  6. from ..settings import M_HOST, M_USER, M_PASSWORD, M_POST, M_DB
  7. class ZuqiuPipeline(object):
  8. def open_spider(self, spider):
  9. self.mongo = pymongo.MongoClient(host=M_HOST, username=M_USER, password=M_PASSWORD, port=M_POST,
  10. authSource=M_DB)
  11. self.db = self.mongo[M_DB]
  12. @defer.inlineCallbacks
  13. def process_item(self, item, spider):
  14. logger = logging.getLogger(__name__)
  15. logger.info("进入管道")
  16. out = defer.Deferred()
  17. reactor.callInThread(self._do_calculation, item, out)
  18. yield out
  19. def _do_calculation(self, item, out):
  20. # 先保存联赛
  21. league_name = item['league']
  22. uuid = Helper.genearte_uuid(league_name)
  23. type = item['showtype']
  24. is_rollball, is_today, is_morningplate = 0, 0, 0
  25. if type == "FT":
  26. is_today = 1
  27. elif type == "FU":
  28. is_morningplate = 1
  29. elif type == "RB":
  30. is_rollball = 1
  31. else:
  32. is_stringscene = 1
  33. league_key = ["name_chinese", "kind", "match_mode", "if_stop", "last_time", "lg_id", "source", "uuid",
  34. "is_rollball", "is_today", "is_morningplate", "is_stringscene"]
  35. league_value = [league_name, "1", "1", "0", item['datetime'], item['id'], "hgg070", uuid, is_rollball, is_today,
  36. is_morningplate, is_stringscene]
  37. # 赛事
  38. childer = dict(zip(league_key, league_value))
  39. # 联赛
  40. obj = {"game_code": "lq", "title": "league", "source": "hgg070", "data": [childer]}
  41. res = Helper.async_post(LEAGUE_URL, obj)
  42. if res:
  43. if res.get('status') == 1:
  44. logging.warning("联赛提交成功,{}".format(res))
  45. # 提交赛事
  46. lres = Helper.async_post(MATCH_URL, childer)
  47. if lres.get('status') == 1:
  48. logging.warning("联赛提交成功,{}".format(res))
  49. else:
  50. logging.warning("联赛提交失败,{}".format(res))
  51. else:
  52. logging.warning("联赛提交失败,{}".format(res))
  53. else:
  54. logging.warning("联赛提交失败,{}".format(res))