This commit is contained in:
root
2021-01-12 09:43:38 +08:00
2 changed files with 67 additions and 40 deletions

View File

@ -305,7 +305,7 @@ def loginBilibili(force=False):
if os.path.exists('cookie'):
try:
with open('cookie', 'r', encoding='utf8') as f:
_cookie = f.read(4096)
_cookie = f.readline().strip()
b = Bilibili(_cookie)
loginTime = datetime.now()
appendOperation("Cookie 登录")

105
api.py
View File

@ -6,24 +6,58 @@ import time
from datetime import datetime, timedelta
DEBUG = False
# 自己抓的自己设备的参数,建议开发者自己抓一个长期使用
# 如果有大佬破解初次激活设备时的数据也行,可以自己生成一堆用
CUSTOM_INFO = {
'iid': "96159232732",
'device_id': "55714661189",
'cdid': "ed4295e8-5d9a-4cb9-b2a2-04009a3baa2d",
'openudid': "70d6668d41512c39",
# 'aid': "32", # 又是一个不变的值
'channel': "xiaomi",
'device_brand': "Xiaomi",
'device_type': "MI+8+SE",
'os_api': "28",
'os_version': "9",
'rom_version': "miui_V12_V12.0.2.0.QEBCNXM",
}
VERSION_INFO = {
'app_name': "video_article",
'version_code': "926",
'version_code_full': "92609",
'version_name': "9.2.6",
'ab_version': "941090,785218,668858,1046292,1073579,830454,956074,929436,797199,1135476,1179370,994679,959010,"
"900042,1113833,668854,1193963,901277,1043330,1038721,994822,1002058,1230687,1189797,1143356,1143441,"
"1143501,1143698,1143713,1371009,1243997,1392586,1395695,1395486,1398858,668852,668856,668853,"
"1186421,668851,668859,999124,668855,1039075",
'manifest_version_code': "518",
'tma_jssdk_version': "1830001",
# 'oaid': "a625f466e0975d42", # 一个定值,几个版本换设备都没变过
}
COMMON_GET_PARAM = (
"&iid=96159232732&device_id=55714661189&channel=xiaomi&aid=32&app_name=video_article&version_code=844"
"&version_name=8.4.4&device_platform=android&ab_version=668855,1640207,1652557,1143698,1043330,1143713,"
"1477978,1189797,1635895,1631832,994822,900042,956074,1143356,1046292,1481027,929436,994679,1419059,"
"1073579,668854,1143441,668852,668853,941090,668858,668851,668859,668856,"
"1639440,1630487&device_type=MI+8+SE&device_brand=Xiaomi&language=zh"
"&os_api=28&os_version=9&openudid=70d6668d41512c39&manifest_version_code=444&update_version_code=84407"
"&_rticket={TIMESTAMP:.0f}&cdid_ts={TIMESTAMP:.0f}&fp=a_fake_fp&tma_jssdk_version=1.53.0.5"
"&cdid=ed4295e8-5d9a-4cb9-b2a2-04009a3baa2d&oaid=a625f466e0975d42")
"&iid={iid}&device_id={device_id}&channel={channel}&aid=32&app_name={app_name}&version_code={version_code}&"
"version_name={version_name}&device_platform=android&ab_version={ab_version}&device_type={device_type}&"
"device_brand={device_brand}&language=zh&os_api={os_api}&os_version={os_version}&openudid={openudid}&fp=a_fake_fp&"
"manifest_version_code={manifest_version_code}&update_version_code={version_code_full}&_rticket={{TIMESTAMP:.0f}}&"
"_rticket={{TIMESTAMP:.0f}}&cdid_ts={{TIMESTAMP:.0f}}&tma_jssdk_version={tma_jssdk_version}&"
"rom_version={rom_version}&cdid={cdid}&oaid=a625f466e0975d42").format_map({**VERSION_INFO, **CUSTOM_INFO})
SEARCH_USER_API = (
"https://search-hl.ixigua.com/video/app/search/search_content/?format=json"
"&fss=&keyword_type=input&offset=0&count=10&search_sug=0&forum=0&is_native_req=0"
"&fss=search_subtab_switch&target_channel=video_search&keyword_type=search_subtab_switch&offset=0&count=10"
"&search_sug=1&forum=1&is_native_req=0&m_tab=video&pd=user&tab=user&_s_tma=SEARCH_STANDARD.list.fe_get_data"
'&_s_page_sub_route=/&_s_ec={{"filterDataType":[],"reserveFilterBar":true}}&__use_xigua_native_bridge_fetch__=1'
'&ab_param={{"is_show_filter_feature": 1, "is_hit_new_ui": 1}}'
"&search_start_time={TIMESTAMP:.0f}&from=live&en_qc=1&pd=xigua_live&ssmix=a{COMMON}&keyword={keyword}")
USER_INFO_API = "https://api3-normal-c-hl.ixigua.com/video/app/user/home/v7/?to_user_id={userId}{COMMON}"
ROOM_INFO_API = "https://webcast3.ixigua.com/webcast/room/enter/?room_id={roomId}&pack_level=4{COMMON}"
USER_INFO_API = "https://api100-quic-c-hl.ixigua.com/video/app/user/home/v7/?to_user_id={userId}{COMMON}"
ROOM_INFO_API = ("https://webcast3-normal-c-hl.ixigua.com/webcast/room/enter/?room_id={roomId}&webcast_sdk_version=1350"
"&webcast_language=zh&webcast_locale=zh_CN&pack_level=4{COMMON}")
COMMON_HEADERS = {
"sdk-version": '1',
"User-Agent": "Dalvik/2.1.0 (Linux; U; Android 9) VideoArticle/8.1.6 cronet/TTNetVersion:b97574c0 2019-09-24",
"sdk-version": '2',
"passport-sdk-version": "19",
"X-SS-DP": "32",
"User-Agent": "Dalvik/2.1.0 (Linux; U; Android 10) VideoArticle/9.2.6 cronet/TTNetVersion:828f6f3c 2020-09-06 "
"QuicVersion:7aee791b 2020-06-05",
# 最好别加br,requests库好像自带没法解析
"Accept-Encoding": "gzip, deflate"
}
@ -40,6 +74,7 @@ class XiGuaLiveApi:
name = "永恒de草薙"
self.broadcaster = None
self.isValidUser = False
self.name = str(name)
if type(name) == User:
self.broadcaster = name
self.name = name.name
@ -52,26 +87,12 @@ class XiGuaLiveApi:
self.isLive = False
self._rawRoomInfo = {}
self.roomID = 0
self.roomPopularity = 0
self._cursor = "0"
self.lottery = None
self.s = requests.session()
self.s.headers.update(COMMON_HEADERS)
self._updRoomAt = datetime.fromtimestamp(0)
self.updRoomInfo()
def _updateRoomPopularity(self, _data):
"""
更新房间人气的方法
Update Room Popularity
:param _data: Received Message
"""
if "extra" in _data:
if "member_count" in _data["extra"] and _data["extra"]["member_count"] > 0:
self.roomPopularity = _data["extra"]["member_count"]
if "data" in _data:
if "popularity" in _data["data"]:
self.roomPopularity = _data["data"]["popularity"]
self._ext = ""
self._cursor = "0"
def getJson(self, url, **kwargs):
if "timeout" not in kwargs:
@ -100,7 +121,7 @@ class XiGuaLiveApi:
if "timeout" not in kwargs:
kwargs["timeout"] = 10
try:
p = self.s.post(url, data, **kwargs)
p = self.s.post(url, data=data, **kwargs)
except Exception as e:
print("网络请求失败")
if DEBUG:
@ -142,20 +163,23 @@ class XiGuaLiveApi:
compare = self.broadcaster
if self.name is None or compare is None:
return False
return self.name == compare.__str__() or compare.__str__() in self.name or self.name in compare.__str__()
return self.name == compare.__str__() or compare.__repr__() in self.name or self.name in compare.__repr__()
def _forceSearchUser(self):
"""
搜索主播名
:return:
"""
_formatData = {"COMMON": COMMON_GET_PARAM, "TIMESTAMP": time.time() * 1000, "keyword": self.name}
_url = SEARCH_USER_API.format_map(_formatData).format_map(_formatData)
_formatData = {"TIMESTAMP": time.time() * 1000, "keyword": self.name}
_COMMON = COMMON_GET_PARAM.format_map(_formatData)
_formatData['COMMON'] = _COMMON
_url = SEARCH_USER_API.format_map(_formatData)
d = self.getJson(_url)
if d is None:
print("搜索接口请求失败")
return False
self.broadcaster = None
self.isValidUser = False
if "data" in d and d["data"] is not None:
for i in d["data"]:
if self.broadcaster is not None:
@ -181,11 +205,13 @@ class XiGuaLiveApi:
获取用户信息
:return:
"""
self.isValidUser = False
if self.broadcaster is None:
self.isValidUser = False
return False
_formatData = {"COMMON": COMMON_GET_PARAM, "TIMESTAMP": time.time() * 1000, "userId": self.broadcaster.ID}
_url = USER_INFO_API.format_map(_formatData).format_map(_formatData)
_formatData = {"TIMESTAMP": time.time() * 1000, "userId": self.broadcaster.ID}
_COMMON = COMMON_GET_PARAM.format_map(_formatData)
_formatData['COMMON'] = _COMMON
_url = USER_INFO_API.format_map(_formatData)
d = self.getJson(_url)
if d is None:
print("获取用户信息失败")
@ -218,8 +244,10 @@ class XiGuaLiveApi:
return False
if (self._updRoomAt + timedelta(minutes=3) > datetime.now()) and not force:
return self.isLive
_formatData = {"COMMON": COMMON_GET_PARAM, "TIMESTAMP": time.time() * 1000, "roomId": self.roomID}
_url = ROOM_INFO_API.format_map(_formatData).format_map(_formatData)
_formatData = {"TIMESTAMP": time.time() * 1000, "roomId": self.roomID}
_COMMON = COMMON_GET_PARAM.format_map(_formatData)
_formatData['COMMON'] = _COMMON
_url = ROOM_INFO_API.format_map(_formatData)
d = self.getJson(_url)
if d is None:
print("获取房间信息接口请求失败")
@ -230,7 +258,6 @@ class XiGuaLiveApi:
self._rawRoomInfo = d["data"]
self.isLive = d["data"]["status"] == 2
self._updRoomAt = datetime.now()
self._updateRoomPopularity(d)
return self.isLive
def updRoomInfo(self, force=False):