From 1baea2d4f33c9867eb41cd046c3215d109f233e0 Mon Sep 17 00:00:00 2001 From: Florian Staudacher Date: Thu, 22 Oct 2020 19:16:49 +0200 Subject: [PATCH] handle different syntax for boolean values in js_to_json --- test/test_utils.py | 6 ++++++ youtube_dl/utils.py | 8 +++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/test/test_utils.py b/test/test_utils.py index c2d1e4fb1..878ac6268 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -1000,6 +1000,12 @@ class TestUtil(unittest.TestCase): on = js_to_json('{ "040": "040" }') self.assertEqual(json.loads(on), {'040': '040'}) + on = js_to_json('{ "defaultQuality":!0}') + self.assertEqual(json.loads(on), {'defaultQuality': 'true'}) + + on = js_to_json('{ "onFullscreen":!1}') + self.assertEqual(json.loads(on), {'onFullscreen': 'false'}) + def test_js_to_json_malformed(self): self.assertEqual(js_to_json('42a1'), '42"a1"') self.assertEqual(js_to_json('42a-1'), '42"a"-1') diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py index 737e2810e..2abd807b7 100644 --- a/youtube_dl/utils.py +++ b/youtube_dl/utils.py @@ -4081,6 +4081,11 @@ def js_to_json(code): elif v.startswith('/*') or v.startswith('//') or v == ',': return "" + if v == "!0": + v = 'true' + if v == "!1": + v = 'false' + if v[0] in ("'", '"'): v = re.sub(r'(?s)\\.|"', lambda m: { '"': '\\"', @@ -4103,7 +4108,8 @@ def js_to_json(code): {comment}|,(?={skip}[\]}}])| (?:(?