From 60a992409c7d8878bf6cead1ff2a80f8d5881887 Mon Sep 17 00:00:00 2001 From: chenwj <654891551@qq.com> Date: Wed, 9 Aug 2023 16:41:44 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A2=9E=E5=8A=A0=E8=B5=9B=E4=BA=8B?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- football/__init__.py | 183 +++++++++++++++++++++++++++++++++++++++++- football/sporttery.py | 141 +++++++++++++++++++++++++++++--- 2 files changed, 309 insertions(+), 15 deletions(-) diff --git a/football/__init__.py b/football/__init__.py index 3cc1aaa..5dd3ac5 100644 --- a/football/__init__.py +++ b/football/__init__.py @@ -23,11 +23,64 @@ class Foootball(object): def __exit__(self, type, value, trace): print("close the db session...") - self.db.close() + 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: @@ -92,6 +145,130 @@ class Foootball(object): football_match.rq_result = None self.db.add(football_match) self.db.commit() - print(f"{football_match.id}:{football_match.match_id}写入数据库完成。。。") + 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}总进球赔率写入数据库完成...") diff --git a/football/sporttery.py b/football/sporttery.py index e7a0eb5..348ee4b 100644 --- a/football/sporttery.py +++ b/football/sporttery.py @@ -1,5 +1,6 @@ import sys import os +import time sys.path.append(os.curdir) import pandas as pd @@ -12,9 +13,10 @@ from football import Foootball class Sporttery(Foootball): _base_url = "https://webapi.sporttery.cn/gateway/jc/football/" + _api_url = "https://i.sporttery.cn/api/fb_match_info/" - def __init__(self, match_begin_date, match_end_date=None, match_page=1, page_no=1, page_size=30, is_fix=0, pc_or_wap=1): + def __init__(self, match_begin_date=None, match_end_date=None, match_page=1, page_no=1, page_size=30, is_fix=0, pc_or_wap=1): super().__init__() self.match_begin_date = match_begin_date if match_end_date is not None: @@ -29,7 +31,7 @@ class Sporttery(Foootball): self.session = HTMLSession() - def get_match_data(self, page_no=None): + def get_match_result(self, page_no=None): if page_no is None: params = dict(matchBeginDate=self.match_begin_date, matchEndDate=self.match_end_date, matchPage=self.match_page, pageSize=self.page_size, pageNo=self.page_no, isFix=self.is_fix, pcOrWap=self.pc_or_wap) @@ -41,22 +43,137 @@ class Sporttery(Foootball): data = r.json() return data - def get_odds_data(self, match_id): + def get_fixed_bonus(self, match_id: str): params = dict(clientCode=3001, matchId=match_id) url = furl(f"{self._base_url}/getFixedBonusV1.qry").add(params) r = self.session.get(url) data = r.json() return data + def get_team_statistics(self, match_id: str): + """赛事统计""" + params = dict(mid=match_id, f_callback="getTeamStatiscsInfo", _=int(time.time()*1000)) + url = furl(f"{self._api_url}/get_team_statistics").add(params) + r = self.session.get(url) + data = r.json() + return data + + def get_team_rec_data(self, tid: str, match_date, limit: int = 10, is_ha="all"): + """赛事信息""" + params = dict(c_id=0, f_callback="getTeamsDataInfo", is_ha=is_ha, md=match_date, limit=limit, _=int(time.time()*1000)) + url = furl(f"{self._api_url}/get_team_rec_data").add(params) + r = self.session.get(url) + data = r.json() + return data + + def get_result_his(self, match_id: str, limit: int = 10, is_ha="all"): + """历史交锋数据""" + params = dict(c_id=0, f_callback="getResultHistoryInfo", is_ha=is_ha, mid=match_id, limit=limit, _=int(time.time()*1000)) + url = furl(f"{self._api_url}/get_result_his").add(params) + r = self.session.get(url) + data = r.json() + return data + +def insert_match(): + """ + """ + match_list = [] + for date in pd.date_range('2023-06-24', '2023-07-01'): + sporttery = Sporttery(date.strftime('%Y-%m-%d')) + for i in range(1, 4): + data = sporttery.get_match_result(page_no=i) + match_result_list = data["value"]["matchResult"] + for match_result in match_result_list: + match_list.append(match_result) + if i + 1 > data["value"]["resultCount"]: + break + match_df = pd.DataFrame(match_list).sort_values(by=['matchId']) + with Sporttery() as sporttery: + for _, match in match_df.iterrows(): + match = match.to_dict() + sporttery.insert_match(match_id=match['matchId'], **match) + def main(): - for date in pd.date_range('2023-05-20', '2023-05-20'): - with Sporttery(date.strftime('%Y-%m-%d')) as sporttery: - for i in range(1, 4): - data = sporttery.get_match_data(page_no=i) - match_result_list = data["value"]["matchResult"] - for match_result in match_result_list: - sporttery.insert_match(match_result["matchId"], **match_result) - if i + 1 > data["value"]["resultCount"]: - continue + """ + """ + with Sporttery() as sporttery: + match_id_list = sporttery.get_match_id_list() + # match_id_list = [('1020187', )] + for match_id in match_id_list[:1]: + match_id = match_id[0] + bonus = sporttery.get_fixed_bonus(match_id) + odds_history = bonus["value"]["oddsHistory"] + print(odds_history) + match_result_list = bonus["value"]["matchResultList"] + print(match_result_list) + hhad_list = pd.DataFrame(odds_history['hhadList']) + hafu_list = pd.DataFrame(odds_history['hafuList']) + crs_list = pd.DataFrame(odds_history['crsList']) + ttg_list = pd.DataFrame(odds_history['ttgList']) + had_list = pd.DataFrame(odds_history['hadList']) + + idx1 = [idx for idx in hafu_list.columns if not idx.endswith("f") and idx[0] in ['a', 'd', 'h']] + idx2 = [idx for idx in crs_list.columns if not idx.endswith("f") and idx.startswith("s")] + idx3 = [idx for idx in ttg_list.columns if not idx.endswith("f") and idx.startswith("s")] + if had_list.empty: + spf_series = pd.Series({"win": None, "lost": None, "draw": None, "odds": None, "result": None}) + else: + spf_series = had_list.iloc[-1][['a', 'd', 'h']].astype(float).sort_values() + rq_spf_series = hhad_list.iloc[-1][['a', 'd', 'h']].astype(float).sort_values() + + bf_odds = crs_list.iloc[-1][idx2].astype(float).sort_values() + bqc_odds = hafu_list.iloc[-1][idx1].astype(float).sort_values() + zjq_odds = ttg_list.iloc[-1][idx3].astype(float).sort_values() + for match_result in match_result_list: + if match_result["code"] == "CRS": + # TODO 更新比分赔率排名 + crs = list(map(int, match_result["combination"].split(":"))) + bf_ranking = bf_odds.index.get_loc(f"s{crs[0]:02d}s{crs[1]:02d}") + 1 + sporttery.update_match_bf_odds_ranking(match_id=match_id, ranking=bf_ranking) + bf_odds["odds"] = match_result["odds"] + bf_odds["result"] = match_result["combination"].replace(":", "_") + bf_odds["odds_history"] = crs_list.to_json() + # 新增比分赔率 + sporttery.insert_bf_odds(match_id=match_id, **bf_odds.to_dict()) + elif match_result["code"] == "TTG": + zjq_ranking = zjq_odds.index.get_loc(f"s{match_result['combination'].replace('+', '')}") + 1 + # 更新总进球赔率排名 + sporttery.update_match_zjq_odds_ranking(match_id=match_id, ranking=zjq_ranking) + zjq_odds["odds"] = match_result["odds"] + zjq_odds["result"] = match_result["combination"] + zjq_odds["odds_history"] = ttg_list.to_json() + sporttery.insert_zjq_odds(match_id=match_id, **zjq_odds.to_dict()) + elif match_result["code"] == "HAFU": + bqc_ranking = bqc_odds.index.get_loc(match_result["combination"].lower().replace(":", "")) + 1 + # 更新半全场赔率排名 + sporttery.update_match_bqc_odds_ranking(match_id=match_id, ranking=bqc_ranking) + bqc_odds["odds"] = match_result["odds"] + bqc_odds["result"] = match_result["combination"].replace("H", "3").replace("D", "1").replace("A", "0").replace(":", "_") + bqc_odds["odds_history"] = hafu_list.to_json() + # 新增半全场赔率 + sporttery.insert_bqc_odds(match_id=match_id, **bqc_odds.to_dict()) + elif match_result["code"] == "HHAD": + rq_spf_ranking = rq_spf_series.index.get_loc(match_result["combination"].lower()) + 1 + # 更新让球胜平负赔率排名 + sporttery.update_match_rq_spf_odds_ranking(match_id=match_id, ranking=rq_spf_ranking) + rq_spf_series.rename(index={"a": "rq_lost", "d": "rq_draw", "h": "rq_win"}, inplace=True) + rq_spf_series["rq_count"] = match_result["goalLine"] + rq_spf_series["rq_odds"] = match_result["odds"] + rq_spf_series["rq_result"] = match_result["combination"].replace("H", "3").replace("D", "1").replace("A", "0") + rq_spf_series["rq_odds_history"] = hhad_list.to_json() + elif match_result["code"] == "HAD": + spf_ranking = spf_series.index.get_loc(match_result["combination"].lower()) + 1 + # 更新胜平负赔率排名 + sporttery.update_match_spf_odds_ranking(match_id=match_id, ranking=spf_ranking) + spf_series.rename(index={"a": "lost", "d": "draw", "h": "win"}, inplace=True) + spf_series["odds"] = match_result["odds"] + spf_series["result"] = match_result["combination"].replace("H", "3").replace("D", "1").replace("A", "0") + spf_series["odds_history"] = had_list.to_json() + spf_odds = pd.concat([spf_series, rq_spf_series]) + print(spf_odds) + # 新增胜平负赔率 + sporttery.insert_spf_odds(match_id=match_id, **spf_odds.to_dict()) + + if __name__ == "__main__": main() \ No newline at end of file