diff --git a/football/__init__.py b/football/__init__.py new file mode 100644 index 0000000..3cc1aaa --- /dev/null +++ b/football/__init__.py @@ -0,0 +1,97 @@ + +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 insert_match(self, match_id, **kwargs): + """ + 插入比赛结果数据 + """ + 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}写入数据库完成。。。") + + diff --git a/lottery/football/get_football_data.py b/football/get_football_data.py similarity index 100% rename from lottery/football/get_football_data.py rename to football/get_football_data.py diff --git a/lottery/football/get_game14.py b/football/get_game14.py similarity index 100% rename from lottery/football/get_game14.py rename to football/get_game14.py diff --git a/lottery/football/jczq.py b/football/jczq.py similarity index 100% rename from lottery/football/jczq.py rename to football/jczq.py diff --git a/lottery/football/sporttery.py b/football/sporttery.py similarity index 52% rename from lottery/football/sporttery.py rename to football/sporttery.py index b7d4e44..e7a0eb5 100644 --- a/lottery/football/sporttery.py +++ b/football/sporttery.py @@ -6,15 +6,16 @@ import pandas as pd from requests_html import HTMLSession from furl import furl -from lottery import Lottery +from football import Foootball -# from lottery.football import Foootball -class Sporttery(object): + +class Sporttery(Foootball): _base_url = "https://webapi.sporttery.cn/gateway/jc/football/" 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): + super().__init__() self.match_begin_date = match_begin_date if match_end_date is not None: self.match_end_date = match_end_date @@ -28,9 +29,13 @@ class Sporttery(object): self.session = HTMLSession() - def get_match_data(self): - params = dict(matchBeginDate=self.match_begin_date, matchEndDate=self.match_end_date, matchPage=self.match_page, + def get_match_data(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) + else: + params = dict(matchBeginDate=self.match_begin_date, matchEndDate=self.match_end_date, matchPage=self.match_page, + pageSize=self.page_size, pageNo=page_no, isFix=self.is_fix, pcOrWap=self.pc_or_wap) url = furl(f"{self._base_url}/getMatchResultV1.qry").add(params) r = self.session.get(url) data = r.json() @@ -42,14 +47,16 @@ class Sporttery(object): r = self.session.get(url) data = r.json() return data - -with Lottery() as lottery: - for date in pd.date_range('2015-02-07', '2023-05-22'): - for i in range(1, 4): - sporttery = Sporttery(date.strftime('%Y-%m-%d'), page_no=i) - data = sporttery.get_match_data() - match_result_list = data["value"]["matchResult"] - for match_result in match_result_list: - lottery.insert_match(match_result["matchId"], **match_result) - if i+1 > data["value"]["resultCount"]: - continue \ No newline at end of file + +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 +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/lottery/football/__init__.py b/lottery/football/__init__.py deleted file mode 100644 index 0a15a25..0000000 --- a/lottery/football/__init__.py +++ /dev/null @@ -1,19 +0,0 @@ - -from sqlalchemy import create_engine -from sqlalchemy.orm import sessionmaker - - -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/lottery?charset=utf8", pool_pre_ping=True, pool_recycle=3600) - _DbSession = sessionmaker(bind=_engine) - return _DbSession() - - def __enter__(self): - return self - - def __exit__(self, type, value, trace): - self.db.close() \ No newline at end of file