Skip to content

Commit

Permalink
Fix comment count extraction due to 'K/M' postfixes
Browse files Browse the repository at this point in the history
YouTube now displays 2K comments instead of 2359, for instance
  • Loading branch information
user234683 committed Oct 15, 2023
1 parent dc443c7 commit bfa858f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
5 changes: 4 additions & 1 deletion youtube/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ def commatize(num):
if num is None:
return ''
if isinstance(num, str):
num = int(num)
try:
num = int(num)
except ValueError:
return num
return '{:,}'.format(num)

def timestamp_replacement(match):
Expand Down
2 changes: 1 addition & 1 deletion youtube/templates/watch.html
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ <h2 class="title">{{ title }}</h2>
<div class="comments-area-outer comments-disabled">Comments disabled</div>
{% else %}
<details class="comments-area-outer" {{'open' if settings.comments_mode == 1 else ''}}>
<summary>{{ comment_count|commatize }} comment{{'s' if comment_count != 1 else ''}}</summary>
<summary>{{ comment_count|commatize }} comment{{'s' if comment_count != '1' else ''}}</summary>
<section class="comments-area-inner comments-area">
{% if comments_info %}
{{ comments.video_comments(comments_info) }}
Expand Down
6 changes: 3 additions & 3 deletions youtube/yt_data_extract/watch_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,12 +363,12 @@ def _extract_watch_info_mobile(top_level):
comment_count_text = extract_str(deep_get(comment_info,
'header', 'commentSectionHeaderRenderer', 'countText'))
if comment_count_text == 'Comments': # just this with no number, means 0 comments
info['comment_count'] = 0
info['comment_count'] = '0'
else:
info['comment_count'] = extract_int(comment_count_text)
info['comment_count'] = extract_approx_int(comment_count_text)
info['comments_disabled'] = False
else: # no comment section present means comments are disabled
info['comment_count'] = 0
info['comment_count'] = '0'
info['comments_disabled'] = True

# check for limited state
Expand Down

0 comments on commit bfa858f

Please sign in to comment.