From 1ab8ea015de66af75d090b0fd24403e80052122c Mon Sep 17 00:00:00 2001 From: MaxwellGoblin Date: Sat, 15 Jul 2017 21:44:54 +0800 Subject: [PATCH] [common zhanqi]m3u8 code in common; rewrite zhanqi --- src/you_get/common.py | 30 +++++++++++-- src/you_get/extractors/zhanqi.py | 77 +++++++++++++++----------------- 2 files changed, 63 insertions(+), 44 deletions(-) diff --git a/src/you_get/common.py b/src/you_get/common.py index 9d688e09..2462bc85 100755 --- a/src/you_get/common.py +++ b/src/you_get/common.py @@ -161,6 +161,22 @@ def rc4(key, data): out_list.append(char ^ prn) return bytes(out_list) + +def general_m3u8_extractor(url): + path_len = len(url.split('/')[-1]) + base_url = url[:-path_len] + + m3u8_list = get_content(url).split('\n') + urls = [] + for line in m3u8_list: + line = line.strip() + if line and not line.startswith('#'): + if line.startswith('http'): + urls.append(line) + else: + urls.append(base_url + line) + return urls + def maybe_print(*s): try: print(*s) except: pass @@ -1027,7 +1043,7 @@ def playlist_not_supported(name): raise NotImplementedError('Playlist is not supported for ' + name) return f -def print_info(site_info, title, type, size): +def print_info(site_info, title, type, size, **kwargs): if json_output: json_output_.print_info(site_info=site_info, title=title, type=type, size=size) return @@ -1092,14 +1108,22 @@ def print_info(site_info, title, type, size): type_info = "Portable Network Graphics (%s)" % type elif type in ['image/gif']: type_info = "Graphics Interchange Format (%s)" % type - + elif type in ['m3u8']: + if 'm3u8_type' in kwargs: + if kwargs['m3u8_type'] == 'master': + type_info = 'M3U8 Master {}'.format(type) + else: + type_info = 'M3U8 Playlist {}'.format(type) else: type_info = "Unknown type (%s)" % type maybe_print("Site: ", site_info) maybe_print("Title: ", unescape_html(tr(title))) print("Type: ", type_info) - print("Size: ", round(size / 1048576, 2), "MiB (" + str(size) + " Bytes)") + if type != 'm3u8': + print("Size: ", round(size / 1048576, 2), "MiB (" + str(size) + " Bytes)") + if type == 'm3u8' and 'm3u8_url' in kwargs: + print('M3U8 Url: {}'.format(kwargs['m3u8_url'])) print() def mime_to_container(mime): diff --git a/src/you_get/extractors/zhanqi.py b/src/you_get/extractors/zhanqi.py index f2c673ca..d0bbddb6 100644 --- a/src/you_get/extractors/zhanqi.py +++ b/src/you_get/extractors/zhanqi.py @@ -4,53 +4,48 @@ __all__ = ['zhanqi_download'] from ..common import * import json +import base64 +from urllib.parse import urlparse def zhanqi_download(url, output_dir = '.', merge = True, info_only = False, **kwargs): - host_name = url.split('/')[2] - first_folder_path = url.split('/')[3].split('?')[0] - - if first_folder_path != 'videos': #url = "https://www.zhanqi.tv/huashan?param_s=1_0.2.0" - if first_folder_path == 'topic': #https://www.zhanqi.tv/topic/lyingman - first_folder_path = url.split('/')[4].split('?')[0] - api_url = "https://www.zhanqi.tv/api/static/v2.1/room/domain/" + first_folder_path + ".json" - api_json = json.loads(get_html(api_url)) - data = api_json['data'] - status = data['status'] - if status != '4': - raise ValueError ("The live stream is not online!") - - nickname = data['nickname'] - title = nickname + ": " + data['title'] - - roomid = data['id'] - videoId = data['videoId'] - jump_url = "http://wshdl.load.cdn.zhanqi.tv/zqlive/" + videoId + ".flv?get_url=1" - jump_url = jump_url.strip('\r\n') - - real_url = get_html(jump_url) - real_url = real_url.strip('\r\n') - site_info = "www.zhanqi.tv" - - print_info(site_info, title, 'flv', float('inf')) - if not info_only: - download_url_ffmpeg(real_url, title, 'flv', {}, output_dir = output_dir, merge = merge) + path = urlparse(url).path[1:] + if not path.startswith('videos'): #url = "https://www.zhanqi.tv/huashan?param_s=1_0.2.0" + path_list = path.split('/') + room_id = path_list[1] if path_list[0] == 'topic' else path_list[0] + zhanqi_live(room_id, merge=merge, output_dir=output_dir, info_only=info_only, **kwargs) else: #url = 'https://www.zhanqi.tv/videos/Lyingman/2017/01/182308.html' - video_id = url.split('/')[-1].split('?')[0].split('.')[0] - assert video_id - api_url = "https://www.zhanqi.tv/api/static/v2.1/video/" + video_id + ".json" - api_json = json.loads(get_html(api_url)) - data = api_json['data'] + video_id = path.split('.')[0].split('/')[-1] + zhanqi_video(video_id, merge=merge, output_dir=output_dir, info_only=info_only, **kwargs) - title = data['title'] +def zhanqi_live(room_id, merge=True, output_dir='.', info_only=False, **kwargs): + api_url = "https://www.zhanqi.tv/api/static/v2.1/room/domain/{}.json".format(room_id) + json_data = json.loads(get_content(api_url))['data'] + status = json_data['status'] + if status != '4': + raise Exception("The live stream is not online!") - video_url_id = data['flashvars']['VideoID'] - real_url = "http://dlvod.cdn.zhanqi.tv/" + video_url_id - site_info = "www.zhanqi.tv/videos" + nickname = json_data['nickname'] + title = nickname + ": " + json_data['title'] + video_levels = base64.b64decode(json_data['flashvars']['VideoLevels']).decode('utf8') + m3u8_url = json.loads(video_levels)['streamUrl'] - print_info(site_info, title, 'flv', float('inf')) - if not info_only: - download_url_ffmpeg(real_url, title, 'flv', {}, output_dir = output_dir, merge = merge) + print_info(site_info, title, 'm3u8', 0, m3u8_url=m3u8_url, m3u8_type='master') + if not info_only: + download_url_ffmpeg(m3u8_url, title, 'mp4', output_dir=output_dir, merge=merge) +def zhanqi_video(video_id, output_dir='.', info_only=False, merge=True, **kwargs): + api_url = 'https://www.zhanqi.tv/api/static/v2.1/video/{}.json'.format(video_id) + json_data = json.loads(get_content(api_url))['data'] + + title = json_data['title'] + vid = json_data['flashvars']['VideoID'] + m3u8_url = 'http://dlvod.cdn.zhanqi.tv/' + vid + urls = general_m3u8_extractor(m3u8_url) + print_info(site_info, title, 'm3u8', 0) + if not info_only: + download_urls(urls, title, 'ts', 0, output_dir=output_dir, merge=merge, **kwargs) + +site_info = "www.zhanqi.tv" download = zhanqi_download -download_playlist = playlist_not_supported('zhanqi') \ No newline at end of file +download_playlist = playlist_not_supported('zhanqi')