Video comments: replace with error message if there is error

Such as 429 error, or an exception
This commit is contained in:
James Taylor 2020-11-29 18:56:22 -08:00
parent 2ac806f9eb
commit b126ce1aa6
3 changed files with 53 additions and 19 deletions

View File

@ -7,6 +7,7 @@ import json
import base64 import base64
import urllib import urllib
import re import re
import traceback
import flask import flask
from flask import request from flask import request
@ -137,23 +138,49 @@ def post_process_comments_info(comments_info):
def video_comments(video_id, sort=0, offset=0, lc='', secret_key=''): def video_comments(video_id, sort=0, offset=0, lc='', secret_key=''):
if settings.comments_mode: try:
comments_info = yt_data_extract.extract_comments_info(request_comments(make_comment_ctoken(video_id, sort, offset, lc, secret_key))) if settings.comments_mode:
post_process_comments_info(comments_info) comments_info = {'error': None}
other_sort_url = (
util.URL_ORIGIN + '/comments?ctoken='
+ make_comment_ctoken(video_id, sort=1 - sort, lc=lc)
)
other_sort_text = 'Sort by ' + ('newest' if sort == 0 else 'top')
other_sort_url = util.URL_ORIGIN + '/comments?ctoken=' + make_comment_ctoken(video_id, sort=1 - sort, lc=lc) this_sort_url = (util.URL_ORIGIN
other_sort_text = 'Sort by ' + ('newest' if sort == 0 else 'top') + '/comments?ctoken='
+ make_comment_ctoken(video_id, sort=sort, lc=lc))
this_sort_url = (util.URL_ORIGIN comments_info['comment_links'] = [
+ '/comments?ctoken=' (other_sort_text, other_sort_url),
+ make_comment_ctoken(video_id, sort=sort, lc=lc)) ('Direct link', this_sort_url)
]
comments_info['comment_links'] = [(other_sort_text, other_sort_url), comments_info.update(yt_data_extract.extract_comments_info(
('Direct link', this_sort_url)] request_comments(
make_comment_ctoken(video_id, sort, offset, lc, secret_key)
)
))
post_process_comments_info(comments_info)
return comments_info return comments_info
else:
return {}
except util.FetchError as e:
print('Error retrieving comments for ' + str(video_id))
if e.code == '429' and settings.route_tor:
comments_info['error'] = 'Error: Youtube blocked the request because the Tor exit node is overutilized.'
if e.error_message:
comments_info['error'] += '\n\n' + e.error_message
comments_info['error'] += '\n\nExit node IP address: %s' % e.ip
else:
comments_info['error'] = traceback.format_exc()
return {} except Exception as e:
print('Error retrieving comments for ' + str(video_id))
comments_info['error'] = traceback.format_exc()
return comments_info

View File

@ -43,13 +43,19 @@
<a class="sort-button" href="{{ link_url }}">{{ link_text }}</a> <a class="sort-button" href="{{ link_url }}">{{ link_text }}</a>
{% endfor %} {% endfor %}
</div> </div>
<div class="comments"> {% if comments_info['error'] %}
{% for comment in comments_info['comments'] %} <div class="comments">
{{ render_comment(comment, comments_info['include_avatars'], True) }} <div class="code-box"><code>{{ comments_info['error'] }}</code></div>
{% endfor %} </div>
</div> {% else %}
{% if 'more_comments_url' is in comments_info %} <div class="comments">
<a class="page-button more-comments" href="{{ comments_info['more_comments_url'] }}">More comments</a> {% for comment in comments_info['comments'] %}
{{ render_comment(comment, comments_info['include_avatars'], True) }}
{% endfor %}
</div>
{% if 'more_comments_url' is in comments_info %}
<a class="page-button more-comments" href="{{ comments_info['more_comments_url'] }}">More comments</a>
{% endif %}
{% endif %} {% endif %}
{% endmacro %} {% endmacro %}

View File

@ -355,6 +355,7 @@ h1{
margin-bottom: 10px; margin-bottom: 10px;
} }
.code-box{ .code-box{
white-space: pre-wrap;
padding: 5px; padding: 5px;
border-style:solid; border-style:solid;
border-width:1px; border-width:1px;