Skip to content

Commit

Permalink
feat: 获取玩家信息
Browse files Browse the repository at this point in the history
  • Loading branch information
WindowsSov8forUs committed Apr 24, 2024
1 parent d9ef94b commit ea763d7
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/publish-to-pypi.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
name: Upload Python Package

on:
release:
types: [published]
push:
tags:
- 'v*.*.*'

workflow_dispatch:

Expand Down
9 changes: 9 additions & 0 deletions bestdori/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,15 @@ def __init__(self, id: int) -> None:
super().__init__(msg)
return

# 玩家不存在
class PlayerNotExistError(NotExistException):
'''玩家不存在'''
# 初始化
def __init__(self, server: str, id: int) -> None:
msg = f'服务器 {server} 上的玩家 ID {id} 不存在。'
super().__init__(msg)
return

# 服务器指定错误
class ServerNotAvailableError(BaseException):
'''服务器指定错误'''
Expand Down
44 changes: 44 additions & 0 deletions bestdori/player.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'''`bestdori.player`
BanG Dream! 玩家信息相关操作'''

from typing import Any, Optional

from .utils.utils import API
from .utils.network import Api
from .exceptions import PlayerNotExistError

class Player:
'''玩家类
参数:
id (int): 玩家 ID
server (str): 服务器
'''
def __init__(self, id: int, server: str) -> None:
self.id: int = id
self.server: str = server
self._info: Optional[dict[str, Any]] = None

def get(self) -> dict[str, Any]:
'''获取玩家信息
返回:
dict[str, Any]: 玩家信息
'''
if self._info is None:
params = {
'mode': 2
}
_info = Api(
API['player']['info'].format(server=self.server, id=self.id)
).request('get', params=params).json()
if not _info.get('result', False):
raise ValueError(f'Invalid Server: {self.server}')
_data: dict[str, Any] = _info.get('data', {})
_profile: Optional[dict[str, Any]] = _data.get('profile', None)
if _profile is None:
raise PlayerNotExistError(self.server, self.id)
self._info = _profile

return self._info
3 changes: 3 additions & 0 deletions bestdori/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
'misc': {
'llsif': 'misc/llsif.{index}.json'
},
'player': {
'get': 'player/{server}/{id}'
},
'all': {
'skills': 'skills/all.{index}.json',
'stamps': 'stamps/all.{index}.json',
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='bestdori-api',
version='0.0.19',
version='0.1.0',
author='WindowsSov8',
author_email='[email protected]',
description='Bestdori 的各种 API 调用整合,另外附带部分功能',
Expand Down

0 comments on commit ea763d7

Please sign in to comment.