Skip to content

Commit

Permalink
Fix compatibility with Youtube shorts
Browse files Browse the repository at this point in the history
  • Loading branch information
egbertbouman committed Sep 6, 2022
1 parent 778d6eb commit 49ace71
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions youtube_comment_downloader/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def get_comments(self, youtube_id, *args, **kwargs):
def get_comments_from_url(self, youtube_url, sort_by=SORT_BY_RECENT, language=None, sleep=.1):
response = self.session.get(youtube_url)

if 'uxe=' in response.request.url:
if 'consent' in response.request.url or 'uxe=' in response.request.url:
self.session.cookies.set('CONSENT', 'YES+cb', domain='.youtube.com')
response = self.session.get(youtube_url)

Expand All @@ -58,14 +58,17 @@ def get_comments_from_url(self, youtube_url, sort_by=SORT_BY_RECENT, language=No

data = json.loads(self.regex_search(html, YT_INITIAL_DATA_RE, default=''))

section = next(self.search_dict(data['contents'], 'itemSectionRenderer'), None)
section = next(self.search_dict(data, 'itemSectionRenderer'), None)
renderer = next(self.search_dict(section, 'continuationItemRenderer'), None) if section else None
if not renderer:
# Comments disabled?
return

needs_sorting = sort_by != SORT_BY_POPULAR
continuations = [renderer['continuationEndpoint']]
sort_menu = next(self.search_dict(data, 'sortFilterSubMenuRenderer'), {}).get('subMenuItems', [])
if not sort_menu or sort_by >= len(sort_menu):
raise RuntimeError('Failed to set sorting')
continuations = [sort_menu[sort_by]['serviceEndpoint']]

while continuations:
continuation = continuations.pop()
response = self.ajax_request(continuation, ytcfg)
Expand All @@ -77,25 +80,17 @@ def get_comments_from_url(self, youtube_url, sort_by=SORT_BY_RECENT, language=No
if error:
raise RuntimeError('Error returned from server: ' + error)

if needs_sorting:
sort_menu = next(self.search_dict(response, 'sortFilterSubMenuRenderer'), {}).get('subMenuItems', [])
if sort_by < len(sort_menu):
continuations = [sort_menu[sort_by]['serviceEndpoint']]
needs_sorting = False
continue
raise RuntimeError('Failed to set sorting')

actions = list(self.search_dict(response, 'reloadContinuationItemsCommand')) + \
list(self.search_dict(response, 'appendContinuationItemsAction'))
for action in actions:
for item in action.get('continuationItems', []):
if action['targetId'] == 'comments-section':
if action['targetId'] in ['comments-section', 'engagement-panel-comments-section']:
# Process continuations for comments and replies.
continuations[:0] = [ep for ep in self.search_dict(item, 'continuationEndpoint')]
if action['targetId'].startswith('comment-replies-item') and 'continuationItemRenderer' in item:
# Process the 'Show more replies' button
continuations.append(next(self.search_dict(item, 'buttonRenderer'))['command'])

for comment in reversed(list(self.search_dict(response, 'commentRenderer'))):
result = {'cid': comment['commentId'],
'text': ''.join([c['text'] for c in comment['contentText'].get('runs', [])]),
Expand Down

0 comments on commit 49ace71

Please sign in to comment.