# import datetime import pymongo # import time import logging # import redis from twisted.internet import defer, reactor from ..settings import M_HOST, M_USER, M_PASSWORD, M_POST, M_DB, SAIGUO_RESULT from ..utils.helper import Helper # 滚球足球 插入 class Jieshuqiupipeline(object): def open_spider(self, spider): self.mongo = pymongo.MongoClient(host=M_HOST, username=M_USER, password=M_PASSWORD, port=M_POST) self.db = self.mongo[M_DB] # cls = redis.Redis(host='192.168.0.110', port=6379, db=0) @defer.inlineCallbacks def process_item(self, item, spider): out = defer.Deferred() reactor.callInThread(self._do_calculation, item, out) yield out defer.returnValue(item) # def process_item(self, item, spider): def _do_calculation(self, item, out): logger = logging.getLogger(__name__) pt = item['pt'] league_id = item["league_id"] league_name = item["league_name"] match_id = item["match_id"] match_date = item["match_date"] match_time = item["match_time"] h_name = item["home_team"] a_name = item["guest_team"] play_data = item["play_data"] if pt == 1: h_score = item["score_half"] f_score = item["score_full"] post_data = {'game_code': 'zq', "title": "result", "source": "hg3535", "data": []} data = {"league_id": league_id, "league_name": league_name, "match_date": match_date, "match_time": match_time, "home_team": h_name, "guest_team": a_name, "score_half": h_score, "score_full": f_score, "play_data": play_data, "match_id": match_id} post_data['data'].append(data) res = Helper.async_post(SAIGUO_RESULT, post_data) if res: if res.get('status') == 1: self.db.zq_saiguo35.insert(post_data) logger.info('{},赛事结果状态交成功, {}'.format('zq', res)) logger.info(post_data) else: logger.warning('{},赛事结果状态交失败, {}'.format('zq', res)) logger.warning(post_data) else: logger.warning('{},赛事结果状态接口异常提交失败, {}'.format('zq', res)) logger.warning(post_data) elif pt == 2: h_score = item["score_half"] f_score = item["score_result"] x_score = item["score_below"] post_data = {'game_code': 'lq', "title": "result", "source": "hg3535", "data": []} data = {"league_id": league_id, "league_name": league_name, "match_date": match_date, "match_time": match_time, "home_team": h_name, "match_id": match_id, "guest_team": a_name, "score_half": h_score, "score_below": x_score, "score_result": f_score, "play_data": play_data} post_data['data'].append(data) res = Helper.async_post(SAIGUO_RESULT, post_data) if res: if res.get('status') == 1: self.db.lq_saiguo35.insert(post_data) logger.info('{},赛事结果状态交成功, {}'.format('lq', res)) logger.info(post_data) else: logger.warning('{},赛事结果状态交失败, {}'.format('lq', res)) logger.warning(post_data) else: logger.warning('{},赛事结果状态接口异常提交失败, {}'.format('lq', res)) logger.warning(post_data) elif pt == 3: f_score = item["score_result"] post_data = {'game_code': 'wq', "title": "result", "source": "hg3535", "data": []} data = {"league_id": league_id, "league_name": league_name, "match_date": match_date, "match_time": match_time, "home_team": h_name, "guest_team": a_name, "score_result": f_score, "play_data": play_data, "match_id": match_id} post_data['data'].append(data) res = Helper.async_post(SAIGUO_RESULT, post_data) if res: if res.get('status') == 1: self.db.wq_saiguo35.insert(post_data) logger.info('{},赛事结果状态交成功, {}'.format('wq', res)) logger.info(post_data) else: logger.warning('{},赛事结果状态交失败, {}'.format('wq', res)) logger.warning(post_data) else: logger.warning('{},赛事结果状态接口异常提交失败, {}'.format('wq', res)) logger.warning(post_data) else: h_score = item["score_half"] f_score = item["score_full"] post_data = {'game_code': 'bq', "title": "result", "source": "hg3535", "data": []} data = {"league_id": league_id, "league_name": league_name, "match_date": match_date, "match_time": match_time, "home_team": h_name, "guest_team": a_name, "score_half": h_score, "score_full": f_score, "play_data": play_data, "match_id": match_id} post_data['data'].append(data) res = Helper.async_post(SAIGUO_RESULT, post_data) if res: if res.get('status') == 1: self.db.bq_saiguo35.insert(post_data) logger.info('{},赛事结果状态交成功, {}'.format('bq', res)) logger.info(post_data) else: logger.warning('{},赛事结果状态交失败, {}'.format('bq', res)) logger.warning(post_data) else: logger.warning('{},赛事结果状态接口异常提交失败, {}'.format('bq', res)) logger.warning(post_data) reactor.callFromThread(out.callback, item)