from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from model.football_match import FootballMatch from model.football_team import FootballTeam from model.football_match_bf_odds import FootballMatchBfOdd from model.football_match_bqc_odds import FootballMatchBqcOdd from model.football_match_spf_odds import FootballMatchSpfOdd from model.football_match_zjq_odds import FootballMatchZjqOdd class Foootball(object): def __init__(self): self.db = self._get_db_session() def _get_db_session(self): _engine = create_engine("mysql+pymysql://test:123456@localhost/football?charset=utf8mb4", pool_pre_ping=True, pool_recycle=3600) _DbSession = sessionmaker(bind=_engine) return _DbSession() def __enter__(self): return self def __exit__(self, type, value, trace): print("close the db session...") self.db.close() def get_match_id_list(self): """ 获取比赛id列表 """ match_id_list = self.db.query(FootballMatch.match_id).order_by(FootballMatch.match_id.asc()).all() return match_id_list def update_match_spf_odds_ranking(self, match_id, ranking): """ 更新胜平负赔率结果排名 :param match_id: :param ranking: """ self.db.query(FootballMatch).filter_by(match_id=match_id).update({FootballMatch.spf_odds_ranking: ranking}) self.db.commit() def update_match_rq_spf_odds_ranking(self, match_id, ranking): """ 更新让球胜平负赔率结果排名 :param match_id: :param ranking: """ self.db.query(FootballMatch).filter_by(match_id=match_id).update({FootballMatch.rq_spf_odds_ranking: ranking}) self.db.commit() def update_match_bf_odds_ranking(self, match_id, ranking): """ 更新比分赔率结果排名 :param match_id: :param ranking: """ self.db.query(FootballMatch).filter_by(match_id=match_id).update({FootballMatch.bf_odds_ranking: ranking}) self.db.commit() def update_match_bqc_odds_ranking(self, match_id, ranking): """ 更新半全场赔率结果排名 :param match_id: :param ranking: """ self.db.query(FootballMatch).filter_by(match_id=match_id).update({FootballMatch.bqc_odds_ranking: ranking}) self.db.commit() def update_match_zjq_odds_ranking(self, match_id, ranking): """ 更新总进球赔率结果排名 :param match_id: :param ranking: """ self.db.query(FootballMatch).filter_by(match_id=match_id).update({FootballMatch.zjq_odds_ranking: ranking}) self.db.commit() def insert_match(self, match_id, **kwargs): """ 插入比赛结果数据 :param match_id: """ result = self.db.query(FootballMatch).filter_by(match_id=match_id).first() if result is not None: print(f"{match_id}已经写入数据库中。。。") return team1 = self.db.query(FootballTeam).filter_by(team_id=kwargs["homeTeamId"]).first() if team1 is None: home_team = FootballTeam(team_id=kwargs["homeTeamId"], team_name=kwargs["homeTeam"], team_all_name=kwargs["allHomeTeam"]) self.db.add(home_team) self.db.commit() team2 = self.db.query(FootballTeam).filter_by(team_id=kwargs["awayTeamId"]).first() if team2 is None: away_team = FootballTeam(team_id=kwargs["awayTeamId"], team_name=kwargs["awayTeam"], team_all_name=kwargs["allAwayTeam"]) self.db.add(away_team) self.db.commit() football_match = FootballMatch(match_id=match_id) football_match.match_num = kwargs["matchNum"] football_match.match_num_str = kwargs["matchNumStr"] football_match.match_date = kwargs["matchDate"] football_match.league_id = kwargs["leagueId"] football_match.league_name = kwargs["leagueNameAbbr"] football_match.home_team_id = kwargs["homeTeamId"] football_match.home_name = kwargs["homeTeam"] football_match.away_team_id = kwargs["awayTeamId"] football_match.away_name = kwargs["awayTeam"] football_match.half_score = kwargs["sectionsNo1"] football_match.score = kwargs["sectionsNo999"] football_match.is_single = kwargs["bettingSingle"] football_match.match_result_status = kwargs["matchResultStatus"] if int(kwargs["matchResultStatus"]) != 0: # 0 代表比赛取消 if kwargs["winFlag"] == "A": football_match.result = 0 elif kwargs["winFlag"] == "D": football_match.result = 1 elif kwargs["winFlag"] == "H": football_match.result = 3 else: result = None if kwargs["goalLine"] is not None: football_match.rq_count = int(kwargs["goalLine"]) else: football_match.rq_count = None if ":" in kwargs["sectionsNo1"]: half_score = list(map(int, kwargs["sectionsNo1"].split(":"))) if half_score[0] < half_score[1]: football_match.bqc_result = f"0{football_match.result}" elif half_score[0] == half_score[1]: football_match.bqc_result = f"1{football_match.result}" else: football_match.bqc_result = f"3{football_match.result}" if ":" in kwargs["sectionsNo999"]: scores = list(map(int, kwargs["sectionsNo999"].split(":"))) football_match.total_goal_count = sum(scores) if scores[0] + int(kwargs["goalLine"]) < scores[1]: football_match.rq_result = 0 elif scores[0] + int(kwargs["goalLine"]) == scores[1]: football_match.rq_result = 1 else: football_match.rq_result = 3 else: football_match.total_goal_count = None football_match.rq_result = None self.db.add(football_match) self.db.commit() print(f"{football_match.id}:{football_match.match_id}写入数据库完成...") def insert_spf_odds(self, match_id, **kwargs): """ 插入胜平负赔率 :param match_id: """ result = self.db.query(FootballMatchSpfOdd).filter_by(match_id=match_id).first() if result is not None: print(f"{match_id}胜平负赔率已经写入数据库...") return spf_odds = FootballMatchSpfOdd(match_id=match_id) spf_odds.result = kwargs["result"] spf_odds.odds = kwargs["odds"] spf_odds.odds_history = kwargs["odds_history"] spf_odds.win = kwargs["win"] spf_odds.draw = kwargs["draw"] spf_odds.lost = kwargs["lost"] spf_odds.rq_count = kwargs["rq_count"] spf_odds.rq_result = kwargs["rq_result"] spf_odds.rq_odds = kwargs["rq_odds"] spf_odds.rq_odds_history = kwargs["rq_odds_history"] spf_odds.rq_win = kwargs["rq_win"] spf_odds.rq_draw = kwargs["rq_draw"] spf_odds.rq_lost = kwargs["rq_lost"] self.db.add(spf_odds) self.db.commit() print(f"{spf_odds.id}:{spf_odds.match_id}胜平负赔率写入数据库完成...") def insert_bf_odds(self, match_id, **kwargs): """ 插入比分赔率数据 :param match_id: """ result = self.db.query(FootballMatchBfOdd).filter_by(match_id=match_id).first() if result is not None: print(f"{match_id}比分赔率已经写入数据库...") return bf_odds = FootballMatchBfOdd(match_id=match_id) bf_odds.result = kwargs["result"] bf_odds.odds = kwargs["odds"] bf_odds.odds_history = kwargs["odds_history"] bf_odds._0_0 = kwargs["s00s00"] bf_odds._0_1 = kwargs["s00s01"] bf_odds._0_2 = kwargs["s00s02"] bf_odds._0_3 = kwargs["s00s03"] bf_odds._0_4 = kwargs["s00s04"] bf_odds._0_5 = kwargs["s00s05"] bf_odds._1_0 = kwargs["s01s00"] bf_odds._1_1 = kwargs["s01s01"] bf_odds._1_2 = kwargs["s01s02"] bf_odds._1_3 = kwargs["s01s03"] bf_odds._1_4 = kwargs["s01s04"] bf_odds._1_5 = kwargs["s01s05"] bf_odds._2_0 = kwargs["s02s00"] bf_odds._2_1 = kwargs["s02s01"] bf_odds._2_2 = kwargs["s02s02"] bf_odds._2_3 = kwargs["s02s03"] bf_odds._2_4 = kwargs["s02s04"] bf_odds._2_5 = kwargs["s02s05"] bf_odds._3_0 = kwargs["s03s00"] bf_odds._3_1 = kwargs["s03s01"] bf_odds._3_2 = kwargs["s03s02"] bf_odds._3_3 = kwargs["s03s03"] bf_odds._4_0 = kwargs["s04s00"] bf_odds._4_1 = kwargs["s04s01"] bf_odds._4_2 = kwargs["s04s02"] bf_odds._5_0 = kwargs["s05s00"] bf_odds._5_1 = kwargs["s05s01"] bf_odds._5_2 = kwargs["s05s02"] bf_odds.win_others = kwargs["s-1sh"] bf_odds.draw_others = kwargs["s-1sd"] bf_odds.lost_others = kwargs["s-1sa"] self.db.add(bf_odds) self.db.commit() print(f"{bf_odds.id}:{bf_odds.match_id}比分赔率写入数据库完成。。。") def insert_bqc_odds(self, match_id, **kwargs): """ 插入半全场赔率 :param match_id: """ result = self.db.query(FootballMatchBqcOdd).filter_by(match_id=match_id).first() if result is not None: print(f"{match_id}半全场赔率已经写入数据库...") return bqc_odds = FootballMatchBqcOdd(match_id=match_id) bqc_odds.result = kwargs["result"] bqc_odds.odds = kwargs["odds"] bqc_odds.odds_history = kwargs["odds_history"] bqc_odds._0_0 = kwargs["aa"] bqc_odds._0_1 = kwargs["ad"] bqc_odds._0_3 = kwargs["ah"] bqc_odds._1_0 = kwargs["da"] bqc_odds._1_1 = kwargs["dd"] bqc_odds._1_3 = kwargs["dh"] bqc_odds._3_0 = kwargs["ha"] bqc_odds._3_1 = kwargs["hd"] bqc_odds._3_3 = kwargs["hh"] self.db.add(bqc_odds) self.db.commit() print(f"{bqc_odds.id}:{bqc_odds.match_id}全场赔率写入数据库完成...") def insert_zjq_odds(self, match_id, **kwargs): """ 插入总进球赔率 :param match_id: """ result = self.db.query(FootballMatchZjqOdd).filter_by(match_id=match_id).first() if result is not None: print(f"{match_id}总进球赔率已经写入数据库...") return zjq_odds = FootballMatchZjqOdd(match_id=match_id) zjq_odds.result = kwargs["result"] zjq_odds.odds = kwargs["odds"] zjq_odds.odds_history = kwargs["odds_history"] zjq_odds.ball_0 = kwargs["s0"] zjq_odds.ball_1 = kwargs["s1"] zjq_odds.ball_2 = kwargs["s2"] zjq_odds.ball_3 = kwargs["s3"] zjq_odds.ball_4 = kwargs["s4"] zjq_odds.ball_5 = kwargs["s5"] zjq_odds.ball_6 = kwargs["s6"] zjq_odds.ball_7_plus = kwargs["s7"] self.db.add(zjq_odds) self.db.commit() print(f"{zjq_odds.id}:{zjq_odds.match_id}总进球赔率写入数据库完成...")