py_scripts/football/__init__.py

98 lines
4.4 KiB
Python

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}写入数据库完成。。。")