# encoding: utf-8 import datetime import pymongo import time import logging from twisted.internet import defer, reactor from ..utils.helper import Helper from ..settings import M_HOST, M_USER, M_PASSWORD, M_POST, M_DB, POST_URL class Roll_Lanqiupipeline(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): # 使用twisted将mysql插入变成异步执行 logger = logging.getLogger(__name__) # 联赛id league_id = item['league_id'] # 联赛名 league_name = item['league_name'] # 比赛id match_id = item['game_id'] # 球队1 #home_team team_home = item['team_home'] # 球队2 # guest_team team_guest = item['team_guest'] # 数量(97>) number = item['number'] # 比赛状态 result_mark = item['score_dict'] time_game = str(item['time_game']) # 现在时间,时间戳 utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() - 43200)) cdate = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") match_date = cdate.split(' ')[0] match_time = cdate.split(' ')[1] # 队1分数 score_home = item['score_home'] # 队2分数 score_guest = item['score_guest'] # 第几节 jijie = item['jijie'] # 球队得分 pt = 4 match_score = "{}:{}".format(score_home, score_guest) concedes_dict = item['concede'] concedes_dict_rule = item['concede_rule'] odd_evens_dict = item['odd_even'] odd_evens_dict_rule = item['odd_even_rule'] total_sizes_dict = item['total_size'] total_sizes_dict_rule = item['total_size_rule'] last_numbers_dict = item['last_number'] capots_dict = item['capot'] team_scores_dict = item['team_score'] team_scores_dict_rule = item['team_score_rule'] last_time = "{}-12-31 23:59:59".format(datetime.datetime.now().year) uuid = Helper.genearte_uuid(league_name) if self.db.lq_league35.find({'lg_id': league_id}).count() < 1: league_dict = {"game_code": "lq", "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('{}/setLeague'.format(POST_URL), league_dict) if res: if res.get('status') == 1: logger.info('篮球滚球联赛提交成功, {}'.format(res)) self.db.lq_league35.insert(league_data) else: logger.warning('篮球滚球联赛提交失败, {}, {}'.format(res, league_dict)) else: logger.warning('篮球滚球联赛接口, 提交失败, {}, {}'.format(res, league_dict)) else: logger.info('篮球滚球联赛已存在') match_identity = Helper.genearte_uuid(team_home + team_guest + match_date) if self.db.lq_competition35.find({"match_id": match_id, 'is_rollball': 1}).count() < 1: match_list = [] match_dict = {"game_code": "lq", "title": "match", "source": "hg3535"} match_kay = ["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", "is_horn", 'match_identity'] match_value = [team_home, team_guest, league_id, 1, match_id, match_date, match_time, number, "hg3535", 1, 0, 0, utime, uuid, 0, 0, 0, match_identity] match_data = dict(zip(match_kay, match_value)) match_list.append(match_data) match_dict['data'] = match_list res = Helper.async_post('{}/setMatch'.format(POST_URL), match_dict) if res: if res.get('status') == 1: logger.info('篮球滚球赛事提交成功, {}'.format(res)) self.db.lq_competition35.insert(match_data) # logger.info(match_dict) else: logger.warning('篮球滚球赛事提交失败, {}, {}'.format(res, match_dict)) else: logger.warning('篮球滚球赛事接口异常,提交失败, {}, {}'.format(res, match_dict)) else: logger.info('篮球滚球赛事已存在') # 赔率 data_list = [] odds_onlys = [] if concedes_dict: for key, value in concedes_dict.items(): if value: for index, concede_value in enumerate(value): hash_str = "CO" + str(key) + str(index) + str(concedes_dict_rule[key][index]) + str( concede_value) + "hg3535" + str(match_id) sole_str = "CO" + str(key) + str(index) + str(match_id) + "hg3535" odds_only = Helper.genearte_MD5(hash_str, pt) sole = Helper.genearte_MD5(sole_str, pt) concede_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition", "odds_only", "sole", "source", "type", "team"] condition = concedes_dict_rule[key][index] concede_value = [match_id, league_id, key, "0", index, 'CO', concede_value, condition, odds_only, sole, "hg3535", "0", ""] concede_data = dict(zip(concede_key, concede_value)) data_list.append(concede_data) if total_sizes_dict: for key, value in total_sizes_dict.items(): if value: for index, total_sizes_value in enumerate(value): condition = total_sizes_dict_rule[key][index] hash_str = "TN" + str(key) + str(index) + str(condition) + str( total_sizes_value) + "hg3535" + str(match_id) sole_str = "TN" + str(key) + str(index) + str(match_id) + "hg3535" odds_only = Helper.genearte_MD5(hash_str, pt) sole = Helper.genearte_MD5(sole_str, pt) total_sizes_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition", "odds_only", "sole", "source", "type", "team"] total_sizes_value = [match_id, league_id, key, "0", index, 'TN', total_sizes_value, condition, odds_only, sole, "hg3535", "0", ""] total_sizes_data = dict(zip(total_sizes_key, total_sizes_value)) data_list.append(total_sizes_data) if odd_evens_dict: for key, value in odd_evens_dict.items(): if value: condition = odd_evens_dict_rule[key] hash_str = "TS" + str(key) + '0' + str(condition) + str( value) + "hg3535" + str(match_id) sole_str = "TS" + str(key) + '0' + str(match_id) + "hg3535" odds_only = Helper.genearte_MD5(hash_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) lasthome_dict = {'0或5': 'lnh0', '1或6': 'lnh1', '2或7': 'lnh2', '3或8': 'lnh3', '4或9': 'lnh4'} last_home = last_numbers_dict['last_home'] if last_home: for key, value in last_home.items(): odds_code =lasthome_dict[key] hash_str = "LN" + odds_code + '0' + str(key) + str( value) + "hg3535" + str(match_id) sole_str = "LN" + str(key) + '0' + str(match_id) + "hg3535" odds_only = Helper.genearte_MD5(hash_str, pt) sole = Helper.genearte_MD5(sole_str, pt) last_home_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition", "odds_only", "sole", "source", "type", "team"] last_home_value = [match_id, league_id, odds_code, "0", 0, 'LN', value, key, odds_only, sole, "hg3535", "0", ""] last_home_data = dict(zip(last_home_key, last_home_value)) data_list.append(last_home_data) last_guest = last_numbers_dict['last_guest'] lastguest_dict = {'0或5': 'lng0', '1或6': 'lng1', '2或7': 'lng2', '3或8': 'lng3', '4或9': 'lng4'} if last_guest: for key, value in last_guest.items(): odds_code = lastguest_dict[key] # condition = lastnumber_dict[key] hash_str = "LN" + odds_code + '0' + str(key) + str(value) + "hg3535" + str(match_id) sole_str = "LN" + str(key) + '0' + str(match_id) + "hg3535" odds_only = Helper.genearte_MD5(hash_str, pt) sole = Helper.genearte_MD5(sole_str, pt) last_guest_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition", "odds_only", "sole", "source", "type", "team"] last_guest_value = [match_id, league_id, odds_code, "0", 0, 'LN', value, key, odds_only, sole, "hg3535", "0", ""] last_guest_data = dict(zip(last_guest_key, last_guest_value)) data_list.append(last_guest_data) if capots_dict: for key, value in capots_dict.items(): if value: hash_str = "C" + str(key) + '0' + '0' + str(value) + "hg3535" + str(match_id) sole_str = "C" + str(key) + '0' + str(match_id) + "hg3535" odds_only = Helper.genearte_MD5(hash_str, pt) sole = Helper.genearte_MD5(sole_str, pt) capots_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition", "odds_only", "sole", "source", "type", "team"] capots_value = [match_id, league_id, key, "0", 0, 'C', value, "", odds_only, sole, "hg3535", "0", ""] capots_data = dict(zip(capots_key, capots_value)) data_list.append(capots_data) if team_scores_dict: for key, team_scores_value in team_scores_dict.items(): if team_scores_value: for index, value in enumerate(team_scores_value): hash_str = "TB" + str(key) + str(index) + str(team_scores_dict_rule[key][index]) + str( value) + "hg3535" + str(match_id) sole_str = "TB" + str(key) + str(index) + str(match_id) + "hg3535" odds_only = Helper.genearte_MD5(hash_str, pt) sole = Helper.genearte_MD5(sole_str, pt) team_scores_key = ["match_id", "lg_id", "odds_code", "status", "sort", "p_code", "odds", "condition", "odds_only", "sole", "source", "type", "team"] condition = team_scores_dict_rule[key][index] team_scores_value = [match_id, league_id, key, "0", index, 'TB', value, condition, odds_only, sole, "hg3535", "0", ""] team_scores_data = dict(zip(team_scores_key, team_scores_value)) data_list.append(team_scores_data) odds_key = ["game_code", "title", "match_id", "lg_id", "data", "source", "odds_only", "tag", "uuid", "is_stringscene", "utime", "pt", 'match_identity'] odds_value = ["lq", "odds", match_id, league_id, data_list, "hg3535", odds_onlys, number, uuid, 0, cdate, pt, match_identity] odds_dict = dict(zip(odds_key, odds_value)) if data_list: res = Helper.async_post('{}/setOdds'.format(POST_URL), odds_dict) if res: if res.get('status') == 1: logger.info('篮球滚球详细赔率提交成功, {}'.format(res)) else: logger.warning('篮球滚球详细赔率提交失败, {}, {}'.format(res, odds_dict)) else: logger.warning('篮球滚球详细赔率接口异常, 提交失败, {}, {}'.format(res, odds_dict)) else: logger.info('篮球滚球详细赔率列表为空, 不提交') data_list = [] lq_rball = {"home_team": team_home, "guest_team": team_guest, "lg_id": league_id, "home_rate": 0, 'match_identity': match_identity, "guest_rate": 0, "home_score": score_home, "guest_score": score_guest, "all_goal": "", "status": 1, "first_score": "", "last_score": "", "match_score": match_score, "uuid": uuid, "match_winer": "", "match_time": time_game, "u_home_score": "", "u_guest_score": "", "match_process": jijie, "tag": number, "result_mark": result_mark, "match_id": match_id, "p_code": ""} data_list.append(lq_rball) r_data_dict = { "game_code": "lq", "title": "match_result_r", "source": "hg3535", "data": data_list } if data_list: try: res = Helper.async_post('{}/setMatchResult'.format(POST_URL), r_data_dict) if res.get('status') == 1: logger.info('篮球滚球赛事结果记录提交成功, {}'.format(res)) self.db.lq_match_result35.insert(lq_rball) else: logger.warning('篮球滚球赛事结果记录提交失败, {}, {}'.format(res, r_data_dict)) # logger.warning(r_data_dict) except Exception as e: logger.warning('篮球滚球赛事结果数据,接口异常,提交失败, {}'.format(e)) else: logger.info('篮球滚球赛事结果数据,为空不提交') # reactor.callFromThread(out.callback, item)