import datetime import pymongo import time import logging import json # from .ball_func import fuhao from ..utils.helper import Helper # from .ball_func import get_pcode, new_time, out_time # from concurrent.futures import ThreadPoolExecutor # from requests_futures.sessions import FuturesSession from ..settings import M_HOST, M_USER, M_PASSWORD, M_POST, M_DB, LEAGUE_URL, ODDS_URL, MATCH_URL, MATCH_RESULT, MATCH_STATUS from twisted.internet import defer, reactor class Roll_Wangqiupipeline(object): def open_spider(self, spider): self.mongo = pymongo.MongoClient(host=M_HOST, username=M_USER, password=M_PASSWORD, port=M_POST, authSource='kaiyou') 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 _do_calculation(self, item, out): # def process_item(self, item, spider): logger = logging.getLogger(__name__) # 联赛id league_id = item['league_id'] # 联赛名 league_name = item['league_name'] # result = item['result'] # 比赛id match_id = item['game_id'] # 球队1 team_home = item['team_home'] # 球队2 team_guest = item['team_guest'] # 数量(97>) tag_number = item['number'] # 比赛状态 zhuangtai = item['zhuangtai'] # 日期 # data_game = item['data_game'] rule = item['rule'] # try: # data_game = item['data_game'].split("/") # month = str(data_game[1].strip()) # day = str(data_game[0]) # except Exception as e: # logger.warning(e) # data_game = item['data_game'].split(" ") # months = str(data_game[1].strip()) # month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07', # 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'} # month = month_dict[months] # day = str(data_game[0]) # 比赛时间 time_game = str(item['time_game']) # 比赛时间,时间戳 # ctime = "2019" + "-" + month + "-" + day + "" + time_game + ":00" # r_ctime = "2019" + "-" + month + "-" + day # 现在时间,时间戳 utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) match_date = datetime.datetime.now().strftime("%Y-%m-%d") # 队1分数 score_home = item['score_home'] # 队2分数 score_guest = item['score_guest'] # 第几节 jijie = item['jijie'] # # 球队得分 # qiudui = item['qiudui'] pt = 4 # 取不到 暂时注掉 # match_date, match_time = new_times(ctime) # 每局比分 score_dict = item['score_dict'] # 让盘 concedes_dict = item['concedes_dict'] concedes_dict_rule = item['concedes_dict_rule'] # 冠军 独赢 kemps_dict = item['kemps_dict'] # 让局 bureaus_dict = item['bureaus_dict'] bureaus_dict_rule = item['bureaus_dict_rule'] # 总局数大小 total_number_dict = item['total_number_dict'] total_number_dict_rule = item['total_number_dict_rule'] # 总局数单双 odd_evens_dict = item['odd_evens_dict'] odd_evens_dict_rule = item['odd_evens_dict_rule'] sq_dict = item['sq_dict'] new_sq_dict = json.dumps(sq_dict) uuid = Helper.genearte_uuid(league_name + 'hg3535') """联赛""" last_time = '{}-12-31 23:59:59'.format(datetime.datetime.now().year) if self.db.wq_league35.find({'lg_id': league_id}).count() < 1: league_dict = {"game_code": "wq", "title": "league", "source": "hg3535"} league_list = [] 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, "hg3535", 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 '成功' in res: self.db.wq_league35.insert(league_data) logger.info('网球联赛提交成功, {}'.format(res)) else: logger.warning('网球联赛提交失败, {}'.format(res)) logger.warning(league_dict) else: logger.warning('网球滚球联赛接口异常提交失败, {}'.format(res)) logger.warning(league_dict) else: logger.info('网球联赛已存在, 不提交') """赛事""" if self.db.hg3535_wq_competition35.find({"match_id": match_id, 'is_rollball': 1}).count() < 1: match_list = [] match_dict = {"game_code": "wq", "title": "match", "source": "hg3535"} 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_value = [team_home, team_guest, league_id, 1, match_id, match_date, '00:00', tag_number, "hg3535", 1, 0, 0, utime, uuid, 0, 0, rule, 0] 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 '成功' in res: self.db.wq_competition35.insert(match_data) logger.info('网球赛事提交成功, {}'.format(res)) else: logger.warning('网球赛事提交失败, {}'.format(res)) logger.warning(match_data) else: logger.warning('网球滚球赛事接口异常提交失败, {}'.format(res)) logger.warning(match_data) else: logging.info('网球赛事已存在, 不提交') """赔率""" data_list = [] odds_onlys = [] # 滚球独赢 if kemps_dict: for key, value in kemps_dict.items(): if value: sole_str = 'C' + str(key) + '0' + str(match_id) + 'hg3535' sole = Helper.genearte_MD5(sole_str, pt) odds_str = 'C' + str(key) + '0' + '0' + str(value) + 'hg3535' + str(match_id) odds_only = Helper.genearte_MD5(odds_str, pt) kemps_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition", "odds_only", "sole", "source", "type", "team"] kemps_value = [match_id, league_id, key, "0", 0, 'C', value, '', odds_only, sole, "hg3535", "0", ""] kemps_data = dict(zip(kemps_key, kemps_value)) data_list.append(kemps_data) # 滚球让盘 if concedes_dict: for key, value in concedes_dict.items(): if value: sole_str = 'LD' + str(key) + '0' + str(match_id) + 'hg3535' sole = Helper.genearte_MD5(sole_str, pt) odds_str = 'LD' + str(key) + '0' + str(concedes_dict_rule[key]) + str( value) + 'hg3535' + str(match_id) odds_only = Helper.genearte_MD5(odds_str, pt) condition = concedes_dict_rule[key] concedes_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition", "odds_only", "sole", "source", "type", "team"] concedes_value = [match_id, league_id, key, "0", 0, 'LD', value, condition, odds_only, sole, "hg3535", "0", ""] concedes_data = dict(zip(concedes_key, concedes_value)) data_list.append(concedes_data) # 滚球让局 if bureaus_dict: for key, value in bureaus_dict.items(): if value: sole_str = 'LB' + str(key) + '0' + str(match_id) + 'hg3535' sole = Helper.genearte_MD5(sole_str, pt) odds_str = 'LB' + str(key) + '0' + str(bureaus_dict_rule[key]) + str( value) + 'hg3535' + str(match_id) odds_only = Helper.genearte_MD5(odds_str, pt) condition = bureaus_dict_rule[key] bureaus_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition", "odds_only", "sole", "source", "type", "team"] bureaus_value = [match_id, league_id, key, "0", 0, 'LB', value, condition, odds_only, sole, "hg3535", "0", ""] bureaus_data = dict(zip(bureaus_key, bureaus_value)) data_list.append(bureaus_data) # 滚球总局数大小 if total_number_dict: for key, value in total_number_dict.items(): if value: sole_str = 'TN' + str(key) + '0' + str(match_id) + 'hg3535' sole = Helper.genearte_MD5(sole_str, pt) odds_str = 'TN' + str(key) + '0' + str( total_number_dict_rule[key]) + str(value) + 'hg3535' + str(match_id) odds_only = Helper.genearte_MD5(odds_str, pt) condition = total_number_dict_rule[key] total_number_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition", "odds_only", "sole", "source", "type", "team"] total_number_value = [match_id, league_id, key, "0", 0, 'TN', value, condition, odds_only, sole, "hg3535", "0", ""] total_number_data = dict(zip(total_number_key, total_number_value)) data_list.append(total_number_data) # 滚球总局数单双 if odd_evens_dict: for key, value in odd_evens_dict.items(): if value: condition = odd_evens_dict_rule[key] odds_str = "TS" + str(key) + '0' + str(odd_evens_dict_rule[key]) + str( value) + "hg3535" + str(match_id) sole_str = "TS" + str(key) + '0' + str(match_id) + "hg3535" odds_only = Helper.genearte_MD5(odds_str, pt) sole = Helper.genearte_MD5(sole_str, pt) odd_evens_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition", "odds_only", "sole", "source", "type", "team"] odd_evens_value = [match_id, league_id, key, "0", 0, 'TS', value, condition, odds_only, sole, "hg3535", "0", ""] odd_evens_data = dict(zip(odd_evens_key, odd_evens_value)) data_list.append(odd_evens_data) odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid", "is_stringscene", "utime", "pt"] odds_value = ["wq", "odds", match_id, league_id, data_list, "hg3535", odds_onlys, tag_number, uuid, 0, utime, pt] odds_dict = dict(zip(odds_key, odds_value)) if data_list: res = Helper.async_post(ODDS_URL, odds_dict) if res: if "成功" in res: logger.info('网球滚球赔率提交成功, {}'.format(res)) logger.info(odds_dict) else: logger.warning('网球滚球赔率提交失败, {}'.format(res)) logger.warning(odds_dict) else: logger.warning('网球滚球赔率接口异常提交失败, {}'.format(res)) logger.warning(odds_dict) else: logger.info('网球滚球详细赔率列表为空, 不提交') # 赛事结果 da_list = [] wq_rball = {"lg_id": league_id, "home_player_name": team_home, "guest_player_name": team_guest, "home_player_let_plate": '', "guest_player_let_plate": '', "home_player_let_inning": "", "guest_player_let_inning": "", "all_inning": "", "home_player_score": score_home, "guest_player_score": score_guest, "status": 1, "first_score_player": "", "last_score_player": "", "first_inning_score": "", "second_inning_score": "", "third_inning_score": "", "match_winer_player": "", "update_time": "", "match_time": time_game, "match_process": jijie, "tag": tag_number, "match_id": match_id, "source": "hg3535", "result_mark": score_dict, "warn_more": '', "uuid": uuid, } da_list.append(wq_rball) r_data_dict = { "game_code": "wq", "title": "match_result_r", "source": "hg3535", "data": da_list } if da_list: try: res = Helper.async_post(MATCH_RESULT, r_data_dict) if "成功" in res: logger.info('网球滚球结果记录提交成功, {}'.format(res)) logger.info(r_data_dict) else: logger.warning('网球滚球结果记录提交失败, {}'.format(res)) logger.warning(r_data_dict) except Exception as e: logger.warning("网球接口数据异常, 提交失败, {}".format(e)) else: logger.info('网球滚球结果记录为空, 不提交') status_dict = {"game_code": 'wq', "title": "match_status", "source": "hg3535"} data_list = [] data = {'match_id': match_id, 'status': 1, "is_rollball": 0, "is_today": 0, "is_morningplate": 0, "is_stringscene": 0, "is_horn": 0} data_list.append(data) status_dict['data'] = data_list res = Helper.async_post(MATCH_STATUS, status_dict) if res: if "成功" in res: self.db.match_status35.insert(status_dict) logger.info('{},赛事结果状态交成功, {}'.format('wq', res)) logger.info(status_dict) else: logger.warning('{},赛事结果状态交失败, {}'.format('wq', res)) logger.warning(status_dict) else: logger.warning('{},赛事结果状态接口异常提交失败, {}'.format('wq', res)) logger.warning(status_dict) reactor.callFromThread(out.callback, item)