diff options
author | Sergey M․ <dstftw@gmail.com> | 2017-04-18 22:39:58 +0700 |
---|---|---|
committer | Sergey M․ <dstftw@gmail.com> | 2017-04-18 22:39:58 +0700 |
commit | a32a9a7ef5f64bcec29ef71c8e5b6dc509232c13 (patch) | |
tree | 154ee7f2afb7879de653a11e8213e35773f425e6 /youtube_dl/utils.py | |
parent | bae1404893341ed89f4c9b556aa4068c13ed9f7a (diff) |
[extractor/common] Add support multiple getters in try_get
Diffstat (limited to 'youtube_dl/utils.py')
-rw-r--r-- | youtube_dl/utils.py | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py index 2340bc306..79a337df4 100644 --- a/youtube_dl/utils.py +++ b/youtube_dl/utils.py @@ -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'): |