Skip to content

Commit

Permalink
重构 SGP Token 获取逻辑,改为订阅
Browse files Browse the repository at this point in the history
  • Loading branch information
Zzaphkiel committed Jun 15, 2024
1 parent 722637c commit 1d4c145
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
35 changes: 22 additions & 13 deletions app/lol/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ def __init__(self):
self.sgpSess = None
self.port = None
self.token = None
self.sgpToken = None
self.server = None
self.inMainLand = False

Expand All @@ -218,7 +219,7 @@ async def start(self, pid):
self.port, self.token, self.server = getPortTokenServerByPid(pid)
self.semaphore = asyncio.Semaphore(self.maxRefCnt)

self.__initSessions()
await self.__initSessions()
self.__initPlatformInfo()
await self.__initManager()
self.__initFolder()
Expand Down Expand Up @@ -247,6 +248,12 @@ async def onGameFlowPhaseChanged(event):
async def onChampSelectChanged(event):
signalBus.champSelectChanged.emit(event)

@self.listener.subscribe(event="OnJsonApiEvent_entitlements_v1_token",
uri='/entitlements/v1/token',
type=("Update",))
async def onSGPTokenChanged(event):
self.sgpToken = event['data']['accessToken']

# @self.listener.subscribe(event='OnJsonApiEvent', type=())
# async def onDebugListen(event):
# print(event)
Expand All @@ -266,7 +273,7 @@ async def close(self):

self.__init__()

def __initSessions(self):
async def __initSessions(self):
self.lcuSess = aiohttp.ClientSession(
base_url=f'https://127.0.0.1:{self.port}',
auth=aiohttp.BasicAuth('riot', self.token)
Expand All @@ -284,6 +291,8 @@ def __initSessions(self):
base_url=url
)

self.sgpToken = await self.getSGPtoken()

def __initFolder(self):
if not os.path.exists("app/resource/game"):
os.mkdir("app/resource/game")
Expand Down Expand Up @@ -909,39 +918,39 @@ async def restartClient(self):

return res

async def getSummonerGamesByPuuidViaSGP(self, token, puuid, begIdx, endIdx):
async def getSummonerGamesByPuuidViaSGP(self, puuid, begIdx, endIdx):
logger.debug(
f"getSummonerGamesByPuuidViaSGP called, {token = }, {puuid = }", TAG)
f"getSummonerGamesByPuuidViaSGP called, {puuid = }", TAG)

url = f"/match-history-query/v1/products/lol/player/{puuid}/SUMMARY"
params = {
'startIndex': begIdx,
'count': endIdx - begIdx + 1,
}

res = await self.__sgp__get(url, token, params)
res = await self.__sgp__get(url, params)
return await res.json()

async def getRankedStatsByPuuidViaSGP(self, token, puuid):
async def getRankedStatsByPuuidViaSGP(self, puuid):
logger.debug(
f"getRankedStatsByPuuidViaSGP called, {token = }, {puuid = }", TAG)
f"getRankedStatsByPuuidViaSGP called, {puuid = }", TAG)

url = f'/leagues-ledge/v2/leagueLadders/puuid/{puuid}'
res = await self.__sgp__get(url, token)
res = await self.__sgp__get(url)

return await res.json()

async def getSummonerByPuuidViaSGP(self, token, puuid):
async def getSummonerByPuuidViaSGP(self, puuid):
"""
该接口的返回值与 `self.getSummonerByPuuid()` 相比,拿不到召唤师的 `tagLine`
即数字编号信息
"""
logger.debug(
f"getSummonerByPuuidViaSGP called, {token = }, {puuid = }", TAG)
f"getSummonerByPuuidViaSGP called, {puuid = }", TAG)

url = f"/summoner-ledge/v1/regions/{self.server.lower()}/summoners/puuid/{puuid}"

res = await self.__sgp__get(url, token)
res = await self.__sgp__get(url)
return await res.json()

