Add code to write info.json output to stdout when option -output is used with a dash: -o -
This commit is contained in:
parent
92f618f2e2
commit
48af4b55b3
@ -67,6 +67,26 @@ class TestInfoJSON(unittest.TestCase):
|
||||
descr = descf.read()
|
||||
self.assertEqual(descr, EXPECTED_DESCRIPTION)
|
||||
|
||||
def test_info_json_with_stdout_as_output_returns_good_json(self):
|
||||
from unittest.mock import patch
|
||||
from io import StringIO
|
||||
params['writedescription'] = False
|
||||
params['quiet'] = True
|
||||
params['outtmpl'] = '-'
|
||||
with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
|
||||
ie = youtube_dl.extractor.YoutubeIE()
|
||||
ydl = YoutubeDL(params)
|
||||
ydl.add_info_extractor(ie)
|
||||
ydl.download([TEST_ID])
|
||||
output = mock_stdout.getvalue()
|
||||
jd = json.loads(output)
|
||||
self.assertEqual(jd['upload_date'], u'20121002')
|
||||
self.assertEqual(jd['description'], EXPECTED_DESCRIPTION)
|
||||
self.assertEqual(jd['id'], TEST_ID)
|
||||
self.assertEqual(jd['extractor'], 'youtube')
|
||||
self.assertEqual(jd['title'], u'''youtube-dl test video "'/\ä↭𝕐''')
|
||||
self.assertEqual(jd['uploader'], 'Philipp Hagemeister')
|
||||
|
||||
def tearDown(self):
|
||||
if os.path.exists(INFO_JSON_FILE):
|
||||
os.remove(INFO_JSON_FILE)
|
||||
|
@ -200,14 +200,21 @@ else:
|
||||
|
||||
# In Python 2.x, json.dump expects a bytestream.
|
||||
# In Python 3.x, it writes to a character stream
|
||||
if sys.version_info < (3,0):
|
||||
# if user passed '-' as outputname, redirect output to stdout
|
||||
if sys.version_info < (3, 0):
|
||||
def write_json_file(obj, fn):
|
||||
with open(fn, 'wb') as f:
|
||||
json.dump(obj, f)
|
||||
if fn.rstrip('.info.json') == u'-':
|
||||
json.dump(obj, sys.stdout)
|
||||
else:
|
||||
with open(fn, 'wb') as f:
|
||||
json.dump(obj, f)
|
||||
else:
|
||||
def write_json_file(obj, fn):
|
||||
with open(fn, 'w', encoding='utf-8') as f:
|
||||
json.dump(obj, f)
|
||||
if fn.rstrip('.info.json') == u'-':
|
||||
json.dump(obj, sys.stdout)
|
||||
else:
|
||||
with open(fn, 'w', encoding='utf-8') as f:
|
||||
json.dump(obj, f)
|
||||
|
||||
if sys.version_info >= (2,7):
|
||||
def find_xpath_attr(node, xpath, key, val):
|
||||
|
Loading…
x
Reference in New Issue
Block a user