perf: support weibo live record by anthor page url

This commit is contained in:
ihmily 2024-06-11 22:26:18 +08:00
parent 4275352254
commit b59b9b109c
2 changed files with 38 additions and 23 deletions

View File

@ -4,7 +4,7 @@
Author: Hmily
GitHub: https://github.com/ihmily
Date: 2023-07-17 23:52:05
Update: 2024-06-10 23:20:11
Update: 2024-06-11 22:21:00
Copyright (c) 2023-2024 by Hmily, All Rights Reserved.
Function: Record live stream video.
"""
@ -346,8 +346,8 @@ def get_tiktok_stream_url(json_data: dict, video_quality: str) -> Dict[str, Any]
stream_data = json.loads(stream_data).get('data', {})
flv_url_list = get_video_quality_url(stream_data, 'flv')
m3u8_url_list = get_video_quality_url(stream_data, 'hls')
for item in flv_url_list:
print(f"FLV URL: {item['url']}, VBitrate: {item['vbitrate']} Resolution: {item['resolution']}")
# for item in flv_url_list:
# print(f"FLV URL: {item['url']}, VBitrate: {item['vbitrate']} Resolution: {item['resolution']}")
while len(flv_url_list) < 5:
flv_url_list.append(flv_url_list[-1])

View File

@ -4,7 +4,7 @@
Author: Hmily
GitHub: https://github.com/ihmily
Date: 2023-07-15 23:15:00
Update: 2024-06-10 23:08:37
Update: 2024-06-11 22:16:51
Copyright (c) 2023 by Hmily, All Rights Reserved.
Function: Get live stream data.
"""
@ -1620,34 +1620,49 @@ def get_baidu_stream_data(url: str, proxy_addr: Union[str, None] = None, cookies
@trace_error_decorator
def get_weibo_stream_url(url: str, proxy_addr: Union[str, None] = None, cookies: Union[str, None] = None) -> \
Dict[str, Any]:
headers = {
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
'Connection': 'keep-alive',
'Referer': 'https://live.baidu.com/',
'User-Agent': 'Mozilla/5.0 (Linux; Android 8.0.0; SM-G955U Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Mobile Safari/537.36 Edg/121.0.0.0',
'Cookie': 'XSRF-TOKEN=qAP-pIY5V4tO6blNOhA4IIOD; SUB=_2AkMRNMCwf8NxqwFRmfwWymPrbI9-zgzEieKnaDFrJRMxHRl-yT9kqmkhtRB6OrTuX5z9N_7qk9C3xxEmNR-8WLcyo2PM; SUBP=0033WrSXqPxfM72-Ws9jqgMF55529P9D9WWemwcqkukCduUO11o9sBqA; WBPSESS=Wk6CxkYDejV3DDBcnx2LOXN9V1LjdSTNQPMbBDWe4lO2HbPmXG_coMffJ30T-Avn_ccQWtEYFcq9fab1p5RR6PEI6w661JcW7-56BszujMlaiAhLX-9vT4Zjboy1yf2l',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36',
}
if cookies:
headers['Cookie'] = cookies
room_id = url.split('?')[0].split('show/')[1]
room_id = ''
if 'show/' in url:
room_id = url.split('?')[0].split('show/')[1]
else:
uid = url.split('?')[0].rsplit('/u/', maxsplit=1)[1]
web_api = f'https://weibo.com/ajax/statuses/mymblog?uid={uid}&page=1&feature=0'
json_str = get_req(web_api, proxy_addr=proxy_addr, headers=headers)
json_data = json.loads(json_str)
for i in json_data['data']['list']:
if 'page_info' in i and i['page_info']['object_type'] == 'live':
room_id = i['page_info']['object_id']
print(room_id)
break
app_api = f'https://weibo.com/l/pc/anchor/live?live_id={room_id}'
# app_api = f'https://weibo.com/l/!/2/wblive/room/show_pc_live.json?live_id={room_id}'
json_str = get_req(url=app_api, proxy_addr=proxy_addr, headers=headers)
json_data = json.loads(json_str)
anchor_name = json_data['data']['user_info']['name']
result = {
"anchor_name": anchor_name,
"anchor_name": '',
"is_live": False,
}
live_status = json_data['data']['item']['status']
if live_status == 1:
result["is_live"] = True
play_url_list = json_data['data']['item']['stream_info']['pull']
result['m3u8_url'] = play_url_list['live_origin_hls_url']
result['flv_url'] = play_url_list['live_origin_flv_url']
result['record_url'] = play_url_list['live_origin_hls_url']
if room_id:
app_api = f'https://weibo.com/l/pc/anchor/live?live_id={room_id}'
# app_api = f'https://weibo.com/l/!/2/wblive/room/show_pc_live.json?live_id={room_id}'
json_str = get_req(url=app_api, proxy_addr=proxy_addr, headers=headers)
json_data = json.loads(json_str)
anchor_name = json_data['data']['user_info']['name']
result["anchor_name"] = anchor_name
live_status = json_data['data']['item']['status']
if live_status == 1:
result["is_live"] = True
play_url_list = json_data['data']['item']['stream_info']['pull']
result['m3u8_url'] = play_url_list['live_origin_hls_url']
result['flv_url'] = play_url_list['live_origin_flv_url']
result['record_url'] = play_url_list['live_origin_hls_url']
return result
@ -1997,7 +2012,7 @@ if __name__ == '__main__':
# room_url = 'https://www.popkontv.com/channel/notices?mcid=wjfal007&mcPartnerCode=P-00117' # popkontv
# room_url = 'https://twitcasting.tv/c:uonq' # TwitCasting
# room_url = 'https://live.baidu.com/m/media/pclive/pchome/live.html?room_id=9175031377&tab_category' # 百度直播
# room_url = 'https://weibo.com/l/wblive/p/show/1022:2321325026370190442592' # 微博直播
# room_url = 'https://weibo.com/u/7849520225' # 微博直播
# room_url = 'https://fanxing2.kugou.com/50428671?refer=2177&sourceFrom=' # 酷狗直播
# room_url = 'https://www.twitch.tv/gamerbee' # TwitchTV
# room_url = 'https://www.liveme.com/zh/v/17141937295821012854/index.html' # LiveMe