def isInMainland(self):
Expand All @@ -964,11 +973,11 @@ async def __put(self, path, data=None):
async def __patch(self, path, data=None):
return await self.lcuSess.patch(path, json=data, ssl=False)

async def __sgp__get(self, path, token, params=None):
async def __sgp__get(self, path, params=None):
assert self.inMainLand

headers = {
"Authorization": f"Bearer {token}"
"Authorization": f"Bearer {self.sgpToken}"
}

return await self.sgpSess.get(path, params=params, ssl=False, headers=headers)
Expand Down
16 changes: 7 additions & 9 deletions app/lol/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,10 +749,10 @@ async def parseAllyGameInfo(session, currentSummonerId, useSGP=False):
if useSGP and connector.isInMainland():
# 如果是国服就优先尝试 SGP
try:
token = await connector.getSGPtoken()
tasks = [getSummonerGamesInfoViaSGP(item, isRank, currentSummonerId, token)
tasks = [getSummonerGamesInfoViaSGP(item, isRank, currentSummonerId)
for item in session['myTeam']]
summoners = await asyncio.gather(*tasks)
print('hi')
except:
tasks = [parseSummonerGameInfo(item, isRank, currentSummonerId)
for item in session['myTeam']]
Expand Down Expand Up @@ -803,8 +803,7 @@ async def parseGameInfoByGameflowSession(session, currentSummonerId, side, useSG
if useSGP and connector.isInMainland():
# 如果是国服就优先尝试 SGP
try:
token = await connector.getSGPtoken()
tasks = [getSummonerGamesInfoViaSGP(item, isRank, currentSummonerId, token)
tasks = [getSummonerGamesInfoViaSGP(item, isRank, currentSummonerId)
for item in team]
summoners = await asyncio.gather(*tasks)

Expand Down Expand Up @@ -1026,7 +1025,7 @@ async def parseSummonerGameInfo(item, isRank, currentSummonerId):
}


async def getSummonerGamesInfoViaSGP(item, isRank, currentSummonerId, token):
async def getSummonerGamesInfoViaSGP(item, isRank, currentSummonerId):
'''
使用 SGP 接口取战绩信息
Expand All @@ -1045,7 +1044,7 @@ async def getSummonerGamesInfoViaSGP(item, isRank, currentSummonerId, token):
icon = await connector.getChampionIcon(championId)
# SGP 接口返回的 summoner name 不是最新的,导致右侧点击召唤师名后搜不到
# 重新使用 LCU API
# summoner = await connector.getSummonerByPuuidViaSGP(token, puuid)
# summoner = await connector.getSummonerByPuuidViaSGP(puuid)
summoner = await connector.getSummonerByPuuid(puuid)

try:
Expand All @@ -1056,8 +1055,7 @@ async def getSummonerGamesInfoViaSGP(item, isRank, currentSummonerId, token):
rankInfo = parseRankInfo(origRankInfo)

try:
origGamesInfo = await connector.getSummonerGamesByPuuidViaSGP(
token, puuid, 0, 14)
origGamesInfo = await connector.getSummonerGamesByPuuidViaSGP(puuid, 0, 14)

if cfg.get(cfg.gameInfoFilter) and isRank:
origGamesInfo["games"] = [
Expand All @@ -1066,7 +1064,7 @@ async def getSummonerGamesInfoViaSGP(item, isRank, currentSummonerId, token):
begIdx = 15
while len(origGamesInfo["games"]) < 11 and begIdx <= 95:
endIdx = begIdx + 10
new = (await connector.getSummonerGamesByPuuidViaSGP(token, puuid, begIdx, endIdx))["games"]
new = (await connector.getSummonerGamesByPuuidViaSGP(puuid, begIdx, endIdx))["games"]

for game in new:
if game['json']["queueId"] in (420, 440):
Expand Down

0 comments on commit 1d4c145

Please sign in to comment.