import datetime import json import time import logging import pymongo from twisted.internet import defer, reactor from ..utils.helper import Helper from ..settings import M_HOST, M_USER, M_PASSWORD, M_POST, M_DB, LEAGUE_URL, ODDS_URL, MATCH_URL class WangqiuPipeline(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] with open('./conf/wangqiu.json', 'r', encoding='utf8') as wq: hgg070 = json.load(wq) self.hgg070 = hgg070 # # @defer.inlineCallbacks def process_item(self, item, spider): pass # out = defer.Deferred() # reactor.callInThread(self._do_calculation, item, out) # yield out # def _do_calculation(self, item, out): # # pass # # def process_item(self, item, spider): logger = logging.getLogger(__name__) match_all = item['data'] pt = str(item['index']) team_h, team_c = match_all['team_h'], match_all['team_c'] league_name, league_id = match_all['league'], match_all['gidm'] us_time, re_time = match_all['datetime'], match_all['re_time'] match_id = match_all['gid'] tag_number = item['tag'] uuid = Helper.genearte_uuid(league_name) league_list = [] last_time = '{}-12-31 23:59:59'.format(datetime.datetime.now().year) match_date, match_time, time3 = Helper.change_time(us_time) utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) if self.db.wq_league070.find({'lg_id': league_id}).count() < 1: league_dict = {"game_code": "wq", "title": "league", "source": "hgg070"} league_key = ["name_chinese", "kind", "match_mode", "if_stop", "last_time", "lg_id", "source", "uuid"] league_value = [league_name, "1", "1", "0", last_time, league_id, "hgg070", uuid] league_data = dict(zip(league_key, league_value)) league_list.append(league_data) league_dict['data'] = league_list res = Helper.async_post(LEAGUE_URL, league_dict) if res: if res.get('status') == 1: self.db.wq_league070.insert(league_data) logging.info('网球联赛提交, {}'.format(res)) else: logging.warning('网球联赛接口异常, {}'.format(res)) else: logging.info('{},联赛已存在, 不提交'.format(league_name)) pt_dict = {'0': 'is_today', '1': 'is_morningplate', '2': 'is_stringscene', '3': 'is_rollball'} pt_status = pt_dict[pt] ris_stringscene = 0 json_key = 'wq' if pt == '0': is_rollball = 0 is_today = 1 is_morningplate = 0 is_stringscene = 0 elif pt == '2': is_rollball = 0 is_today = 0 is_morningplate = 1 is_stringscene = 0 else: # json_key = 'chuanchang' is_today = 0 is_rollball = 0 is_morningplate = 0 is_stringscene = 1 ris_stringscene = 1 match_list = [] match_identity = Helper.genearte_uuid(team_h + team_c + match_date) if self.db.wq_competition070.find({'match_id': match_id, pt_status: 1}).count() < 1: match_dict = {"game_code": "wq", "title": "match", "source": "hgg070"} match_key = ["home_team", "guest_team", "lg_id", "status", "match_id", "match_date", "match_time", "tag", "source", "is_rollball", "is_morningplate", "is_stringscene", "us_time", "uuid", "half_match_id", "is_today", 'rule', "is_horn", "match_identity"] match_value = [team_h, team_c, league_id, 0, match_id, match_date, match_time, tag_number, "hgg070", is_rollball, is_morningplate, is_stringscene, us_time, uuid, 0, is_today, '', 0, match_identity] match_data = dict(zip(match_key, match_value)) match_list.append(match_data) match_dict['data'] = match_list res = Helper.async_post(MATCH_URL, match_dict) if res: if res.get('status') == 1: self.db.wq_competition070.insert(match_data) logging.info('网球赛事提交, {}'.format(res)) else: logger.warning('网球赛事表提交失败, {}'.format(res)) # logger.warning(match_dict) else: logger.warning('网球赛事接口异常提交失败, {}'.format(res)) # logger.warning(match_dict) else: logger.info('网球赛事已存在,不提交') data_list = [] for i in self.hgg070[json_key]: r_code = i['prodds'] p_code = i['plodds'] if match_all.get(r_code) == 'Y': for y in i['items']: odd = match_all.get(y['rodds']) if odd: code = y['lodds'] sole_str = p_code + code + '0' + str(match_id) + 'hgg070' sole = Helper.genearte_MD5(sole_str, pt) ratio_name = y['ratio_name'] if ratio_name: condition = match_all[ratio_name] else: condition = y['latio'] hash_str = p_code + code + '0' + str(odd) + "hgg070" + str(match_id) odds_only = Helper.genearte_MD5(hash_str, pt) odd_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition", "odds_only", "sole", "source", "type", "team"] odd_value = [match_id, league_id, code, '0', '0', code, odd, condition, odds_only, sole, 'hgg070', '0', ''] odd_dict = dict(zip(odd_key, odd_value)) data_list.append(odd_dict) odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid", "is_stringscene", "utime", "pt", 'match_identity'] odds_value = ["wq", "odds", match_id, league_id, data_list, "hgg070", '', tag_number, uuid, ris_stringscene, utime, pt, match_identity] odds_dict = dict(zip(odds_key, odds_value)) if data_list: res = Helper.async_post(ODDS_URL, odds_dict) if res: if res.get('status') == 1: logger.info('网球详细赔率提交成功, {}'.format(res)) # logger.info(odds_dict) else: logger.warning('网球详细赔率提交失败, {}'.format(res)) # logger.warning(odds_dict) else: logging.warning('网球详细赔率接口异常, {}'.format(res)) else: logger.info('网球详细赔率列表为空') # reactor.callFromThread(out.callback, item)