From 8c0320c97e9a3890e6b1639954b6540a45dde678 Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Tue, 29 Jan 2019 13:50:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=A8=E9=9D=A2=E6=94=B9=E4=B8=BAAndroidApi?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0=E6=8A=BD=E5=A5=96=E8=AF=86=E5=88=AB?= =?UTF-8?q?=E5=8F=8A=E6=B6=88=E6=81=AF=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Chat.py | 10 ++++- Digg.py | 0 Lottery.py | 57 ++++++++++++++++++++++++++++ LuckyUser.py | 19 ++++++++++ MemberMsg.py | 3 +- README.md | 3 +- User.py | 8 ++-- WinMain.py | 22 ++++++----- api.py | 94 ++++++++++++++++++++++++++++------------------- liveDownloader.py | 12 +++--- 10 files changed, 169 insertions(+), 59 deletions(-) create mode 100644 Digg.py create mode 100644 Lottery.py create mode 100644 LuckyUser.py diff --git a/Chat.py b/Chat.py index 4fc7356..c208240 100644 --- a/Chat.py +++ b/Chat.py @@ -1,19 +1,27 @@ from User import User +from Lottery import Lottery + class Chat: content: str ="" user: User=None + filterString:list = ["",] + isFiltered = False - def __init__(self, json=None): + def __init__(self, json=None, lottery:Lottery = None): if json: self.parse(json) + if lottery: + self.filterString.append(lottery.content) def parse(self, json): self.user = User(json) if "extra" in json: if "content" in json["extra"]: self.content = json["extra"]['content'] + if self.content in self.filterString: + self.isFiltered = True def __str__(self): return "{} : {}".format(self.user,self.content) diff --git a/Digg.py b/Digg.py new file mode 100644 index 0000000..e69de29 diff --git a/Lottery.py b/Lottery.py new file mode 100644 index 0000000..fb89f08 --- /dev/null +++ b/Lottery.py @@ -0,0 +1,57 @@ +import requests + +from LuckyUser import LuckyUser + + +class Lottery: + ID: int = 0 + isActive = False + content = "" + isFinished = False + luckyUsers = [] + joinedUserCount = 0 + prizeName = "" + + def __init__(self, json=None): + if json: + self.parse(json) + + def parse(self, json): + if "lottery_info" not in json or json["lottery_info"] is not None: + self.isActive = int(json["lottery_info"]["status"]) > 0 + self.ID = json["lottery_info"]["lottery_id"] + for i in json["lottery_info"]['conditions']: + if i['type'] != 3: + continue + self.content = i["content"] + self.joinedUserCount = int(json["lottery_info"]["candidate_num"]) + self.prizeName = json["lottery_info"]["prize_info"]["name"] + + def checkFinished(self): + p = requests.get("https://i.snssdk.com/videolive/lottery/check_user_right?lottery_id={}" + "&version_code=730&device_platform=android".format( + self.ID + )) + d = p.json() + if d["base_resp"]["status_code"] != 0: + self.isActive = False + self.isFinished = False + return + self.isActive = int(d["lottery_info"]["status"]) == 1 + self.isFinished = int(d["lottery_info"]["status"]) == 2 + self.joinedUserCount = int(d["lottery_info"]["candidate_num"]) + if self.isFinished: + for i in d["lottery_info"]["lucky_users"]: + self.luckyUsers.append(LuckyUser(i)) + + def __str__(self): + if self.isFinished: + ret = "恭喜以下中奖用户:\n" + for i in self.luckyUsers: + ret += "\t{} {}\n".format(i,self.prizeName) + ret += "\t参与人数:{}".format(self.joinedUserCount) + return ret + elif self.isActive: + return "正在抽奖中。。。参与人数:{}".format(self.joinedUserCount) + else: + return "抽奖已失效" diff --git a/LuckyUser.py b/LuckyUser.py new file mode 100644 index 0000000..d9a0cd2 --- /dev/null +++ b/LuckyUser.py @@ -0,0 +1,19 @@ +from User import User + +class LuckyUser: + + user = None + count = 0 + + def __init__(self, json=None): + if json: + self.parse(json) + + def parse(self, json): + self.user = User() + self.user.ID = json['user_id'] + self.user.name = json['user_name'] + self.count = int(json["grant_count"]) + + def __str__(self): + return "用户 {} 获得了 {} 个".format(self.user,self.count) diff --git a/MemberMsg.py b/MemberMsg.py index 7536f86..04e60f8 100644 --- a/MemberMsg.py +++ b/MemberMsg.py @@ -28,7 +28,8 @@ class MemberMsg: elif self.type == 1: return "{} 进入了房间".format(self.user) else: - print(self.type) + if self.content == "": + return "未知消息{} 关于用户 {}".format(self.type, self.user) return self.content.format(self.user) def __unicode__(self): diff --git a/README.md b/README.md index 154dab4..ac8c851 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,11 @@ 界面版:[q792602257/XiguaDanmakuHelperGUI](https://github.com/q792602257/XiguaDanmakuHelperGUI "C# ver") ### 计划更新: ++ Digg消息(点亮了喜欢)实例化 + √ ~~使用android app协议~~ 已使用 - 除从用户ID获取roomID及判断是否在播外,其他均改为Android Api + 除从用户ID获取roomID外,其他均改为Android Api + √ ~~闲的无聊的时候看一看有没有好用的GUI轮子可以用用~~ diff --git a/User.py b/User.py index 7d4a746..97d9c58 100644 --- a/User.py +++ b/User.py @@ -23,10 +23,10 @@ class User: 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'] + elif "room" in json: + if "user_info" in json["room"]: + self.ID = json["room"]['user_info']['user_id'] + self.name = json["room"]['user_info']['name'] if self.type is None: self.type = 0 if isinstance(self.level, str): diff --git a/WinMain.py b/WinMain.py index cc841bb..0b5cb52 100644 --- a/WinMain.py +++ b/WinMain.py @@ -3,9 +3,9 @@ import sys import time from Gift import Gift +from Lottery import Lottery from MemberMsg import MemberMsg from User import User - from Chat import Chat from api import XiGuaLiveApi as Api import msvcrt @@ -136,7 +136,14 @@ class WinMain(Api): def onChat(self, chat: Chat): if SHOW_ALL: - print(chat) + resetColor() + if not chat.isFiltered: + print(chat) + + def onLottery(self, i:Lottery): + set_cmd_text_color(FOREGROUND_WHITE | BACKGROUND_DARKGRAY) + print(i) + resetColor() def onPresent(self, gift: Gift): if SHOW_ALL: @@ -164,7 +171,7 @@ def warning(*args): if __name__ == "__main__": - room = 97621754276 # 永恒 + room = 6651493149011086094 # 永恒 # room = 75366565294 # room = 83940182312 #Dae resetColor() @@ -178,7 +185,7 @@ if __name__ == "__main__": pass else: try: - room = int(readInput("请输入用户ID号,默认为永恒的ID号", room, 3)) + room = int(readInput("请输入房间号,默认为永恒的房间号", room, 3)) except ValueError: pass api = WinMain(room) @@ -190,11 +197,8 @@ if __name__ == "__main__": print("=" * 30) while True: if api.isLive: - try: - os.system("title {}".format(api.getTitle())) - api.getDanmaku() - except Exception as e: - warning(e) + os.system("title {}".format(api.getTitle())) + api.getDanmaku() time.sleep(1) else: set_cmd_text_color(FOREGROUND_RED) diff --git a/api.py b/api.py index f237a34..cfbc9e6 100644 --- a/api.py +++ b/api.py @@ -4,6 +4,7 @@ from MemberMsg import MemberMsg from User import User from Gift import Gift from Chat import Chat +from Lottery import Lottery import requests import time @@ -20,14 +21,14 @@ class XiGuaLiveApi: roomTitle: str = "" roomLiver: User = None roomPopularity: int = 0 - roomMember: int = 0 _cursor:str = "0" + _updRoomCount:int = 0 + lottery:Lottery = None - def __init__(self, room: int): - self.room = room + def __init__(self, roomId: int = 6651493149011086094): + self.roomID = roomId self.updRoomInfo() Gift.update(self.roomID) - self._enterRoom() def _updateRoomInfo(self, json): if "extra" in json: @@ -52,7 +53,8 @@ class XiGuaLiveApi: pass def onChat(self, chat: Chat): - print(chat) + if not chat.isFiltered: + print(chat) def onEnter(self, msg: MemberMsg): print("提示 :", msg) @@ -70,25 +72,52 @@ class XiGuaLiveApi: print("用户", user, "点了喜欢") def onLeave(self, json: any): - print("消息 :", "主播离开一小会") + print("消息 :", "主播离开了") - def _enterRoom(self): - if not self.isValidRoom: - return - p = s.post("https://i.snssdk.com/videolive/room/enter&version_code=730" + def onLottery(self, i:Lottery): + print("中奖消息 :", i) + + def updRoomInfo(self): + 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"}) + d = p.json() + self.isValidRoom = d["base_resp"]["status_code"] == 0 + if d["base_resp"]["status_code"] != 0: + return False + if "room" not in d and d["room"] is None: + self.apiChangedError("Api发生改变,请及时联系我") + return False + self._rawRoomInfo = d["room"] + self.isLive = d["room"]['status'] == 2 + self.roomLiver = User(d) + self.roomTitle = d["room"]["title"] + self.roomPopularity = d["room"]["user_count"] + l = Lottery(d) + if l.isActive: + self.lottery = l + return True + + @staticmethod + def findRoomByUserId(userId:int): + p = s.get("https://live.ixigua.com/api/room?anchorId={room}".format(room=userId)) if DEBUG: print(p.text) + d = p.json() + if "data" not in d or "title" not in d["data"] or "id" not in d["data"]: + XiGuaLiveApi.apiChangedError("无法获取RoomID,请与我联系") + return XiGuaLiveApi() + return XiGuaLiveApi(d["data"]["id"]) - def searchLive(self, keyword): + @staticmethod + def searchLive(keyword): ret = [] p = s.get("https://security.snssdk.com/video/app/search/live/?version_code=730&device_platform=android" - "&format=json&keyword={}".format(keyword)) + "&format=json&keyword={}".format(keyword)) d = p.json() - if "data" not in d: + if "data" in d: for i in d["data"]: if i["block_type"] != 2: continue @@ -96,25 +125,6 @@ class XiGuaLiveApi: ret.append(_i["room"]) return ret - def updRoomInfo(self): - p = s.get("https://live.ixigua.com/api/room?anchorId={room}".format(room=self.room)) - if DEBUG: - print(p.text) - d = p.json() - if "data" not in d or "title" not in d["data"] or "id" not in d["data"]: - self.apiChangedError("无法获取RoomID,请与我联系") - return - self.isValidRoom = True - self._rawRoomInfo = d["data"] - self.roomLiver = User(d) - self.roomTitle = d["data"]["title"] - self.roomID = d["data"]["id"] - self._updateRoomInfo(d) - if "status" in d["data"] and d["data"]["status"] == 2: - self.isLive = True - else: - self.isLive = False - def getDanmaku(self): if not self.isValidRoom: return @@ -133,9 +143,6 @@ class XiGuaLiveApi: self._cursor = d["extra"]["cursor"] if DEBUG: print("Cursor", self._cursor) - if len(d['data']) == 0: - self.updRoomInfo() - return for i in d['data']: if DEBUG: print(i) @@ -148,7 +155,7 @@ class XiGuaLiveApi: elif i["common"]['method'] == "VideoLiveRoomAdMessage": self.onAd(i) elif i["common"]['method'] == "VideoLiveChatMessage": - self.onChat(Chat(i)) + self.onChat(Chat(i, self.lottery)) elif i["common"]['method'] == "VideoLiveMemberMessage": self._updateRoomInfo(i) self.onEnter(MemberMsg(i)) @@ -162,10 +169,20 @@ class XiGuaLiveApi: self.onLike(User(i)) else: pass + self._updRoomCount += 1 + if self._updRoomCount > 30 or len(d['data']) == 0: + if self.lottery is not None: + self.lottery.checkFinished() + if self.lottery.isFinished: + self.onLottery(self.lottery) + self.lottery = None + self.updRoomInfo() + self._updRoomCount = 0 + return if __name__ == "__main__": - room = 97621754276 # 永恒 + room = 6651493149011086094 # 永恒 # room = 75366565294 # room = 83940182312 #Dae if len(sys.argv) > 1: @@ -177,10 +194,11 @@ if __name__ == "__main__": pass print("西瓜直播弹幕助手 by JerryYan") api = XiGuaLiveApi(room) - print("进入", api.roomLiver, "的直播间") if not api.isValidRoom: + print(api.roomID) input("房间不存在") sys.exit() + print("进入", api.roomLiver, "的直播间") print("=" * 30) while True: if api.isLive: diff --git a/liveDownloader.py b/liveDownloader.py index aa20991..1ab878c 100644 --- a/liveDownloader.py +++ b/liveDownloader.py @@ -23,12 +23,13 @@ class downloader(XiGuaLiveApi): self.updPlayList() def updPlayList(self): - if "playInfo" not in self._rawRoomInfo or "Main" not in self._rawRoomInfo["playInfo"]: + if "stream_url" not in self._rawRoomInfo: if self.playlist is None: self.apiChangedError("无法获取直播链接") self.playlist = False else: - self.playlist = self._rawRoomInfo["playInfo"]["Main"]["1"]["Url"]["HlsUrl"] + self.playlist = self._rawRoomInfo["stream_url"]["alternate_pull_url"] + self.playlist = self.playlist.replace("_uhd","").replace("_sd","").replace("_ld","") def onLike(self, user): pass @@ -119,7 +120,7 @@ def upload(date=datetime.strftime(datetime.now(), "%Y_%m_%d")): if i is True: print("自动投稿中,请稍后") b.finishUpload(config["t_t"].format(date), 17, config["tag"], config["des"], - source="https://live.ixigua.com/userlive/97621754276", no_reprint=0) + source=config["src"], no_reprint=0) break print("{} : Upload {}".format(datetime.strftime(datetime.now(), "%y%m%d %H%M"), i)) try: @@ -134,7 +135,7 @@ b = Bilibili() b.login(config["b_u"], config["b_p"]) if __name__ == "__main__": - room = 97621754276 # 永恒 + room = 6651493149011086094 # 永恒 # room = 75366565294 # room = 83940182312 #Dae # room = 5947850784 #⑦ @@ -181,7 +182,8 @@ if __name__ == "__main__": uq.put(True) isUpload = False else: - pass + del config + from config import config # print("主播未开播,等待1分钟后重试") time.sleep(60) api.updRoomInfo()