]> git.bitcoin.ninja Git - youtube-dl/commitdiff
[extractor/common] Add support multiple getters in try_get
authorSergey M․ <dstftw@gmail.com>
Tue, 18 Apr 2017 15:39:58 +0000 (22:39 +0700)
committerSergey M․ <dstftw@gmail.com>
Tue, 18 Apr 2017 15:39:58 +0000 (22:39 +0700)
youtube_dl/utils.py

index 2340bc306b69652d43246472e7fd3f5e819e2af2..79a337df4d73397e2d1aa395e5c5202d44c0e2cc 100644 (file)
@@ -2103,13 +2103,16 @@ def dict_get(d, key_or_keys, default=None, skip_false_values=True):
 
 
 def try_get(src, getter, expected_type=None):
-    try:
-        v = getter(src)
-    except (AttributeError, KeyError, TypeError, IndexError):
-        pass
-    else:
-        if expected_type is None or isinstance(v, expected_type):
-            return v
+    if not isinstance(getter, (list, tuple)):
+        getter = [getter]
+    for get in getter:
+        try:
+            v = get(src)
+        except (AttributeError, KeyError, TypeError, IndexError):
+            pass
+        else:
+            if expected_type is None or isinstance(v, expected_type):
+                return v
 
 
 def encode_compat_str(string, encoding=preferredencoding(), errors='strict'):