fix: update xhs stream url rule

This commit is contained in:
ihmily 2025-01-23 17:14:20 +08:00
parent 4181e9745a
commit c9f26b116d
2 changed files with 58 additions and 19 deletions

View File

@ -4,7 +4,7 @@
Author: Hmily
GitHub: https://github.com/ihmily
Date: 2023-07-15 23:15:00
Update: 2024-11-30 18:46:16
Update: 2025-01-23 15:48:16
Copyright (c) 2023-2024 by Hmily, All Rights Reserved.
Function: Get live stream data.
"""
@ -818,19 +818,34 @@ def get_xhs_stream_url(url: str, proxy_addr: OptionalStr = None, cookies: Option
result = {"anchor_name": '', "is_live": False}
flv_url = ''
room_id = re.search('/livestream/(.*?)(?=/|\\?|$)', url)
host_id = get_params(url, 'host_id')
if room_id:
room_id = room_id.group(1)
api = f'https://www.xiaohongshu.com/api/sns/red/live/app/v1/ecology/outside/share_info?room_id={room_id}'
# api = f'https://www.redelight.cn/api/sns/red/live/app/v1/ecology/outside/share_info?room_id={room_id}'
json_str = get_req(api, proxy_addr=proxy_addr, headers=headers)
html_str = get_req(url, proxy_addr=proxy_addr, headers=headers)
json_str = re.search('window.__INITIAL_STATE__=(.*?)</script>', html_str, re.S).group(1)
json_data = json.loads(json_str)
anchor_name = json_data['data']['host_info']['nickname']
live_title = json_data['data']['room']['name']
flv_url = f'http://live-play.xhscdn.com/live/{room_id}.flv'
result |= {"anchor_name": anchor_name, "title": live_title, "flv_url": flv_url, 'record_url': flv_url}
live_title = json_data['liveStream']['roomData']['roomInfo']['roomTitle']
anchor_name = json_data['liveStream']['roomData']['hostInfo']['nickName']
play_data = json.loads(json_data['liveStream']['roomData']['roomInfo']['pullConfig'])
m3u8_url_list = []
flv_url_list = []
for i in play_data['h264']:
play_url = i['master_url']
if play_url.endswith('.m3u8'):
m3u8_url_list.append(play_url)
else:
flv_url_list.append(play_url)
flv_url = flv_url_list[0]
m3u8_url = m3u8_url_list[0]
result |= {
"anchor_name": anchor_name,
"title": live_title,
"flv_url": flv_url,
"m3u8_url": m3u8_url,
'record_url': m3u8_url if m3u8_url else flv_url
}
user_id = re.search('/user/profile/(.*?)(?=/|\\?|$)', url)
user_id = user_id.group(1) if user_id else get_params(url, 'host_id')
user_id = user_id.group(1) if user_id else host_id
if user_id:
params = {
'user_id_list': user_id,
@ -843,11 +858,27 @@ def get_xhs_stream_url(url: str, proxy_addr: OptionalStr = None, cookies: Option
if json_data['data']:
live_link = json_data['data'][0]['live_link']
anchor_name = get_params(live_link, "host_nickname")
if flv_url and get_response_status(flv_url, proxy_addr=proxy_addr, headers=headers):
if flv_url and get_response_status(flv_url, proxy_addr=proxy_addr, headers=headers, timeout=5):
result['is_live'] = True
return result
flv_url = get_params(live_link, "flvUrl")
result |= {"anchor_name": anchor_name, "is_live": True, "flv_url": flv_url, 'record_url': flv_url}
room_id = flv_url.split('live/')[1].split('.')[0]
flv_url = f'http://live-source-play.xhscdn.com/live/{room_id}.flv'
m3u8_url = flv_url.replace('.flv', '.m3u8')
result |= {
"anchor_name": anchor_name,
"is_live": True,
"flv_url": flv_url,
"m3u8_url": m3u8_url,
'record_url': flv_url
}
else:
html_str = get_req(url, proxy_addr=proxy_addr, headers=headers)
json_str = re.search('window.__INITIAL_STATE__=(.*?)</script>', html_str, re.S).group(1)
json_data = json.loads(json_str)
anchor_name = json_data['profile']['userInfo']['nickname']
result['anchor_name'] = anchor_name
else:
print(f"xhs {json_data['msg']}")
return result

22
main.py
View File

@ -4,11 +4,10 @@
Author: Hmily
GitHub: https://github.com/ihmily
Date: 2023-07-17 23:52:05
Update: 2024-11-30 23:35:00
Update: 2025-01-23 17:09:00
Copyright (c) 2023-2024 by Hmily, All Rights Reserved.
Function: Record live stream video.
"""
import os
import sys
import builtins
@ -365,7 +364,7 @@ def check_subprocess(record_name: str, record_url: str, ffmpeg_command: list, sa
script_command: str | None = None) -> bool:
save_file_path = ffmpeg_command[-1]
process = subprocess.Popen(
ffmpeg_command, stderr=subprocess.STDOUT, startupinfo=get_startup_info(os_type)
ffmpeg_command, stdin=subprocess.PIPE, stderr=subprocess.STDOUT, startupinfo=get_startup_info(os_type)
)
subs_file_path = save_file_path.rsplit('.', maxsplit=1)[0]
@ -381,7 +380,13 @@ def check_subprocess(record_name: str, record_url: str, ffmpeg_command: list, sa
if record_url in url_comments or exit_recording:
color_obj.print_colored(f"[{record_name}]录制时已被注释,本条线程将会退出", color_obj.YELLOW)
clear_record_info(record_name, record_url)
process.terminate()
# process.terminate()
if os.name == 'nt':
if process.stdin:
process.stdin.write(b'q')
process.stdin.close()
else:
process.send_signal(signal.SIGINT)
process.wait()
return True
time.sleep(1)
@ -1078,7 +1083,7 @@ def start_record(url_data: tuple, count_variable: int = -1) -> None:
only_flv_record = False
only_flv_platform_list = ['shopee', '花椒直播']
if 'live.xhscdn.com' in real_url or platform in only_flv_platform_list:
if platform in only_flv_platform_list:
logger.debug(f"提示: {platform} 将强制使用FLV格式录制")
only_flv_record = True
@ -1117,7 +1122,7 @@ def start_record(url_data: tuple, count_variable: int = -1) -> None:
error_window.append(1)
try:
if converts_to_mp4 and 'live.xhscdn.com' not in real_url:
if converts_to_mp4:
seg_file_path = f"{full_path}/{anchor_name}_{title_in_name}{now}_%03d.mp4"
if split_video_by_time:
segment_video(
@ -1379,6 +1384,9 @@ def start_record(url_data: tuple, count_variable: int = -1) -> None:
custom_script
)
if comment_end:
threading.Thread(
target=converts_mp4, args=(save_file_path, delete_origin_file)
).start()
return
except subprocess.CalledProcessError as e:
@ -1919,4 +1927,4 @@ while True:
t2.start()
first_run = False
time.sleep(3)
time.sleep(3)