feat: 新增获取竞彩足球接口

This commit is contained in:
chenwj 2023-05-23 15:10:29 +08:00
parent 9f6faea23d
commit 5bcff1d5bd
5 changed files with 171 additions and 10 deletions

View File

@ -8,6 +8,12 @@ from model.plw import PLW
from model.sd import SD from model.sd import SD
from model.klb import KLB from model.klb import KLB
from model.football_sfc import FootballSfc 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): class Lottery(object):
""" """
@ -117,3 +123,79 @@ class Lottery(object):
else: else:
print(f"{draw_issue}开奖结果已经写入数据库中...") print(f"{draw_issue}开奖结果已经写入数据库中...")
return None 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}写入数据库完成。。。")

View File

@ -0,0 +1,55 @@
import sys
import os
sys.path.append(os.curdir)
import pandas as pd
from requests_html import HTMLSession
from furl import furl
from lottery import Lottery
# from lottery.football import Foootball
class Sporttery(object):
_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):
self.match_begin_date = match_begin_date
if match_end_date is not None:
self.match_end_date = match_end_date
else:
self.match_end_date = match_begin_date
self.match_page = match_page
self.page_no = page_no
self.page_size = page_size
self.is_fix = is_fix
self.pc_or_wap = pc_or_wap
self.session = HTMLSession()
def get_match_data(self):
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)
url = furl(f"{self._base_url}/getMatchResultV1.qry").add(params)
r = self.session.get(url)
data = r.json()
return data
def get_odds_data(self, match_id):
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
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

View File

@ -44,7 +44,7 @@ for i in sum_group6_dict.keys():
f1 = lambda number: len([i for i in number if int(i) %2 == 0]) != 0 f1 = lambda number: len([i for i in number if int(i) %2 == 0]) != 0
f2 = lambda number: len([i for i in number if int(i) %2 == 1]) != 0 f2 = lambda number: len([i for i in number if int(i) %2 == 1]) != 0
f3 = lambda n: True if int(n[1]) - int(n[0]) == 1 or int(n[2]) - int(n[1]) == 1 else False f3 = lambda n: True if int(n[1]) - int(n[0]) == 1 or int(n[2]) - int(n[1]) == 1 else False
f4 = lambda number: True if '4' not in number and '6' not in number else False f4 = lambda number: True if '3' in number else False
f5 = lambda number: True if '2' in number or '5' in number or '7' in number else False f5 = lambda number: True if '2' in number or '5' in number or '7' in number else False
f6 = lambda number: True if len(set('279').difference(number)) == 2 else False f6 = lambda number: True if len(set('279').difference(number)) == 2 else False
if i in [12, 13, 14, 15, 16, 17]: if i in [12, 13, 14, 15, 16, 17]:

View File

@ -1,5 +1,5 @@
# coding: utf-8 # coding: utf-8
from sqlalchemy import Column, DateTime, Integer, String, text from sqlalchemy import Column, Date, DateTime, Integer, String, text
from sqlalchemy.dialects.mysql import VARCHAR from sqlalchemy.dialects.mysql import VARCHAR
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
@ -12,20 +12,27 @@ class FootballMatch(Base):
__table_args__ = {'comment': '足球比赛表'} __table_args__ = {'comment': '足球比赛表'}
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
issue = Column(VARCHAR(255)) match_id = Column(Integer, nullable=False, comment='竞彩网的比赛id')
match_date = Column(DateTime, comment='比赛时间') match_num = Column(VARCHAR(50))
buy_end_time = Column(DateTime, comment='购买截至时间') match_num_str = Column(VARCHAR(50))
league_name = Column(String(255, 'utf8mb4_general_ci'), comment='联赛名') match_date = Column(Date, comment='比赛时间')
home_name = Column(VARCHAR(255), comment='主队') league_id = Column(Integer, comment='联赛id')
away_name = Column(VARCHAR(255), comment='客队') league_name = Column(VARCHAR(255), comment='联赛名')
half_score = Column(String(10, 'utf8mb4_general_ci'), comment='半场比分') home_team_id = Column(Integer, comment='主队id')
score = Column(String(10, 'utf8mb4_general_ci'), comment='全场比分') home_name = Column(VARCHAR(255), comment='主队名')
away_team_id = Column(Integer, comment='客队id')
away_name = Column(VARCHAR(255), comment='客队名')
half_score = Column(VARCHAR(10), comment='半场比分')
score = Column(VARCHAR(10), comment='全场比分')
total_goal_count = Column(Integer, comment='总进球数') total_goal_count = Column(Integer, comment='总进球数')
result = Column(Integer, comment='结果0 负 1 平 3 胜') result = Column(Integer, comment='结果0 负 1 平 3 胜')
rq_count = Column(Integer, comment='让球数') rq_count = Column(Integer, comment='让球数')
rq_result = Column(Integer, comment='让球结果0 负 1 平 3 胜') rq_result = Column(Integer, comment='让球结果0 负 1 平 3 胜')
bqc_result = Column(String(10, 'utf8mb4_general_ci'), comment='半全场结果')
spf_odds_ranking = Column(Integer, comment='胜平负赔率排名') spf_odds_ranking = Column(Integer, comment='胜平负赔率排名')
bf_odds_ranking = Column(Integer, comment='比分赔率排名') bf_odds_ranking = Column(Integer, comment='比分赔率排名')
bqc_odds_ranking = Column(Integer, comment='半全场赔率排名') bqc_odds_ranking = Column(Integer, comment='半全场赔率排名')
zjq_odds_ranking = Column(Integer, comment='总进球数赔率排名') zjq_odds_ranking = Column(Integer, comment='总进球数赔率排名')
is_single = Column(Integer, server_default=text("'0'"), comment='是否单场')
created_at = Column(DateTime, server_default=text("CURRENT_TIMESTAMP")) created_at = Column(DateTime, server_default=text("CURRENT_TIMESTAMP"))
match_result_status = Column(Integer, comment='比赛结果状态')

17
model/football_team.py Normal file
View File

@ -0,0 +1,17 @@
# coding: utf-8
from sqlalchemy import Column, DateTime, Integer, text
from sqlalchemy.dialects.mysql import VARCHAR
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class FootballTeam(Base):
__tablename__ = 'football_team'
__table_args__ = {'comment': '足球球队信息表'}
team_id = Column(Integer, primary_key=True, comment='球队id')
team_name = Column(VARCHAR(255), comment='球队名称')
team_all_name = Column(VARCHAR(255), comment='球队全称')
created_at = Column(DateTime, server_default=text("CURRENT_TIMESTAMP"))