| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- 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, LEAGUE_URL, ODDS_URL, MATCH_URL, MATCH_RESULT
- 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)
- 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']
- 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'])
- # 现在时间,时间戳
- utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
- 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 + 'hg3535')
- 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(LEAGUE_URL, league_dict)
- if res:
- if res.get('status') == 1:
- logger.info('篮球滚球联赛提交成功, {}'.format(res))
- # self.db.lq_league35.update({'lg_id': league_id}, {'$set': league_data},
- # upsert=True)
- self.db.lq_league35.insert(league_data)
- logger.info(league_dict)
- else:
- logger.warning('篮球滚球联赛提交失败, {}'.format(res))
- else:
- logger.warning('篮球滚球联赛接口, 提交失败, {}'.format(res))
- logger.warning(league_dict)
- else:
- logger.info('篮球滚球联赛已存在')
- 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_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_data = dict(zip(match_kay, 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:
- logger.info('篮球滚球赛事提交成功, {}'.format(res))
- self.db.lq_competition35.insert(match_data)
- # self.db.lq_competition35.update({"match_id": match_id, 'is_rollball': 1}, {'$set': match_data},
- # upsert=True)
- logger.info(match_dict)
- else:
- logger.warning('篮球滚球赛事提交失败, {}'.format(res))
- logger.warning(match_dict)
- else:
- logger.warning('篮球滚球赛事接口异常,提交失败, {}'.format(res))
- logger.warning(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"]
- odds_value = ["lq", "odds", match_id, league_id, data_list, "hg3535", odds_onlys, 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 res.get('status') == 1:
- logger.info('篮球滚球详细赔率提交成功, {}'.format(res))
- else:
- logger.warning('篮球滚球详细赔率提交失败, {}'.format(res))
- logger.warning(odds_dict)
- else:
- logger.warning('篮球滚球详细赔率接口异常, 提交失败, {}'.format(res))
- logger.warning(odds_dict)
- else:
- logger.info('篮球滚球详细赔率列表, 不提交')
- data_list = []
- lq_rball = {"home_team": team_home, "guest_team": team_guest,
- "lg_id": league_id, "home_rate": 0,
- "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(MATCH_RESULT, 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))
- logger.warning(r_data_dict)
- except Exception as e:
- logger.warning('篮球滚球赛事结果数据,接口异常,提交失败, {}'.format(e))
- else:
- logger.info('篮球滚球赛事结果数据,为空不提交')
- reactor.callFromThread(out.callback, item)
|