add dict get value robust and release 0.0.6

This commit is contained in:
Cam 2022-06-17 00:01:05 +08:00
parent ee135a8648
commit 6a188c3b53
3 changed files with 80 additions and 73 deletions

3
.gitignore vendored
View File

@ -4,4 +4,5 @@
dist dist
build build
blive.egg-info blive.egg-info
test.py test.py
.DS_Store

View File

@ -7,6 +7,18 @@ from typing import List
""" """
def dict_chain_get(dic, chain, default=None):
if isinstance(chain, str):
chain = tuple(chain.split("."))
try:
for k in chain:
dic = dic[k]
return dic
except (TypeError, KeyError):
return default
class DictObject: class DictObject:
def __getitem__(self, idx): def __getitem__(self, idx):
return getattr(self, idx) return getattr(self, idx)
@ -45,6 +57,9 @@ class BaseMsg(ABC):
def __repr__(self) -> str: def __repr__(self) -> str:
return json.dumps(self.body) return json.dumps(self.body)
def chain_get(self, key_chain, default=None):
return dict_chain_get(self.body, key_chain, default=default)
class DanMuMsg(BaseMsg): class DanMuMsg(BaseMsg):
def __init__(self, body) -> None: def __init__(self, body) -> None:
@ -56,14 +71,12 @@ class DanMuMsg(BaseMsg):
@property @property
def sender(self): def sender(self):
if not hasattr(self, "_sender"): return Sender(
self._sender = Sender( id=self.body["info"][2][0],
id=self.body["info"][2][0], name=self.body["info"][2][1],
name=self.body["info"][2][1], medal_name=self.body["info"][3][1] if self.body["info"][3] else "",
medal_name=self.body["info"][3][1] if self.body["info"][3] else "", medal_level=self.body["info"][3][0] if self.body["info"][3] else 0,
medal_level=self.body["info"][3][0] if self.body["info"][3] else 0, )
)
return self._sender
@property @property
def timestamp(self): def timestamp(self):
@ -76,18 +89,16 @@ class InteractWordMsg(BaseMsg):
@property @property
def user(self): def user(self):
if not hasattr(self, "_user"): return Sender(
self._user = Sender( id=dict_chain_get(self.body, "data.uid"),
id=self.body["data"]["uid"], name=dict_chain_get(self.body, "data.uname"),
name=self.body["data"]["uname"], medal_name=dict_chain_get(self.body, "data.fans_medal.medal_name"),
medal_name=self.body["data"]["fans_medal"]["medal_name"], medal_level=dict_chain_get(self.body, "data.fans_medal.medal_level"),
medal_level=self.body["data"]["fans_medal"]["medal_level"], )
)
return self._user
@property @property
def timestamp(self): def timestamp(self):
return self.body["data"]["timestamp"] return dict_chain_get(self.body, "data.timestamp")
class StopLiveRoomListMsg(BaseMsg): class StopLiveRoomListMsg(BaseMsg):
@ -96,7 +107,7 @@ class StopLiveRoomListMsg(BaseMsg):
@property @property
def room_id_list(self) -> List[int]: def room_id_list(self) -> List[int]:
return self.body["data"]["room_id_list"] return dict_chain_get(self.body, "data.room_id_list")
class HotRankChangeV2Msg(BaseMsg): class HotRankChangeV2Msg(BaseMsg):
@ -105,63 +116,60 @@ class HotRankChangeV2Msg(BaseMsg):
@property @property
def area_name(self): def area_name(self):
return self.body["data"]["area_name"] return dict_chain_get(self.body, "data.area_name")
@property @property
def rank_desc(self): def rank_desc(self):
return self.body["data"]["rank_desc"] return dict_chain_get(self.body, "data.rank_desc")
@property @property
def rank(self): def rank(self):
return self.body["data"]["rank"] return dict_chain_get(self.body, "data.rank")
@property @property
def trend(self): def trend(self):
return self.body["data"]["trend"] return dict_chain_get(self.body, "data.trend")
@property @property
def timestamp(self): def timestamp(self):
return self.body["data"]["timestamp"] return dict_chain_get(self.body, "data.timestamp")
class SendGiftMsg(BaseMsg): class SendGiftMsg(BaseMsg):
# TODO 礼物逻辑复杂, 考虑更复杂的封装类 # TODO 礼物逻辑复杂, 考虑更复杂的封装类
def __init__(self, body) -> None: def __init__(self, body) -> None:
super().__init__(body) super().__init__(body)
@property @property
def sender(self): def sender(self):
if not hasattr(self, "_sender"): return Sender(
self._sender = Sender( id=dict_chain_get(self.body, "data.uid"),
id=self.body["data"]["uid"], name=dict_chain_get(self.body, "data.uname"),
name=self.body["data"]["uname"], medal_name=dict_chain_get(self.body, "data.medal_info.medal_name"),
medal_name=self.body["data"]["medal_info"]["medal_name"], medal_level=dict_chain_get(self.body, "data.medal_info.medal_level"),
medal_level=self.body["data"]["medal_info"]["medal_level"], )
)
return self._sender
@property @property
def action(self): def action(self):
return self.body["data"]["action"] return dict_chain_get(self.body, "data.action")
@property @property
def gift(self): def gift(self):
return { return {
"gift_id": self.body["data"]["giftId"], "gift_id": dict_chain_get(self.body, "data.giftId"),
"gift_name": self.body["data"]["giftName"], "gift_name": dict_chain_get(self.body, "data.giftName"),
"gift_type": self.body["data"]["giftType"], "gift_type": dict_chain_get(self.body, "data.giftType"),
} }
@property @property
def combo(self): def combo(self):
return { return {
"batch_combo_id": self.body["data"]["batch_combo_id"], "batch_combo_id": dict_chain_get(self.body, "data.batch_combo_id"),
"batch_combo_send": self.body["data"]["batch_combo_send"], "batch_combo_send": dict_chain_get(self.body, "data.batch_combo_send"),
"combo_resources_id": self.body["data"]["combo_resources_id"], "combo_resources_id": dict_chain_get(self.body, "data.combo_resources_id"),
"combo_send": self.body["data"]["combo_send"], "combo_send": dict_chain_get(self.body, "data.combo_send"),
"combo_stay_time": self.body["data"]["combo_stay_time"], "combo_stay_time": dict_chain_get(self.body, "data.combo_stay_time"),
"combo_total_coin": self.body["data"]["combo_total_coin"], "combo_total_coin": dict_chain_get(self.body, "data.combo_total_coin"),
} }
@ -171,30 +179,28 @@ class SuperChatMsg(BaseMsg):
@property @property
def content(self): def content(self):
return self.body["data"]["message"] return dict_chain_get(self.body, "data.message")
@property @property
def sender(self): def sender(self):
if not hasattr(self, "_sender"): return Sender(
self._sender = Sender( id=dict_chain_get(self.body, "data.user_info.uname"),
id=self.body["data"]["user_info"]["uname"], name=dict_chain_get(self.body, "data.uid"),
name=self.body["data"]["uid"], medal_name=dict_chain_get(self.body, "data.medal_info.medal_name"),
medal_name=self.body["data"]["medal_info"]["medal_name"], medal_level=dict_chain_get(self.body, "data.medal_info.medal_level"),
medal_level=self.body["data"]["medal_info"]["medal_level"], )
)
return self._sender
@property @property
def price(self): def price(self):
return self.body["data"]["price"] return dict_chain_get(self.body, "data.price")
@property @property
def start_time(self): def start_time(self):
return self.body["data"]["start_time"] return dict_chain_get(self.body, "data.start_time")
@property @property
def time(self): def time(self):
return self.body["data"]["time"] return dict_chain_get(self.body, "data.time")
class EntryEffectMsg(BaseMsg): class EntryEffectMsg(BaseMsg):
@ -203,23 +209,23 @@ class EntryEffectMsg(BaseMsg):
@property @property
def uid(self): def uid(self):
return self.body["data"]["uid"] return dict_chain_get(self.body, "data.uid")
@property @property
def face(self): def face(self):
return self.body["data"]["face"] return dict_chain_get(self.body, "data.face")
@property @property
def copy_writting(self): def copy_writting(self):
return self.body["data"]["copy_writing"] return dict_chain_get(self.body, "data.copy_writing")
@property @property
def web_basemap_url(self): def web_basemap_url(self):
return self.body["data"]["web_basemap_url"] return dict_chain_get(self.body, "data.web_basemap_url")
@property @property
def basemap_url(self): def basemap_url(self):
return self.body["data"]["basemap_url"] return dict_chain_get(self.body, "data.basemap_url")
class LiveInteractiveGameMsg(BaseMsg): class LiveInteractiveGameMsg(BaseMsg):
@ -228,36 +234,36 @@ class LiveInteractiveGameMsg(BaseMsg):
@property @property
def uid(self): def uid(self):
return self.body["data"]["uid"] return dict_chain_get(self.body, "data.uid")
@property @property
def uname(self): def uname(self):
return self.body["data"]["uname"] return dict_chain_get(self.body, "data.uname")
@property @property
def uface(self): def uface(self):
return self.body["data"]["uface"] return dict_chain_get(self.body, "data.uface")
@property @property
def fans_medal_level(self): def fans_medal_level(self):
return self.body["data"]["fans_medal_level"] return dict_chain_get(self.body, "data.fans_medal_level")
@property @property
def guard_level(self): def guard_level(self):
return self.body["data"]["guard_level"] return dict_chain_get(self.body, "data.guard_level")
@property @property
def gift(self): def gift(self):
return { return {
"gift_id": self.body["data"]["gift_id"], "gift_id": dict_chain_get(self.body, "data.gift_id"),
"gift_name": self.body["data"]["gift_name"], "gift_name": dict_chain_get(self.body, "data.gift_name"),
"gift_num": self.body["data"]["gift_num"], "gift_num": dict_chain_get(self.body, "data.gift_num"),
"price": self.body["data"]["price"], "price": dict_chain_get(self.body, "data.price"),
"paid": self.body["data"]["paid"], "paid": dict_chain_get(self.body, "data.paid"),
} }
def timestamp(self): def timestamp(self):
return self.body["data"]["timestamp"] return dict_chain_get(self.body, "data.timestamp")
class OnlineRankCountMsg(BaseMsg): class OnlineRankCountMsg(BaseMsg):
@ -266,4 +272,4 @@ class OnlineRankCountMsg(BaseMsg):
@property @property
def count(self): def count(self):
return self.body["data"]["count"] return dict_chain_get(self.body, "data.count")

View File

@ -5,7 +5,7 @@ with open("./README.md", "r") as f:
setuptools.setup( setuptools.setup(
name="blive", name="blive",
version="0.0.5", version="0.0.6",
author="cam", author="cam",
author_email="yulinfeng000@gmail.com", author_email="yulinfeng000@gmail.com",
long_description=description, long_description=description,