实现虎牙积分查询执行器
This commit is contained in:
@@ -6,4 +6,19 @@ from .wss_client import HuyaWssClient
|
||||
__all__ = [
|
||||
"HuyaHttpClient",
|
||||
"HuyaWssClient",
|
||||
"GetUserScoreReq",
|
||||
"GetUserScoreResp",
|
||||
]
|
||||
|
||||
|
||||
def __getattr__(name: str):
|
||||
if name in {"GetUserScoreReq", "GetUserScoreResp"}:
|
||||
from .activity_structs import GetUserScoreReq, GetUserScoreResp
|
||||
|
||||
values = {
|
||||
"GetUserScoreReq": GetUserScoreReq,
|
||||
"GetUserScoreResp": GetUserScoreResp,
|
||||
}
|
||||
globals().update(values)
|
||||
return values[name]
|
||||
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
"""虎牙活动积分相关 JCE 结构。"""
|
||||
|
||||
from .taf_protocol import TafInputStream, TafOutputStream, TafStruct, TafType
|
||||
|
||||
|
||||
class ActivityUserId(TafStruct):
|
||||
"""活动组件使用的用户标识。"""
|
||||
|
||||
def __init__(self):
|
||||
self.lUid: int = 0
|
||||
self.sGuid: str = ""
|
||||
self.sToken: str = ""
|
||||
self.sHuYaUA: str = "web&1.0&huya"
|
||||
self.sCookie: str = ""
|
||||
self.iTokenType: int = 0
|
||||
self.sDeviceInfo: str = ""
|
||||
|
||||
def write_to(self, os: TafOutputStream):
|
||||
os.write_int64(0, self.lUid)
|
||||
os.write_string(1, self.sGuid)
|
||||
os.write_string(2, self.sToken)
|
||||
os.write_string(3, self.sHuYaUA)
|
||||
os.write_string(4, self.sCookie)
|
||||
os.write_int32(5, self.iTokenType)
|
||||
os.write_string(6, self.sDeviceInfo)
|
||||
|
||||
def read_from(self, ins: TafInputStream):
|
||||
self.lUid = ins.read_int64(0, default=self.lUid)
|
||||
self.sGuid = ins.read_string(1, default=self.sGuid)
|
||||
self.sToken = ins.read_string(2, default=self.sToken)
|
||||
self.sHuYaUA = ins.read_string(3, default=self.sHuYaUA)
|
||||
self.sCookie = ins.read_string(4, default=self.sCookie)
|
||||
self.iTokenType = ins.read_int32(5, default=self.iTokenType)
|
||||
self.sDeviceInfo = ins.read_string(6, default=self.sDeviceInfo)
|
||||
|
||||
|
||||
class GetUserScoreReq(TafStruct):
|
||||
"""webActUI.getUserScore 请求。"""
|
||||
|
||||
def __init__(self):
|
||||
self.userId = ActivityUserId()
|
||||
self.sid: int = 0
|
||||
|
||||
def write_to(self, os: TafOutputStream):
|
||||
os.write_struct(0, self.userId)
|
||||
os.write_int32(1, self.sid)
|
||||
|
||||
def read_from(self, ins: TafInputStream):
|
||||
self.userId = ins.read_struct(0, ActivityUserId) or self.userId
|
||||
self.sid = ins.read_int32(1, default=self.sid)
|
||||
|
||||
|
||||
class GetUserScoreResp(TafStruct):
|
||||
"""webActUI.getUserScore 响应。"""
|
||||
|
||||
def __init__(self):
|
||||
self.status: int = 0
|
||||
self.msg: str = ""
|
||||
self.gainScore: int = 0
|
||||
self.usedScore: int = 0
|
||||
self.newGainScore: int = 0
|
||||
self.newUsedScore: int = 0
|
||||
self.score: int = 0
|
||||
self.isOpenExchangeTip: int = 0
|
||||
|
||||
@property
|
||||
def available_score(self) -> int:
|
||||
"""按现网页组件逻辑计算可用积分。"""
|
||||
if self.newGainScore or self.newUsedScore:
|
||||
return max(0, int(self.newGainScore) - int(self.newUsedScore))
|
||||
return int(self.score)
|
||||
|
||||
def read_from(self, ins: TafInputStream):
|
||||
self.status = ins.read_int32(0, default=self.status)
|
||||
self.msg = ins.read_string(1, default=self.msg)
|
||||
self.gainScore = ins.read_int32(2, default=self.gainScore)
|
||||
self.usedScore = ins.read_int32(3, default=self.usedScore)
|
||||
self.newGainScore = ins.read_int64(4, default=self.newGainScore)
|
||||
self.newUsedScore = ins.read_int64(5, default=self.newUsedScore)
|
||||
self.score = ins.read_int64(6, default=self.score)
|
||||
self.isOpenExchangeTip = ins.read_int32(7, default=self.isOpenExchangeTip)
|
||||
|
||||
def write_to(self, os: TafOutputStream):
|
||||
os.write_int32(0, self.status)
|
||||
os.write_string(1, self.msg)
|
||||
os.write_int32(2, self.gainScore)
|
||||
os.write_int32(3, self.usedScore)
|
||||
os.write_int64(4, self.newGainScore)
|
||||
os.write_int64(5, self.newUsedScore)
|
||||
os.write_int64(6, self.score)
|
||||
os.write_int32(7, self.isOpenExchangeTip)
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {
|
||||
"status": self.status,
|
||||
"msg": self.msg,
|
||||
"gain_score": self.gainScore,
|
||||
"used_score": self.usedScore,
|
||||
"new_gain_score": self.newGainScore,
|
||||
"new_used_score": self.newUsedScore,
|
||||
"score": self.score,
|
||||
"available_score": self.available_score,
|
||||
"is_open_exchange_tip": self.isOpenExchangeTip,
|
||||
}
|
||||
|
||||
|
||||
def _self_check():
|
||||
rsp = GetUserScoreResp()
|
||||
rsp.status = 200
|
||||
rsp.msg = "请求成功"
|
||||
rsp.gainScore = 300
|
||||
rsp.usedScore = 120
|
||||
rsp.newGainScore = 300
|
||||
rsp.newUsedScore = 120
|
||||
rsp.score = 180
|
||||
|
||||
os = TafOutputStream()
|
||||
os.write_struct(0, rsp)
|
||||
ins = TafInputStream(os.get_bytes())
|
||||
_, dtype = ins.read_head()
|
||||
assert dtype == TafType.STRUCT_BEGIN
|
||||
|
||||
parsed = GetUserScoreResp()
|
||||
parsed.read_from(ins)
|
||||
assert parsed.status == 200
|
||||
assert parsed.available_score == 180
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
_self_check()
|
||||
@@ -162,6 +162,16 @@ class HuyaHttpClient:
|
||||
"input_1": str(item_count),
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _build_activity_user(uid: int, cookie: str):
|
||||
"""构造活动组件 UserId,与当前积分兑换组件保持一致。"""
|
||||
from .activity_structs import ActivityUserId
|
||||
user = ActivityUserId()
|
||||
user.lUid = uid
|
||||
user.sHuYaUA = "web&1.0&huya"
|
||||
user.sCookie = cookie or ""
|
||||
return user
|
||||
|
||||
def call_rpc(self, service: str, method: str,
|
||||
req_struct: TafStruct, rsp_class=None,
|
||||
uid: int = 0, guid: str = "", cookie: str = "",
|
||||
@@ -233,6 +243,22 @@ class HuyaHttpClient:
|
||||
|
||||
# ---------- 业务便捷方法 ----------
|
||||
|
||||
def query_user_score(self, uid: int, cookie: str, sid: int, timeout: float = 15.0):
|
||||
"""查询活动可用积分。"""
|
||||
from .activity_structs import GetUserScoreReq, GetUserScoreResp
|
||||
req = GetUserScoreReq()
|
||||
req.userId = self._build_activity_user(uid, cookie)
|
||||
req.sid = sid
|
||||
return self.call_rpc(
|
||||
"webActUI",
|
||||
"getUserScore",
|
||||
req,
|
||||
GetUserScoreResp,
|
||||
uid=uid,
|
||||
cookie=cookie,
|
||||
timeout=timeout,
|
||||
)
|
||||
|
||||
def get_goods_info(self, uid, guid, cookie, pid, spu_id, sku_id=0, game_id="",
|
||||
source_id="yellowcarlist", scene=7):
|
||||
from .shop_structs import GetGoodsInfoReqV5, GoodsInfoRsp
|
||||
|
||||
Reference in New Issue
Block a user