aboutsummaryrefslogtreecommitdiffstats
path: root/youtube/yt_data_extract/common.py
diff options
context:
space:
mode:
authorJames Taylor <user234683@users.noreply.github.com>2021-08-07 17:05:58 -0700
committerJesús <heckyel@hyperbola.info>2021-08-09 12:10:42 -0500
commit3dee7ea0d1156642d02f504b9229676b287ddf0a (patch)
tree76770d1aacfac266613ed32b18bee8329d413d71 /youtube/yt_data_extract/common.py
parentbee14ea9ea2a3e78f70b1a68c965d15297a6ede8 (diff)
downloadyt-local-3dee7ea0d1156642d02f504b9229676b287ddf0a.tar.lz
yt-local-3dee7ea0d1156642d02f504b9229676b287ddf0a.tar.xz
yt-local-3dee7ea0d1156642d02f504b9229676b287ddf0a.zip
Switch to new comments api now that old one is being disabled
watch_comment api periodically gives the error "Top level comments mweb servlet is turned down." The continuation items for the new api are in a different arrangement in the json, so changes were necessary to the extract_items function. Signed-off-by: Jesús <heckyel@hyperbola.info>
Diffstat (limited to 'youtube/yt_data_extract/common.py')
-rw-r--r--youtube/yt_data_extract/common.py39
1 files changed, 32 insertions, 7 deletions
diff --git a/youtube/yt_data_extract/common.py b/youtube/yt_data_extract/common.py
index e87808b..74ac1d6 100644
--- a/youtube/yt_data_extract/common.py
+++ b/youtube/yt_data_extract/common.py
@@ -478,6 +478,22 @@ def extract_items_from_renderer(renderer, item_types=_item_types):
renderer = None
+
+def extract_items_from_renderer_list(renderers, item_types=_item_types):
+ '''Same as extract_items_from_renderer, but provide a list of renderers'''
+ items = []
+ ctoken = None
+ for renderer in renderers:
+ new_items, new_ctoken = extract_items_from_renderer(
+ renderer,
+ item_types=item_types)
+ items += new_items
+ # prioritize ctoken associated with items
+ if (not ctoken) or (new_ctoken and new_items):
+ ctoken = new_ctoken
+ return items, ctoken
+
+
def extract_items(response, item_types=_item_types,
search_engagement_panels=False):
'''return items, ctoken'''
@@ -495,6 +511,15 @@ def extract_items(response, item_types=_item_types,
item_types=item_types)
if items:
break
+ elif 'onResponseReceivedEndpoints' in response:
+ for endpoint in response.get('onResponseReceivedEndpoints', []):
+ items, ctoken = extract_items_from_renderer_list(
+ deep_get(endpoint, 'appendContinuationItemsAction',
+ 'continuationItems', default=[]),
+ item_types=item_types,
+ )
+ if items:
+ break
elif 'contents' in response:
renderer = get(response, 'contents', {})
items, ctoken = extract_items_from_renderer(
@@ -502,11 +527,11 @@ def extract_items(response, item_types=_item_types,
item_types=item_types)
if search_engagement_panels and 'engagementPanels' in response:
- for engagement_renderer in response['engagementPanels']:
- additional_items, cont = extract_items_from_renderer(
- engagement_renderer,
- item_types=item_types)
- items += additional_items
- if cont and not ctoken:
- ctoken = cont
+ new_items, new_ctoken = extract_items_from_renderer_list(
+ response['engagementPanels'], item_types=item_types
+ )
+ items += new_items
+ if (not ctoken) or (new_ctoken and new_items):
+ ctoken = new_ctoken
+
return items, ctoken