lanqiu.py 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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,time
  6. from ..settings import M_HOST,M_USER,M_PASSWORD,M_POST,M_DB,ODDS_URL
  7. class LanqiuPipeline(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","is_rollball","is_today","is_morningplate","is_stringscene"]
  34. league_value = [league_name, "1", "1", "0", item['datetime'], item['match_id'], "hgg070", uuid,is_rollball,is_today,is_morningplate,is_stringscene]
  35. #赛事
  36. childer = dict(zip(league_key, league_value))
  37. #联赛
  38. obj = {"game_code": "lq", "title": "league", "source": "hgg070","data":[childer]}
  39. res=Helper.async_post(LEAGUE_URL,obj)
  40. if res:
  41. if res.get('status')==1:
  42. logging.warning("联赛提交成功,{}1".format(res))
  43. #提交赛事
  44. lres=Helper.async_post(MATCH_URL,childer)
  45. if lres.get('status')==1:
  46. logging.warning("赛事提交成功,{}2".format(res))
  47. #保存赔率
  48. # 是否串场
  49. if item['isP'] == 'P':
  50. ris_stringscene = 1
  51. else:
  52. ris_stringscene = 0
  53. # 现在时间,时间戳
  54. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  55. odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid",
  56. "is_stringscene", "utime", "pt", 'match_identity']
  57. odds_value = ["lq", "odds", item['match_id'], item['league_id'], item["content"], "hgg070", [],
  58. item['more_count'], uuid,
  59. ris_stringscene, utime, item['isP'], item["match_identity"]]
  60. # 赛事
  61. odderlist = dict(zip(odds_key, odds_value))
  62. res = Helper.async_post(ODDS_URL, odderlist)
  63. if res:
  64. if res.get('status') == 1:
  65. logging.warning("赔率提交成功,{}3".format(res))
  66. else:
  67. logging.warning("赔率提交失败,{}4".format(res))
  68. else:
  69. logging.warning("赔率提交失败,{}5".format(res))
  70. is_stringscene = 1
  71. league_key = ["name_chinese", "kind", "match_mode", "if_stop", "last_time", "lg_id", "source", "uuid",
  72. "is_rollball", "is_today", "is_morningplate", "is_stringscene"]
  73. league_value = [league_name, "1", "1", "0", item['datetime'], item['id'], "hgg070", uuid, is_rollball, is_today,
  74. is_morningplate, is_stringscene]
  75. # 赛事
  76. childer = dict(zip(league_key, league_value))
  77. # 联赛
  78. obj = {"game_code": "lq", "title": "league", "source": "hgg070", "data": [childer]}
  79. res = Helper.async_post(LEAGUE_URL, obj)
  80. if res:
  81. if res.get('status') == 1:
  82. logging.warning("联赛提交成功,{}".format(res))
  83. # 提交赛事
  84. lres = Helper.async_post(MATCH_URL, childer)
  85. if lres.get('status') == 1:
  86. logging.warning("联赛提交成功,{}".format(res))
  87. else:
  88. logging.warning("赛事提交失败,{}".format(res))
  89. else:
  90. logging.warning("联赛提交失败,{}".format(res))
  91. else:
  92. logging.warning("联赛提交失败,{}".format(res))