201 lines
9.0 KiB
Python
201 lines
9.0 KiB
Python
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
import re
|
|
from datetime import date, datetime
|
|
|
|
from model.pls import PLS
|
|
from model.plw import PLW
|
|
from model.sd import SD
|
|
from model.klb import KLB
|
|
from model.football_sfc import FootballSfc
|
|
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 Lottery(object):
|
|
"""
|
|
Lottery Base Object
|
|
"""
|
|
def __init__(self, lottery_type='pls'):
|
|
self._pat1 = re.compile('开奖日期:(\d+)年(\d+)月(\d+)日.*')
|
|
self._pat2 = re.compile('开奖日期:(\d+)-(\d+)-(\d+)\s.*')
|
|
self._lottery_type = lottery_type
|
|
self.db = self._get_db_session()
|
|
if lottery_type.lower() == 'pls':
|
|
self._Model = PLS
|
|
elif lottery_type.lower() == 'sd':
|
|
self._Model = SD
|
|
elif lottery_type.lower() == 'plw':
|
|
self._Model = PLW
|
|
elif lottery_type.lower() == 'sfc':
|
|
self._Model = FootballSfc
|
|
elif lottery_type.lower() == 'klb':
|
|
self._Model = KLB
|
|
else:
|
|
raise Exception("未知的lottery_type")
|
|
|
|
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):
|
|
print("close the db session...")
|
|
self.db.close()
|
|
|
|
def get_last_id_by_sum_num(self, sum_num):
|
|
"""
|
|
根据总和获取id
|
|
"""
|
|
last_id = self.db.query(self._Model.id).filter_by(sum_num=sum_num).order_by(self._Model.id.desc()).first()
|
|
if last_id is not None:
|
|
return last_id[0]
|
|
return None
|
|
|
|
|
|
def insert(self, draw_issue, draw_date, draw_code, **kwargs):
|
|
"""
|
|
插入3d、排列三、排列五
|
|
"""
|
|
result = self.db.query(self._Model).filter_by(draw_issue=draw_issue).first()
|
|
if result is None:
|
|
record = self._Model()
|
|
record.draw_issue = draw_issue
|
|
record.draw_code = draw_code
|
|
record.created_at = datetime.now()
|
|
m1 = re.match(self._pat1, draw_date)
|
|
m2 = re.match(self._pat2, draw_date)
|
|
if m1 or m2 :
|
|
if m1:
|
|
record.draw_date = date(int(m1.group(1)), int(m1.group(2)), int(m1.group(3)))
|
|
else:
|
|
record.draw_date = date(int(m2.group(1)), int(m2.group(2)), int(m2.group(3)))
|
|
else:
|
|
raise Exception(f"issue:{draw_issue}数据写入失败。。。")
|
|
# 如果是排列五、排列五和3D
|
|
if isinstance(record, (PLS, SD, PLW)):
|
|
record.sum_num = sum(map(int, draw_code))
|
|
record.code_small = len(list(filter(lambda x: True if int(x) < 5 else False, draw_code)))
|
|
record.code_big = len(list(filter(lambda x: True if int(x) >= 5 else False, draw_code)))
|
|
record.code_single = len(list(filter(lambda x: True if int(x) % 2 == 1 else False, draw_code)))
|
|
record.code_double = len(list(filter(lambda x: True if int(x) % 2 == 0 else False, draw_code)))
|
|
if isinstance(record, (PLS, SD)):
|
|
record.hundred = draw_code[0]
|
|
record.ten = draw_code[1]
|
|
record.one = draw_code[2]
|
|
record.sum_hundred_one = int(draw_code[2]) + int(draw_code[0])
|
|
record.sum_hundred_ten = int(draw_code[2]) + int(record.draw_code[1])
|
|
record.sum_ten_one = int(record.draw_code[1]) + int(record.draw_code[0])
|
|
if len(set(draw_code)) == 2:
|
|
record.group_type = 3
|
|
elif len(set(draw_code)) == 3:
|
|
record.group_type = 6
|
|
else:
|
|
record.group_type = 1
|
|
elif isinstance(record, PLW):
|
|
record.ten_thousand = draw_code[0]
|
|
record.thousand = draw_code[1]
|
|
record.hundred = draw_code[2]
|
|
record.ten = draw_code[3]
|
|
record.one = draw_code[4]
|
|
record.sum_hundred_one = int(draw_code[2]) + int(draw_code[4])
|
|
record.sum_hundred_ten = int(draw_code[2]) + int(record.draw_code[3])
|
|
record.sum_ten_one = int(record.draw_code[3]) + int(record.draw_code[4])
|
|
elif isinstance(record, FootballSfc):
|
|
record.open_result = draw_code
|
|
record.sale_money = kwargs["sfc_sale"]
|
|
record.r9_sale_money = kwargs["rj_sale"]
|
|
record.award_1_money = kwargs["award_1_money"]
|
|
record.award_1_num = kwargs["award_1_num"]
|
|
record.award_2_money = kwargs["award_2_money"]
|
|
record.award_2_num = kwargs["award_2_num"]
|
|
record.r9_award_money = kwargs["r9_award_money"]
|
|
record.r9_award_num = kwargs["r9_award_num"]
|
|
self.db.add(record)
|
|
self.db.commit()
|
|
return record.id
|
|
else:
|
|
print(f"{draw_issue}开奖结果已经写入数据库中...")
|
|
return None
|
|
|
|
|
|
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}写入数据库完成。。。")
|
|
|
|
|
|
|
|
|
|
|
|
|