[utils] Add ability to control skipping false values in dict_get
[youtube-dl] / youtube_dl / utils.py
index 18dbe28bb5ecbcdb922f292490a43ccf6662ac9b..f3b0180abb050173099e03ccde81c8412898e19f 100644 (file)
@@ -1717,6 +1717,16 @@ def encode_dict(d, encoding='utf-8'):
     return dict((encode(k), encode(v)) for k, v in d.items())
 
 
+def dict_get(d, key_or_keys, default=None, skip_false_values=True):
+    if isinstance(key_or_keys, (list, tuple)):
+        for key in key_or_keys:
+            if key not in d or d[key] is None or skip_false_values and not d[key]:
+                continue
+            return d[key]
+        return default
+    return d.get(key_or_keys, default)
+
+
 def encode_compat_str(string, encoding=preferredencoding(), errors='strict'):
     return string if isinstance(string, compat_str) else compat_str(string, encoding, errors)
 
@@ -2017,7 +2027,7 @@ def dfxp2srt(dfxp_data):
         'ttaf1': 'http://www.w3.org/2006/10/ttaf1',
     })
 
-    class TTMLPElementParser:
+    class TTMLPElementParser(object):
         out = ''
 
         def start(self, tag, attrib):