| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- # encoding: utf-8
- import pymongo
- import logging
- from twisted.internet import defer, reactor
- from ..settings import M_HOST, M_USER, M_PASSWORD, M_POST, M_DB, POST_URL
- 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, authSource=M_DB)
- self.db = self.mongo[M_DB]
- @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('{}/setResultExpress'.format(POST_URL), 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, post_data))
- else:
- logger.warning('{},赛事结果状态接口异常提交失败, {}, {}'.format('zq', res, 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('{}/setResultExpress'.format(POST_URL), 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, post_data))
- else:
- logger.warning('{},赛事结果状态接口异常提交失败, {}, {}'.format('lq', res, 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('{}/setResultExpress'.format(POST_URL), 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, post_data))
- else:
- logger.warning('{},赛事结果状态接口异常提交失败, {}, {}'.format('wq', res, 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('{}/setResultExpress'.format(POST_URL), 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, post_data))
- else:
- logger.warning('{},赛事结果状态接口异常提交失败, {}, {}'.format('bq', res, post_data))
- reactor.callFromThread(out.callback, item)
|