diff options
author | James Taylor <user234683@users.noreply.github.com> | 2019-12-26 18:38:18 -0800 |
---|---|---|
committer | James Taylor <user234683@users.noreply.github.com> | 2019-12-26 18:38:18 -0800 |
commit | c7edea08487254e3e611b1bf652569670642c4f6 (patch) | |
tree | 9f60743fd78746c57531dd8f7d6dcbf96f8707d3 /youtube | |
parent | f7a5f7fbaa519eeb340537fe920fbe7ded8b16f2 (diff) | |
download | yt-local-c7edea08487254e3e611b1bf652569670642c4f6.tar.lz yt-local-c7edea08487254e3e611b1bf652569670642c4f6.tar.xz yt-local-c7edea08487254e3e611b1bf652569670642c4f6.zip |
yt_data_extract: Simplify extract_items so it needs only 1 while loop
Diffstat (limited to 'youtube')
-rw-r--r-- | youtube/yt_data_extract/common.py | 63 |
1 files changed, 31 insertions, 32 deletions
diff --git a/youtube/yt_data_extract/common.py b/youtube/yt_data_extract/common.py index 5572281..85eb363 100644 --- a/youtube/yt_data_extract/common.py +++ b/youtube/yt_data_extract/common.py @@ -378,43 +378,42 @@ def extract_items(response, item_types=item_types): renderer = get(response, 'contents', {}, types=dict) while True: - # mode 1: dig into the current renderer - # Will stay in mode 1 (via continue) if a new renderer is found inside this one - # Otherwise, after finding that it is an item renderer, - # contains a list, or contains nothing, - # falls through into mode 2 to get a new renderer - if len(renderer) != 0: - key, value = list(renderer.items())[0] - - # has a list in it, add it to the iter stack - if key in nested_renderer_list_dispatch: - renderer_list, continuation = nested_renderer_list_dispatch[key](value) - if renderer_list: - iter_stack.append(current_iter) - current_iter = iter(renderer_list) - if continuation: - ctoken = continuation - - # new renderer nested inside this one - elif key in nested_renderer_dispatch: - renderer = nested_renderer_dispatch[key](value) - continue # back to mode 1 - - # the renderer is an item - elif key in item_types: - items.append(renderer) - - - # mode 2: get a new renderer by iterating. - # goes up the stack for an iterator if one has been exhausted - while current_iter is not None: + # mode 1: get a new renderer by iterating. + # goes down the stack for an iterator if one has been exhausted + if not renderer: try: renderer = current_iter.__next__() - break except StopIteration: try: - current_iter = iter_stack.pop() # go back up the stack + current_iter = iter_stack.pop() except IndexError: return items, ctoken + # Get new renderer or check that the one we got is good before + # proceeding to mode 2 + continue + + + # mode 2: dig into the current renderer + key, value = list(renderer.items())[0] + + # has a list in it, add it to the iter stack + if key in nested_renderer_list_dispatch: + renderer_list, continuation = nested_renderer_list_dispatch[key](value) + if renderer_list: + iter_stack.append(current_iter) + current_iter = iter(renderer_list) + if continuation: + ctoken = continuation + + # new renderer nested inside this one + elif key in nested_renderer_dispatch: + renderer = nested_renderer_dispatch[key](value) + continue # don't reset renderer to None + + # the renderer is an item + elif key in item_types: + items.append(renderer) + + renderer = None else: return [], None |