Fix NoneType inside comments.py
Bug:
Traceback (most recent call last):
File "/home/rusian/yt-local/youtube/comments.py", line 180, in video_comments
post_process_comments_info(comments_info)
File "/home/rusian/yt-local/youtube/comments.py", line 81, in post_process_comments_info
comment['author'] = strip_non_ascii(comment['author'])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/rusian/yt-local/youtube/util.py", line 843, in strip_non_ascii
stripped = (c for c in string if 0 < ord(c) < 127)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'NoneType' object is not iterable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "src/gevent/greenlet.py", line 900, in gevent._gevent_cgreenlet.Greenlet.run
File "/home/rusian/yt-local/youtube/comments.py", line 195, in video_comments
comments_info['error'] = 'YouTube blocked the request. IP address: %s' % e.ip
^^^^
AttributeError: 'TypeError' object has no attribute 'ip'
2025-03-08T01:25:47Z <Greenlet at 0x7f251e5279c0: video_comments('hcm55lU9knw', 0, lc='')> failed with AttributeError
This commit is contained in:
@@ -53,7 +53,7 @@ def request_comments(ctoken, replies=False):
|
|||||||
'hl': 'en',
|
'hl': 'en',
|
||||||
'gl': 'US',
|
'gl': 'US',
|
||||||
'clientName': 'MWEB',
|
'clientName': 'MWEB',
|
||||||
'clientVersion': '2.20240328.08.00',
|
'clientVersion': '2.20210804.02.00',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
'continuation': ctoken.replace('=', '%3D'),
|
'continuation': ctoken.replace('=', '%3D'),
|
||||||
@@ -78,7 +78,7 @@ def single_comment_ctoken(video_id, comment_id):
|
|||||||
|
|
||||||
def post_process_comments_info(comments_info):
|
def post_process_comments_info(comments_info):
|
||||||
for comment in comments_info['comments']:
|
for comment in comments_info['comments']:
|
||||||
comment['author'] = strip_non_ascii(comment['author'])
|
comment['author'] = strip_non_ascii(comment['author']) if comment.get('author') else ""
|
||||||
comment['author_url'] = concat_or_none(
|
comment['author_url'] = concat_or_none(
|
||||||
'/', comment['author_url'])
|
'/', comment['author_url'])
|
||||||
comment['author_avatar'] = concat_or_none(
|
comment['author_avatar'] = concat_or_none(
|
||||||
@@ -189,10 +189,10 @@ def video_comments(video_id, sort=0, offset=0, lc='', secret_key=''):
|
|||||||
comments_info['error'] += '\n\n' + e.error_message
|
comments_info['error'] += '\n\n' + e.error_message
|
||||||
comments_info['error'] += '\n\nExit node IP address: %s' % e.ip
|
comments_info['error'] += '\n\nExit node IP address: %s' % e.ip
|
||||||
else:
|
else:
|
||||||
comments_info['error'] = 'YouTube blocked the request. IP address: %s' % e.ip
|
comments_info['error'] = 'YouTube blocked the request. Error: %s' % str(e)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
comments_info['error'] = 'YouTube blocked the request. IP address: %s' % e.ip
|
comments_info['error'] = 'YouTube blocked the request. Error: %s' % str(e)
|
||||||
|
|
||||||
if comments_info.get('error'):
|
if comments_info.get('error'):
|
||||||
print('Error retrieving comments for ' + str(video_id) + ':\n' +
|
print('Error retrieving comments for ' + str(video_id) + ':\n' +
|
||||||
|
|||||||
@@ -840,6 +840,8 @@ def call_youtube_api(client, api, data):
|
|||||||
|
|
||||||
def strip_non_ascii(string):
|
def strip_non_ascii(string):
|
||||||
''' Returns the string without non ASCII characters'''
|
''' Returns the string without non ASCII characters'''
|
||||||
|
if string is None:
|
||||||
|
return ""
|
||||||
stripped = (c for c in string if 0 < ord(c) < 127)
|
stripped = (c for c in string if 0 < ord(c) < 127)
|
||||||
return ''.join(stripped)
|
return ''.join(stripped)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user