Properly handle terminated channel on channel's playlist and about page

This commit is contained in:
James Taylor 2018-12-21 01:59:33 -08:00
parent e499ff6185
commit dca25bd4cc
3 changed files with 36 additions and 22 deletions

View File

@ -1,3 +1,8 @@
class Code2xx(Exception):
pass
class Code200(Code2xx):
pass
class Error4xx(Exception): class Error4xx(Exception):
pass pass
class Error404(Error4xx): class Error404(Error4xx):

View File

@ -115,6 +115,10 @@ def site_dispatch(env, start_response):
yield error_code('404 Not Found', start_response) yield error_code('404 Not Found', start_response)
return return
except http_errors.Code200 as e: # Raised in scenarios where a simple status message is to be returned, such as a terminated channel
start_response('200 OK', ())
yield str(e).encode('utf-8')
except http_errors.Error404 as e: except http_errors.Error404 as e:
start_response('404 Not Found', ()) start_response('404 Not Found', ())
yield str(e).encode('utf-8') yield str(e).encode('utf-8')

View File

@ -181,6 +181,25 @@ def channel_sort_buttons_html(channel_id, tab, current_sort):
) )
return result return result
def get_microformat(response):
try:
return response['microformat']['microformatDataRenderer']
# channel doesn't exist or was terminated
# example terminated channel: https://www.youtube.com/channel/UCnKJeK_r90jDdIuzHXC0Org
except KeyError:
if 'alerts' in response and len(response['alerts']) > 0:
result = ''
for alert in response['alerts']:
result += alert['alertRenderer']['text']['simpleText'] + '\n'
raise http_errors.Code200(result)
elif 'errors' in response['responseContext']:
for error in response['responseContext']['errors']['error']:
if error['code'] == 'INVALID_VALUE' and error['location'] == 'browse_id':
raise http_errors.Error404('This channel does not exist')
raise
# example channel with no videos: https://www.youtube.com/user/jungleace # example channel with no videos: https://www.youtube.com/user/jungleace
def get_grid_items(response): def get_grid_items(response):
try: try:
@ -203,26 +222,10 @@ def get_grid_items(response):
def channel_videos_html(polymer_json, current_page=1, current_sort=3, number_of_videos = 1000, current_query_string=''): def channel_videos_html(polymer_json, current_page=1, current_sort=3, number_of_videos = 1000, current_query_string=''):
response = polymer_json[1]['response'] response = polymer_json[1]['response']
try: microformat = get_microformat(response)
microformat = response['microformat']['microformatDataRenderer']
# channel doesn't exist or was terminated
# example terminated channel: https://www.youtube.com/channel/UCnKJeK_r90jDdIuzHXC0Org
except KeyError:
if 'alerts' in response and len(response['alerts']) > 0:
result = ''
for alert in response['alerts']:
result += alert['alertRenderer']['text']['simpleText'] + '\n'
return result
elif 'errors' in response['responseContext']:
for error in response['responseContext']['errors']['error']:
if error['code'] == 'INVALID_VALUE' and error['location'] == 'browse_id':
raise http_errors.Error404('This channel does not exist')
raise
else:
raise
channel_url = microformat['urlCanonical'].rstrip('/') channel_url = microformat['urlCanonical'].rstrip('/')
channel_id = channel_url[channel_url.rfind('/')+1:] channel_id = channel_url[channel_url.rfind('/')+1:]
items = get_grid_items(response) items = get_grid_items(response)
items_html = grid_items_html(items, {'author': microformat['title']}) items_html = grid_items_html(items, {'author': microformat['title']})
@ -240,7 +243,7 @@ def channel_videos_html(polymer_json, current_page=1, current_sort=3, number_of_
def channel_playlists_html(polymer_json, current_sort=3): def channel_playlists_html(polymer_json, current_sort=3):
response = polymer_json[1]['response'] response = polymer_json[1]['response']
microformat = response['microformat']['microformatDataRenderer'] microformat = get_microformat(response)
channel_url = microformat['urlCanonical'].rstrip('/') channel_url = microformat['urlCanonical'].rstrip('/')
channel_id = channel_url[channel_url.rfind('/')+1:] channel_id = channel_url[channel_url.rfind('/')+1:]
@ -278,7 +281,8 @@ channel_link_template = Template('''
stat_template = Template(''' stat_template = Template('''
<li>$stat_value</li>''') <li>$stat_value</li>''')
def channel_about_page(polymer_json): def channel_about_page(polymer_json):
avatar = '/' + polymer_json[1]['response']['microformat']['microformatDataRenderer']['thumbnail']['thumbnails'][0]['url'] microformat = get_microformat(polymer_json[1]['response'])
avatar = '/' + microformat['thumbnail']['thumbnails'][0]['url']
# my goodness... # my goodness...
channel_metadata = tab_with_content(polymer_json[1]['response']['contents']['twoColumnBrowseResultsRenderer']['tabs'])['sectionListRenderer']['contents'][0]['itemSectionRenderer']['contents'][0]['channelAboutFullMetadataRenderer'] channel_metadata = tab_with_content(polymer_json[1]['response']['contents']['twoColumnBrowseResultsRenderer']['tabs'])['sectionListRenderer']['contents'][0]['itemSectionRenderer']['contents'][0]['channelAboutFullMetadataRenderer']
channel_links = '' channel_links = ''
@ -317,11 +321,12 @@ def channel_about_page(polymer_json):
) )
def channel_search_page(polymer_json, query, current_page=1, number_of_videos = 1000, current_query_string=''): def channel_search_page(polymer_json, query, current_page=1, number_of_videos = 1000, current_query_string=''):
microformat = polymer_json[1]['response']['microformat']['microformatDataRenderer'] response = polymer_json[1]['response']
microformat = get_microformat(response)
channel_url = microformat['urlCanonical'].rstrip('/') channel_url = microformat['urlCanonical'].rstrip('/')
channel_id = channel_url[channel_url.rfind('/')+1:] channel_id = channel_url[channel_url.rfind('/')+1:]
response = polymer_json[1]['response']
try: try:
items = tab_with_content(response['contents']['twoColumnBrowseResultsRenderer']['tabs'])['sectionListRenderer']['contents'] items = tab_with_content(response['contents']['twoColumnBrowseResultsRenderer']['tabs'])['sectionListRenderer']['contents']
except KeyError: except KeyError: