Skip to content

Commit

Permalink
feat: add jd live record
Browse files Browse the repository at this point in the history
  • Loading branch information
ihmily committed Dec 3, 2024
1 parent 80f88f6 commit 0c97d06
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 5 deletions.
1 change: 1 addition & 0 deletions config/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ huamao_cookie =
shopee_cookie =
youtube_cookie =
taobao_cookie =
jd_cookie =

[Authorization]
popkontv_token =
Expand Down
12 changes: 10 additions & 2 deletions demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,23 @@
"youtube": {
"url": "https://www.youtube.com/watch?v=cS6zS5hi1w0",
"func": spider.get_youtube_stream_url,
},
"taobao": {
"url": "https://m.tb.cn/h.TWp0HTd",
"func": spider.get_taobao_stream_url,
},
"jd": {
"url": "https://3.cn/28MLBy-E",
"func": spider.get_jd_stream_url,
}
}


def test_live_stream(platform_name: str, proxy_addr=None) -> None:
def test_live_stream(platform_name: str, proxy_addr=None, cookies=None) -> None:
if platform_name in LIVE_STREAM_CONFIG:
config = LIVE_STREAM_CONFIG[platform_name]
try:
stream_data = config['func'](config['url'], proxy_addr=proxy_addr)
stream_data = config['func'](config['url'], proxy_addr=proxy_addr, cookies=cookies)
logger.debug(f"Stream data for {platform_name}: {stream_data}")
except Exception as e:
logger.error(f"Error fetching stream data for {platform_name}: {e}")
Expand Down
64 changes: 64 additions & 0 deletions douyinliverecorder/spider.py
Original file line number Diff line number Diff line change
Expand Up @@ -3053,3 +3053,67 @@ def get_taobao_stream_url(url: str, proxy_addr: OptionalStr = None, cookies: Opt
else:
print('Error: Try to update cookie failed, please update the cookies in the configuration file')


@trace_error_decorator
def get_jd_stream_url(url: str, proxy_addr: OptionalStr = None, cookies: OptionalStr = None) -> dict:
headers = {
'User-Agent': 'ios/7.830 (ios 17.0; ; iPhone 15 (A2846/A3089/A3090/A3092))',
'origin': 'https://lives.jd.com',
'referer': 'https://lives.jd.com/',
'x-referer-page': 'https://lives.jd.com/',
}

if cookies:
headers['Cookie'] = cookies

redirect_url = get_req(url, proxy_addr=proxy_addr, headers=headers, redirect_url=True)
author_id = get_params(redirect_url, 'authorId')
result = {"anchor_name": '', "is_live": False}
if not author_id:
live_id = re.search('#/(.*?)\\?origin', redirect_url)
if not live_id:
return result
live_id = live_id.group(1)
result['anchor_name'] = f'jd_{live_id}'
else:
data = {
'functionId': 'talent_head_findTalentMsg',
'appid': 'dr_detail',
'body': '{"authorId":"' + author_id + '","monitorSource":"1","userId":""}',
}
info_api = 'https://api.m.jd.com/talent_head_findTalentMsg'
json_str = get_req(info_api, data=data, proxy_addr=proxy_addr, headers=headers)
json_data = json.loads(json_str)
anchor_name = json_data['result']['talentName']
result['anchor_name'] = anchor_name
if 'livingRoomJump' not in json_data['result']:
return result
live_id = json_data['result']['livingRoomJump']['params']['id']
params = {
"body": '{"liveId": "' + live_id + '"}',
"functionId": "getImmediatePlayToM",
"appid": "h5-live"
}

api = f'https://api.m.jd.com/client.action?{urllib.parse.urlencode(params)}'
# backup_api: https://api.m.jd.com/api
json_str = get_req(api, proxy_addr=proxy_addr, headers=headers)
json_data = json.loads(json_str)
live_status = json_data['data']['status']
if live_status == 1:
if author_id:
data = {
'functionId': 'jdTalentContentList',
'appid': 'dr_detail',
'body': '{"authorId":"' + author_id + '","type":1,"userId":"","page":1,"offset":"-1",'
'"monitorSource":"1","pageSize":1}',
}
json_str2 = get_req('https://api.m.jd.com/jdTalentContentList', data=data,
proxy_addr=proxy_addr, headers=headers)
json_data2 = json.loads(json_str2)
result['title'] = json_data2['result']['content'][0]['title']

flv_url = json_data['data']['videoUrl']
m3u8_url = json_data['data']['h5VideoUrl']
result |= {"is_live": True, "m3u8_url": m3u8_url, "flv_url": flv_url, "record_url": m3u8_url}
return result
15 changes: 12 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

version = "v4.0.2"
platforms = ("\n国内站点:抖音|快手|虎牙|斗鱼|YY|B站|小红书|bigo|blued|网易CC|千度热播|猫耳FM|Look|TwitCasting|百度|微博|"
"酷狗|花椒|流星|Acfun|畅聊|映客|音播|知乎|嗨秀|VV星球|17Live|浪Live|漂漂|六间房|乐嗨|花猫|淘宝"
"酷狗|花椒|流星|Acfun|畅聊|映客|音播|知乎|嗨秀|VV星球|17Live|浪Live|漂漂|六间房|乐嗨|花猫|淘宝|京东"
"\n海外站点:TikTok|SOOP|PandaTV|WinkTV|FlexTV|PopkonTV|TwitchTV|LiveMe|ShowRoom|CHZZK|Shopee|Youtube")

recording = set()
Expand Down Expand Up @@ -67,7 +67,7 @@
url_config_file = f'{script_path}/config/URL_config.ini'
backup_dir = f'{script_path}/backup_config'
text_encoding = 'utf-8-sig'
rstr = r"[\/\\\:\*\?\"\<\>\|&#.。,, ~!· ]"
rstr = r"[\/\\\:\*\??\"\<\>\|&#.。,, ~!· ]"
ffmpeg_path = f"{script_path}/ffmpeg.exe"
default_path = f'{script_path}/downloads'
os.makedirs(default_path, exist_ok=True)
Expand Down Expand Up @@ -868,6 +868,12 @@ def start_record(url_data: tuple, count_variable: int = -1) -> None:
url_type='all', hls_extra_key='hlsUrl', flv_extra_key='flvUrl'
)

elif record_url.find("3.cn") > -1 or record_url.find("m.jd.com") > -1:
platform = '京东直播'
with semaphore:
port_info = spider.get_jd_stream_url(
url=record_url, proxy_addr=proxy_address, cookies=jd_cookie)

elif record_url.find(".m3u8") > -1 or record_url.find(".flv") > -1:
platform = '自定义录制直播'
port_info = {
Expand Down Expand Up @@ -1692,6 +1698,7 @@ def read_config_value(config_parser: configparser.RawConfigParser, section: str,
shopee_cookie = read_config_value(config, 'Cookie', 'shopee_cookie', '')
youtube_cookie = read_config_value(config, 'Cookie', 'youtube_cookie', '')
taobao_cookie = read_config_value(config, 'Cookie', 'taobao_cookie', '')
jd_cookie = read_config_value(config, 'Cookie', 'jd_cookie', '')

video_save_type_list = ("FLV", "MKV", "TS", "MP4", "MP3音频", "M4A音频")
if video_save_type and video_save_type.upper() in video_save_type_list:
Expand Down Expand Up @@ -1808,6 +1815,8 @@ def contains_url(string: str) -> bool:
'h.catshow168.com',
'e.tb.cn',
'huodong.m.taobao.com',
'3.cn',
'eco.m.jd.com'
]
overseas_platform_host = [
'www.tiktok.com',
Expand Down Expand Up @@ -1910,4 +1919,4 @@ def contains_url(string: str) -> bool:
t2.start()
first_run = False

time.sleep(3)
time.sleep(3)

0 comments on commit 0c97d06

Please sign in to comment.