Api使用Android app弹幕协议

This commit is contained in:
Jerry Yan 2019-01-28 18:54:24 +08:00
parent e46e4ec0d7
commit 5da70e81f9
7 changed files with 77 additions and 64 deletions

10
Chat.py
View File

@ -2,8 +2,8 @@ from User import User
class Chat:
content:str=""
user:User=None
content: str =""
user: User=None
def __init__(self, json=None):
if json:
@ -11,9 +11,9 @@ class Chat:
def parse(self, json):
self.user = User(json)
if "Msg" in json:
if "content" in json["Msg"]:
self.content = json["Msg"]['content']
if "extra" in json:
if "content" in json["extra"]:
self.content = json["extra"]['content']
def __str__(self):
return "{} : {}".format(self.user,self.content)

23
Gift.py
View File

@ -16,26 +16,27 @@ class Gift:
def parse(self, json):
self.user = User(json)
if "Msg" in json:
if "present_end_info" in json["Msg"]:
self.ID = json["Msg"]['present_end_info']['id']
self.count = json["Msg"]['present_end_info']['count']
elif "present_info" in json["Msg"]:
self.ID = json["Msg"]['present_info']['id']
self.count = json["Msg"]['present_info']['repeat_count']
if "extra" in json:
if "present_info" in json["extra"] and json["extra"]['present_info'] is not None:
self.ID = int(json["extra"]['present_info']['id'])
self.count = json["extra"]['present_info']['repeat_count']
elif "present_end_info" in json["extra"] and json["extra"]['present_end_info'] is not None:
self.ID = int(json["extra"]['present_end_info']['id'])
self.count = json["extra"]['present_end_info']['count']
if self.ID in self.giftList:
self.amount = self.giftList[self.ID]["Price"] * self.count
@staticmethod
def update(roomID):
Gift.roomID = roomID
p = requests.get("https://live.ixigua.com/api/gifts/{roomID}".format(roomID= roomID))
p = requests.get("https://i.snssdk.com/videolive/gift/get_gift_list?room_id={roomID}".format(roomID= roomID))
d = p.json()
if isinstance(d, int) or "data" not in d:
if "gift_info" not in d:
print("错误:礼物更新失败")
else:
for i in d["data"]:
Gift.giftList[i["ID"]] = {"Name": i["Name"], "Price": i["DiamondCount"]}
for i in d["gift_info"]:
_id = int(i["id"])
Gift.giftList[_id] = {"Name": i["name"], "Price": i["diamond_count"]}
def __str__(self):
if self.ID in self.giftList:

View File

@ -12,11 +12,11 @@ class MemberMsg:
def parse(self, json):
self.user = User(json)
if "Msg" in json:
if "action" in json["Msg"]:
self.type = json["Msg"]['action']
elif "content" in json["Msg"]:
self.content = json["Msg"]['content']
if "extra" in json:
if "action" in json["extra"]:
self.type = json["extra"]['action']
elif "content" in json["extra"]:
self.content = json["extra"]['content']
def __str__(self):
if self.type == 3:
@ -28,6 +28,7 @@ class MemberMsg:
elif self.type == 1:
return "{} 进入了房间".format(self.user)
else:
print(self.type)
return self.content.format(self.user)
def __unicode__(self):

View File

@ -4,5 +4,9 @@
因个人能力有限不懂C#,无法做出界面版,只好用控制台版先顶着
### 计划更新:
- ~~使用android app协议~~ Android协议在未登陆时不显示赠送西瓜礼物及其他多数不显示的内容
- 闲的无聊的时候看一看有没有好用的GUI轮子可以用用
+ √ 使用android app协议
除从用户ID获取roomID及判断是否在播外其他均改为Android Api
- √ 闲的无聊的时候看一看有没有好用的GUI轮子可以用用
已基于BiliLive_dm制作出初代西瓜直播弹幕姬api未跟进

22
User.py
View File

@ -4,25 +4,33 @@ class User:
brand: str = ""
level: int = 0
type: int = 0
block: bool = False
mute: bool = False
def __init__(self, json=None):
if json:
self.parse(json)
def parse(self, json):
if "Msg" in json:
if "user" in json["Msg"]:
self.ID = json["Msg"]['user']['user_id']
self.name = json["Msg"]['user']['name']
if "discipulus_info" in json["Msg"]:
self.level = json["Msg"]["discipulus_info"]["level"]
self.brand = json["Msg"]["discipulus_info"]["discipulus_group_title"]
if "extra" in json:
if "user" in json["extra"] and json["extra"]["user"] is not None:
self.ID = json["extra"]['user']['user_id']
self.name = json["extra"]['user']['name']
if "im_discipulus_info" in json["extra"] and json["extra"]["im_discipulus_info"] is not None:
self.level = json["extra"]["im_discipulus_info"]["level"]
self.brand = json["extra"]["im_discipulus_info"]["discipulus_group_title"]
if "user_room_auth_status" in json["extra"] and json["extra"]["user_room_auth_status"] is not None:
self.type = json["extra"]["user_room_auth_status"]["user_type"]
self.block = json["extra"]["user_room_auth_status"]["is_block"]
self.mute = json["extra"]["user_room_auth_status"]["is_silence"]
elif "data" in json:
if "anchorInfo" in json["data"]:
self.ID = json["data"]['anchorInfo']['id']
self.name = json["data"]['anchorInfo']['name']
if self.type is None:
self.type = 0
if isinstance(self.level, str):
self.level = int(self.level)
def __str__(self):
if self.level == 0:

