录播端

This commit is contained in:
2019-12-31 15:43:35 +08:00
parent f598a5da05
commit 3cdd12644e
14 changed files with 11 additions and 1863 deletions

187
api.py
View File

@ -3,11 +3,7 @@ import json
import sys
import random
from Struct.MemberMsg import MemberMsg
from Struct.User import User
from Struct.Gift import Gift
from Struct.Chat import Chat
from Struct.Lottery import Lottery
import requests
import time
from datetime import datetime, timedelta
@ -87,94 +83,6 @@ class XiGuaLiveApi:
if DEBUG:
print(*args)
def onPresent(self, gift: Gift):
"""
礼物连击中的消息
Message On Sending Presents
:param gift: Struct of Gift Message
"""
print("礼物连击 :", gift)
def onPresentEnd(self, gift: Gift):
"""
礼物送完了的提示信息
Message On Finished Send Present
:param gift: Struct of Gift Message
"""
print("感谢", gift)
def onAd(self, i):
"""
全局广播
All Channel Broadcasting Message( Just An Ad )
:param i: JSON DATA if you wanna using it
"""
# print(i)
pass
def onChat(self, chat: Chat):
"""
聊天信息
On Chatting
:param chat: Struct of Chat Message
"""
if not chat.isFiltered:
print(chat)
def onEnter(self, msg: MemberMsg):
"""
进入房间消息
On Entering Room
:param msg: Struct of Member Message
"""
print("提示 :", msg)
def onSubscribe(self, user: User):
"""
关注主播时的消息
On Subscribe
:param user: Struct of User Message
"""
print("消息 :", user, "关注了主播")
def onJoin(self, user: User):
"""
加入粉丝团消息
:param user:
"""
print("欢迎", user, "加入了粉丝团")
def onMessage(self, msg: str):
"""
系统消息
:param msg:
"""
print("消息 :", msg)
def onLike(self, user: User):
"""
点击喜欢的消息
On Like
:param user:
"""
print("用户", user, "点了喜欢")
def onLeave(self, json: any):
"""
下播消息
On Liver Leave
:param json:
"""
print("消息 :", "主播离开了")
self.updRoomInfo()
def onLottery(self, i: Lottery):
"""
中奖的内容
:param i:
"""
print("中奖消息 :", i)
def _checkUsernameIsMatched(self):
"""
验证主播名字是自己想要的那个
@ -227,11 +135,6 @@ class XiGuaLiveApi:
self._rawRoomInfo = d["user_info"]['live_info']
if self.isLive:
self.roomID = d["user_info"]['live_info']['room_id']
# 处理抽奖事件
l = Lottery(self._rawRoomInfo)
if l.isActive:
# 因为现在每个房间只能同时开启一个抽奖,所以放一个就行了
self.lottery = l
return True
def updRoomInfo(self, force=False):
@ -293,93 +196,3 @@ class XiGuaLiveApi:
for _j in i["cells"]:
ret.append(User(_j))
return ret
def getDanmaku(self):
"""
获取弹幕
"""
if not self.isValidRoom:
self.updRoomInfo()
return
p = self.s.get("https://i.snssdk.com/videolive/im/get_msg?cursor={cursor}&room_id={roomID}"
"&version_code=800&device_platform=android".format(
roomID=self.roomID,
cursor=self._cursor
))
d = p.json()
if "data" not in d or "extra" not in d or "cursor" not in d["extra"]:
if "base_resp" in d and d["base_resp"]["status_code"] != 10038:
print(d["base_resp"]["status_message"])
self.apiChangedError("接口数据返回错误", d)
return
else:
self._cursor = d["extra"]["cursor"]
if DEBUG:
print("Cursor:\t", self._cursor)
for i in d['data']:
if DEBUG:
print(i)
if "common" not in i and "method" not in i["common"]:
continue
if i["common"]['method'] == "VideoLivePresentMessage":
self.onPresent(Gift(i))
elif i["common"]['method'] == "VideoLivePresentEndTipMessage":
self.onPresentEnd(Gift(i))
elif i["common"]['method'] == "VideoLiveRoomAdMessage":
self.onAd(i)
elif i["common"]['method'] == "VideoLiveChatMessage":
self.onChat(Chat(i, self.lottery))
elif i["common"]['method'] == "VideoLiveMemberMessage":
self.onEnter(MemberMsg(i))
self._updateRoomPopularity(i)
elif i["common"]['method'] == "VideoLiveSocialMessage":
self.onSubscribe(User(i))
elif i["common"]['method'] == "VideoLiveJoinDiscipulusMessage":
self.onJoin(User(i))
elif i["common"]['method'] == "VideoLiveControlMessage":
print("消息:", "主播离开一小会")
# 这个消息代表主播下播了,直接更新房间信息
self.updRoomInfo(True)
elif i["common"]['method'] == "VideoLiveDiggMessage":
self.onLike(User(i))
else:
pass
if self.lottery is None or self.lottery.ID == 0:
self.lottery = Lottery(i)
# 更新抽奖信息
if self.lottery is not None and self.lottery.ID != 0:
self.lottery.update()
if self.lottery.isFinished:
self.onLottery(self.lottery)
self.lottery = None
# 2分钟自动更新下房间信息
self.updRoomInfo(len(d['data']) == 0)
if __name__ == "__main__":
name = "永恒de草薙"
if len(sys.argv) > 2:
if sys.argv[-1] == "d":
DEBUG = True
name = sys.argv[1]
print("西瓜直播弹幕助手 by JerryYan")
api = XiGuaLiveApi(name)
if not api.isValidRoom:
input("房间不存在")
sys.exit()
print("进入", api.roomLiver, "的直播间")
print("=" * 30)
while True:
if api.isLive:
try:
api.getDanmaku()
time.sleep(1)
except requests.exceptions.BaseHTTPError:
print("网络错误,请确认网络")
time.sleep(5)
except Exception as e:
print(e)
else:
print("主播未开播等待2分钟后重试")
time.sleep(60)
api.updRoomInfo()