py_scripts/football/sporttery.py

179 lines
9.0 KiB
Python

import sys
import os
import time
sys.path.append(os.curdir)
import pandas as pd
from requests_html import HTMLSession
from furl import furl
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=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:
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_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)
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()
return data
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():
"""
"""
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()