From c7edea08487254e3e611b1bf652569670642c4f6 Mon Sep 17 00:00:00 2001 From: James Taylor Date: Thu, 26 Dec 2019 18:38:18 -0800 Subject: yt_data_extract: Simplify extract_items so it needs only 1 while loop --- youtube/yt_data_extract/common.py | 63 +++++++++++++++++++-------------------- 1 file 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 -- cgit v1.2.3