View File

@ -104,17 +104,13 @@ class WinMain(Api):
if self._tmp > 10:
self._tmp = 0
if self._tmp < 5 :
return "{} {} --弹幕助手 by JerryYan".format(self.roomLiver, "的直播间")
return "{} 的直播间 --弹幕助手 by JerryYan".format(self.roomLiver)
else:
if self.roomPopularity == 0:
self._tmp = 0
return self.getTitle()
else:
if self.roomMember > 0:
return "观看:{} 人气:{} --弹幕助手 by JerryYan".format(self.roomMember, self.roomPopularity)
else:
return "观看:待刷新 人气:{} --弹幕助手 by JerryYan".format(self.roomPopularity)
return "人气:{} --弹幕助手 by JerryYan".format(self.roomPopularity)
def onMessage(self, msg: str):
set_cmd_text_color(FOREGROUND_DARKGRAY)

53
api.py
View File

@ -21,7 +21,7 @@ class XiGuaLiveApi:
roomLiver: User = None
roomPopularity: int = 0
roomMember: int = 0
_cursor = ""
_cursor:str = "0"
def __init__(self, room: int):
self.room = room
@ -30,11 +30,9 @@ class XiGuaLiveApi:
self._enterRoom()
def _updateRoomInfo(self, json):
if "Msg" in json:
if "member_count" in json["Msg"]:
self.roomMember = json["Msg"]["member_count"]
if "popularity" in json["Msg"]:
self.roomPopularity = json["Msg"]["popularity"]
if "extra" in json:
if "member_count" in json["extra"] and json["extra"]["member_count"] > 0:
self.roomPopularity = json["extra"]["member_count"]
elif "data" in json:
if "popularity" in json["data"]:
self.roomPopularity = json["data"]["popularity"]
@ -77,7 +75,11 @@ class XiGuaLiveApi:
def _enterRoom(self):
if not self.isValidRoom:
return
p = s.post("https://live.ixigua.com/api/room/enter/{roomID}".format(roomID=self.roomID))
p = s.post("https://i.snssdk.com/videolive/room/enter&version_code=730"
"&device_platform=android",
data="room_id={roomID}&version_code=730"
"&device_platform=android".format(roomID=self.roomID),
headers={"Content-Type":"application/x-www-form-urlencoded"})
if DEBUG:
print(p.text)
@ -103,56 +105,57 @@ class XiGuaLiveApi:
def getDanmaku(self):
if not self.isValidRoom:
return
p = s.get("https://live.ixigua.com/api/msg/list/{roomID}?AnchorID={room}&Cursor={cursor}".format(
p = s.get("https://i.snssdk.com/videolive/im/get_msg?cursor={cursor}&room_id={roomID}&version_code=730"
"&device_platform=android".format(
roomID=self.roomID,
room=self.room,
cursor=self._cursor
))
d = p.json()
if "data" not in d or "Extra" not in d["data"] or "Cursor" not in d["data"]["Extra"]:
if "data" not in d or "extra" not in d or "cursor" not in d["extra"]:
if DEBUG:
print(d)
self.apiChangedError("数据结构改变,请与我联系")
return
else:
self._cursor = d["data"]["Extra"]["Cursor"]
self._cursor = d["extra"]["cursor"]
if DEBUG:
print("Cursor", self._cursor)
if "LiveMsgs" not in d["data"]:
if len(d['data']) == 0:
self.updRoomInfo()
return
for i in d['data']['LiveMsgs']:
for i in d['data']:
if DEBUG:
print(i)
if "Method" not in i:
if "common" not in i and "method" not in i["common"]:
continue
if i['Method'] == "VideoLivePresentMessage":
if i["common"]['method'] == "VideoLivePresentMessage":
self.onPresent(Gift(i))
elif i['Method'] == "VideoLivePresentEndTipMessage":
elif i["common"]['method'] == "VideoLivePresentEndTipMessage":
self.onPresentEnd(Gift(i))
elif i['Method'] == "VideoLiveRoomAdMessage":
elif i["common"]['method'] == "VideoLiveRoomAdMessage":
self.onAd(i)
elif i['Method'] == "VideoLiveChatMessage":
elif i["common"]['method'] == "VideoLiveChatMessage":
self.onChat(Chat(i))
elif i['Method'] == "VideoLiveMemberMessage":
elif i["common"]['method'] == "VideoLiveMemberMessage":
self._updateRoomInfo(i)
self.onEnter(MemberMsg(i))
elif i['Method'] == "VideoLiveSocialMessage":
elif i["common"]['method'] == "VideoLiveSocialMessage":
self.onSubscribe(User(i))
elif i['Method'] == "VideoLiveJoinDiscipulusMessage":
elif i["common"]['method'] == "VideoLiveJoinDiscipulusMessage":
self.onJoin(User(i))
elif i['Method'] == "VideoLiveControlMessage":
elif i["common"]['method'] == "VideoLiveControlMessage":
print("消息:", "主播离开一小会")
elif i['Method'] == "VideoLiveDiggMessage":
elif i["common"]['method'] == "VideoLiveDiggMessage":
self.onLike(User(i))
else:
pass
if __name__ == "__main__":
# room = 97621754276 # 永恒
room = 97621754276 # 永恒
# room = 75366565294
room = 83940182312 #Dae
# room = 83940182312 #Dae
# room = 58649240617 #ll
if len(sys.argv) > 1:
if sys.argv[-1] == "d":
DEBUG = True