diff --git a/src/you_get/common.py b/src/you_get/common.py index 0c0d720a..ff040992 100755 --- a/src/you_get/common.py +++ b/src/you_get/common.py @@ -12,8 +12,10 @@ from urllib import request, parse from .version import __version__ from .util import log from .util.strings import get_filename, unescape_html +from . import json_output as json_output_ dry_run = False +json_output = False force = False player = None extractor_proxy = None @@ -519,6 +521,9 @@ def get_output_filename(urls, title, ext, output_dir, merge): def download_urls(urls, title, ext, total_size, output_dir='.', refer=None, merge=True, faker=False): assert urls + if json_output: + json_output_.download_urls(urls=urls, title=title, ext=ext, total_size=total_size, refer=refer) + return if dry_run: print('Real URLs:\n%s' % '\n'.join(urls)) return @@ -724,6 +729,9 @@ def playlist_not_supported(name): return f def print_info(site_info, title, type, size): + if json_output: + json_output_.print_info(site_info=site_info, title=title, type=type, size=size) + return if type: type = type.lower() if type in ['3gp']: @@ -880,12 +888,12 @@ def script_main(script_name, download, download_playlist = None): global force global dry_run + global json_output global player global extractor_proxy global cookies_txt cookies_txt = None - json_output = False info_only = False playlist = False merge = True @@ -912,8 +920,8 @@ def script_main(script_name, download, download_playlist = None): elif o in ('--json', ): json_output = True # to fix extractors not use VideoExtractor - info_only = True dry_run = True + info_only = False elif o in ('-c', '--cookies'): from http import cookiejar cookies_txt = cookiejar.MozillaCookieJar(a) diff --git a/src/you_get/json_output.py b/src/you_get/json_output.py index fd773c7e..86a42abc 100644 --- a/src/you_get/json_output.py +++ b/src/you_get/json_output.py @@ -1,6 +1,9 @@ import json +# save info from common.print_info() +last_info = None + def output(video_extractor, pretty_print=True): ve = video_extractor out = {} @@ -13,3 +16,30 @@ def output(video_extractor, pretty_print=True): else: print(json.dumps(out)) +# a fake VideoExtractor object to save info +class VideoExtractor(object): + pass + +def print_info(site_info=None, title=None, type=None, size=None): + global last_info + # create a VideoExtractor and save info for download_urls() + ve = VideoExtractor() + last_info = ve + ve.name = site_info + ve.title = title + ve.url = None + +def download_urls(urls=None, title=None, ext=None, total_size=None, refer=None): + ve = last_info + # save download info in streams + stream = {} + stream['container'] = ext + stream['size'] = total_size + stream['src'] = urls + if refer: + stream['refer'] = refer + stream['video_profile'] = '__default__' + ve.streams = {} + ve.streams['__default__'] = stream + output(ve) +