提取请求接口,添加ping房间
This commit is contained in:
parent
a3fec20b52
commit
d3cce09a7a
112
api.py
112
api.py
@ -26,6 +26,7 @@ SEARCH_USER_API = (
|
|||||||
"https://security.snssdk.com/video/app/search/live/?format=json&search_sug=0&forum=0&m_tab=live&is_native_req=0"
|
"https://security.snssdk.com/video/app/search/live/?format=json&search_sug=0&forum=0&m_tab=live&is_native_req=0"
|
||||||
"&offset=0&from=live&en_qc=1&pd=xigua_live&ssmix=a{COMMON}&keyword={keyword}")
|
"&offset=0&from=live&en_qc=1&pd=xigua_live&ssmix=a{COMMON}&keyword={keyword}")
|
||||||
USER_INFO_API = "https://is.snssdk.com/video/app/user/home/v7/?to_user_id={userId}{COMMON}"
|
USER_INFO_API = "https://is.snssdk.com/video/app/user/home/v7/?to_user_id={userId}{COMMON}"
|
||||||
|
PING_ROOM_API = "https://webcast3.ixigua.com/webcast/room/ping/audience/?room_id={roomId}{COMMON}"
|
||||||
COMMON_HEADERS = {
|
COMMON_HEADERS = {
|
||||||
"sdk-version": '1',
|
"sdk-version": '1',
|
||||||
"User-Agent": "Dalvik/2.1.0 (Linux; U; Android 9) VideoArticle/8.1.6 cronet/TTNetVersion:b97574c0 2019-09-24",
|
"User-Agent": "Dalvik/2.1.0 (Linux; U; Android 9) VideoArticle/8.1.6 cronet/TTNetVersion:b97574c0 2019-09-24",
|
||||||
@ -43,23 +44,26 @@ class XiGuaLiveApi:
|
|||||||
"""
|
"""
|
||||||
if name is None:
|
if name is None:
|
||||||
name = "永恒de草薙"
|
name = "永恒de草薙"
|
||||||
|
self.roomLiver = None
|
||||||
if type(name) == User:
|
if type(name) == User:
|
||||||
self.roomLiver = name
|
self.roomLiver = name
|
||||||
self.name = name.name
|
self.name = name.name
|
||||||
|
elif str(name).isdigit():
|
||||||
|
self.roomLiver = User()
|
||||||
|
self.roomLiver.ID = int(name)
|
||||||
else:
|
else:
|
||||||
self.name = str(name)
|
self.name = str(name)
|
||||||
self.isLive = False
|
self.isLive = False
|
||||||
self.isValidRoom = False
|
self.isValidRoom = False
|
||||||
self._rawRoomInfo = {}
|
self._rawRoomInfo = {}
|
||||||
self.roomID = 0
|
self.roomID = 0
|
||||||
self.roomLiver = None
|
|
||||||
self.roomPopularity = 0
|
self.roomPopularity = 0
|
||||||
self._cursor = "0"
|
self._cursor = "0"
|
||||||
self.lottery = None
|
self.lottery = None
|
||||||
self.s = requests.session()
|
self.s = requests.session()
|
||||||
self.s.headers.update(COMMON_HEADERS)
|
self.s.headers.update(COMMON_HEADERS)
|
||||||
self._updRoomAt = datetime.now()
|
self._updRoomAt = datetime.now()
|
||||||
self.updRoomInfo(True)
|
self.updRoomInfo()
|
||||||
|
|
||||||
def _updateRoomPopularity(self, _data):
|
def _updateRoomPopularity(self, _data):
|
||||||
"""
|
"""
|
||||||
@ -74,6 +78,48 @@ class XiGuaLiveApi:
|
|||||||
if "popularity" in _data["data"]:
|
if "popularity" in _data["data"]:
|
||||||
self.roomPopularity = _data["data"]["popularity"]
|
self.roomPopularity = _data["data"]["popularity"]
|
||||||
|
|
||||||
|
def getJson(self, url, **kwargs):
|
||||||
|
try:
|
||||||
|
p = self.s.get(url, **kwargs)
|
||||||
|
except Exception as e:
|
||||||
|
print("网络请求失败")
|
||||||
|
if DEBUG:
|
||||||
|
print("GET")
|
||||||
|
print("URL", url)
|
||||||
|
print("ERR ", e.__str__())
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
return p.json()
|
||||||
|
except Exception as e:
|
||||||
|
print("解析请求失败")
|
||||||
|
if DEBUG:
|
||||||
|
print("GET JSON")
|
||||||
|
print("URL", url)
|
||||||
|
print("CNT", p.text)
|
||||||
|
print("ERR ", e.__str__())
|
||||||
|
return None
|
||||||
|
|
||||||
|
def postJson(self, url, data, **kwargs):
|
||||||
|
try:
|
||||||
|
p = self.s.post(url, data, **kwargs)
|
||||||
|
except Exception as e:
|
||||||
|
print("网络请求失败")
|
||||||
|
if DEBUG:
|
||||||
|
print("POST")
|
||||||
|
print("URL", url)
|
||||||
|
print("ERR ", e.__str__())
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
return p.json()
|
||||||
|
except Exception as e:
|
||||||
|
print("解析请求失败")
|
||||||
|
if DEBUG:
|
||||||
|
print("GET JSON")
|
||||||
|
print("URL", url)
|
||||||
|
print("CNT", p.text)
|
||||||
|
print("ERR ", e.__str__())
|
||||||
|
return None
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def apiChangedError(msg: str, *args):
|
def apiChangedError(msg: str, *args):
|
||||||
"""
|
"""
|
||||||
@ -205,15 +251,9 @@ class XiGuaLiveApi:
|
|||||||
return False
|
return False
|
||||||
_formatData = {"COMMON": COMMON_GET_PARAM, "TIMESTAMP": time.time() * 1000, "userId": self.roomLiver.ID}
|
_formatData = {"COMMON": COMMON_GET_PARAM, "TIMESTAMP": time.time() * 1000, "userId": self.roomLiver.ID}
|
||||||
_url = USER_INFO_API.format_map(_formatData).format_map(_formatData)
|
_url = USER_INFO_API.format_map(_formatData).format_map(_formatData)
|
||||||
try:
|
d = self.getJson(_url)
|
||||||
p = self.s.get(_url)
|
if d is None:
|
||||||
except Exception as e:
|
print("获取用户信息失败")
|
||||||
self.apiChangedError("更新用户信息接口请求失败", e.__str__())
|
|
||||||
return False
|
|
||||||
try:
|
|
||||||
d = p.json()
|
|
||||||
except Exception as e:
|
|
||||||
self.apiChangedError("更新房间接口错误", e.__str__())
|
|
||||||
return False
|
return False
|
||||||
self.isValidRoom = d["status"] == 0
|
self.isValidRoom = d["status"] == 0
|
||||||
if "user_info" not in d and d["user_info"] is None:
|
if "user_info" not in d and d["user_info"] is None:
|
||||||
@ -234,39 +274,29 @@ class XiGuaLiveApi:
|
|||||||
self.lottery = l
|
self.lottery = l
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def updRoomInfo(self, force=False):
|
def _checkRoom(self):
|
||||||
|
if self.roomID == 0:
|
||||||
|
return False
|
||||||
|
_formatData = {"COMMON": COMMON_GET_PARAM, "TIMESTAMP": time.time() * 1000, "roomId": self.roomID}
|
||||||
|
_url = PING_ROOM_API.format_map(_formatData).format_map(_formatData)
|
||||||
|
d = self.getJson(_url)
|
||||||
|
if d is None:
|
||||||
|
print("检查房间失败")
|
||||||
|
return False
|
||||||
|
self.isLive = d["data"]["room_status"] == 0
|
||||||
|
return self.isLive
|
||||||
|
|
||||||
|
def updRoomInfo(self):
|
||||||
"""
|
"""
|
||||||
更新房间信息
|
更新房间信息
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
if not force and self._updRoomAt > (datetime.now() - timedelta(minutes=3)):
|
if not self.isValidRoom:
|
||||||
return self.isLive
|
return self._forceSearchUser()
|
||||||
self._updRoomAt = datetime.now()
|
elif not self.isLive:
|
||||||
if self.isValidRoom:
|
|
||||||
return self._updateUserOnly()
|
return self._updateUserOnly()
|
||||||
else:
|
else:
|
||||||
return self._forceSearchUser()
|
return self._checkRoom()
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def getUserInfoByUserId(userId):
|
|
||||||
"""
|
|
||||||
通过UserId查找用户的房间号
|
|
||||||
:param userId: 用户ID
|
|
||||||
:return: XiGuaLiveApi
|
|
||||||
"""
|
|
||||||
_formatData = {"COMMON": COMMON_GET_PARAM, "TIMESTAMP": time.time() * 1000, "userId": userId}
|
|
||||||
_url = USER_INFO_API.format_map(_formatData).format_map(_formatData)
|
|
||||||
try:
|
|
||||||
p = requests.get(_url, headers=COMMON_HEADERS)
|
|
||||||
except Exception as e:
|
|
||||||
XiGuaLiveApi.apiChangedError("更新用户信息接口请求失败", e.__str__())
|
|
||||||
return None
|
|
||||||
try:
|
|
||||||
d = p.json()
|
|
||||||
except Exception as e:
|
|
||||||
XiGuaLiveApi.apiChangedError("更新房间接口错误", e.__str__())
|
|
||||||
return None
|
|
||||||
return XiGuaLiveApi(User(d))
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def searchUser(keyword):
|
def searchUser(keyword):
|
||||||
@ -298,9 +328,7 @@ class XiGuaLiveApi:
|
|||||||
"""
|
"""
|
||||||
获取弹幕
|
获取弹幕
|
||||||
"""
|
"""
|
||||||
if not self.isValidRoom:
|
self.updRoomInfo()
|
||||||
self.updRoomInfo()
|
|
||||||
return
|
|
||||||
p = self.s.get("https://i.snssdk.com/videolive/im/get_msg?cursor={cursor}&room_id={roomID}"
|
p = self.s.get("https://i.snssdk.com/videolive/im/get_msg?cursor={cursor}&room_id={roomID}"
|
||||||
"&version_code=800&device_platform=android".format(
|
"&version_code=800&device_platform=android".format(
|
||||||
roomID=self.roomID,
|
roomID=self.roomID,
|
||||||
@ -351,7 +379,7 @@ class XiGuaLiveApi:
|
|||||||
self.onLottery(self.lottery)
|
self.onLottery(self.lottery)
|
||||||
self.lottery = None
|
self.lottery = None
|
||||||
# 2分钟自动更新下房间信息
|
# 2分钟自动更新下房间信息
|
||||||
self.updRoomInfo(len(d['data']) == 0)
|
self.updRoomInfo()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Reference in New Issue
Block a user