aboutsummaryrefslogtreecommitdiff
path: root/youtube_dl/utils.py
diff options
context:
space:
mode:
authorPhilipp Hagemeister <phihag@phihag.de>2014-08-22 02:33:29 +0200
committerPhilipp Hagemeister <phihag@phihag.de>2014-08-22 02:33:29 +0200
commite05f693942b0dacecc60b987a79859ee62b58ab9 (patch)
treece88a30bd33ab7e669fc655616ebf172e56f6de7 /youtube_dl/utils.py
parentb27295d2ab6e9e720a1ed26477841ddb224848bf (diff)
[patreon] Simplify (#3390)
Diffstat (limited to 'youtube_dl/utils.py')
-rw-r--r--youtube_dl/utils.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py
index f8ec5389f..42ad520f9 100644
--- a/youtube_dl/utils.py
+++ b/youtube_dl/utils.py
@@ -1474,6 +1474,34 @@ def strip_jsonp(code):
return re.sub(r'(?s)^[a-zA-Z0-9_]+\s*\(\s*(.*)\);?\s*?\s*$', r'\1', code)
+def js_to_json(code):
+ def fix_kv(m):
+ key = m.group(2)
+ if key.startswith("'"):
+ assert key.endswith("'")
+ assert '"' not in key
+ key = '"%s"' % key[1:-1]
+ elif not key.startswith('"'):
+ key = '"%s"' % key
+
+ value = m.group(4)
+ if value.startswith("'"):
+ assert value.endswith("'")
+ assert '"' not in value
+ value = '"%s"' % value[1:-1]
+
+ return m.group(1) + key + m.group(3) + value
+
+ res = re.sub(r'''(?x)
+ ([{,]\s*)
+ ("[^"]*"|\'[^\']*\'|[a-z0-9A-Z]+)
+ (:\s*)
+ ([0-9.]+|true|false|"[^"]*"|\'[^\']*\'|\[|\{)
+ ''', fix_kv, code)
+ res = re.sub(r',(\s*\])', lambda m: m.group(1), res)
+ return res
+
+
def qualities(quality_ids):
""" Get a numeric quality value out of a list of possible values """
def q(qid):