import logging from twisted.internet import defer, reactor from ..utils.helper import Helper from ..settings import LEAGUE_URL, MATCH_URL import pymongo from ..settings import M_HOST, M_USER, M_PASSWORD, M_POST, M_DB class ZuqiuPipeline(object): def open_spider(self, spider): self.mongo = pymongo.MongoClient(host=M_HOST, username=M_USER, password=M_PASSWORD, port=M_POST, authSource=M_DB) self.db = self.mongo[M_DB] @defer.inlineCallbacks def process_item(self, item, spider): logger = logging.getLogger(__name__) logger.info("进入管道") out = defer.Deferred() reactor.callInThread(self._do_calculation, item, out) yield out def _do_calculation(self, item, out): # 先保存联赛 league_name = item['league'] uuid = Helper.genearte_uuid(league_name) type = item['showtype'] is_rollball, is_today, is_morningplate = 0, 0, 0 if type == "FT": is_today = 1 elif type == "FU": is_morningplate = 1 elif type == "RB": is_rollball = 1 else: is_stringscene = 1 league_key = ["name_chinese", "kind", "match_mode", "if_stop", "last_time", "lg_id", "source", "uuid", "is_rollball", "is_today", "is_morningplate", "is_stringscene"] league_value = [league_name, "1", "1", "0", item['datetime'], item['id'], "hgg070", uuid, is_rollball, is_today, is_morningplate, is_stringscene] # 赛事 childer = dict(zip(league_key, league_value)) # 联赛 obj = {"game_code": "lq", "title": "league", "source": "hgg070", "data": [childer]} res = Helper.async_post(LEAGUE_URL, obj) if res: if res.get('status') == 1: logging.warning("联赛提交成功,{}".format(res)) # 提交赛事 lres = Helper.async_post(MATCH_URL, childer) if lres.get('status') == 1: logging.warning("联赛提交成功,{}".format(res)) else: logging.warning("联赛提交失败,{}".format(res)) else: logging.warning("联赛提交失败,{}".format(res)) else: logging.warning("联赛提交失败,{}".format(res))