aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Taylor <user234683@users.noreply.github.com>2018-12-21 01:59:33 -0800
committerJames Taylor <user234683@users.noreply.github.com>2018-12-21 01:59:33 -0800
commitdca25bd4cc52fd7555f4210f3fa7fa73bf920d97 (patch)
tree846caafc095ac60d034d309e745c608945ab8aae
parente499ff6185b8f7a87de3dd4fd81f34f70fd2f9c0 (diff)
downloadyt-local-dca25bd4cc52fd7555f4210f3fa7fa73bf920d97.tar.lz
yt-local-dca25bd4cc52fd7555f4210f3fa7fa73bf920d97.tar.xz
yt-local-dca25bd4cc52fd7555f4210f3fa7fa73bf920d97.zip
Properly handle terminated channel on channel's playlist and about page
-rw-r--r--http_errors.py5
-rw-r--r--server.py4
-rw-r--r--youtube/channel.py49
3 files changed, 36 insertions, 22 deletions
diff --git a/http_errors.py b/http_errors.py
index 975e0d4..bd8fbb4 100644
--- a/http_errors.py
+++ b/http_errors.py
@@ -1,3 +1,8 @@
+class Code2xx(Exception):
+ pass
+class Code200(Code2xx):
+ pass
+
class Error4xx(Exception):
pass
class Error404(Error4xx):
diff --git a/server.py b/server.py
index 34f465f..647accf 100644
--- a/server.py
+++ b/server.py
@@ -115,6 +115,10 @@ def site_dispatch(env, start_response):
yield error_code('404 Not Found', start_response)
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:
start_response('404 Not Found', ())
yield str(e).encode('utf-8')
diff --git a/youtube/channel.py b/youtube/channel.py
index 2921cf9..ae2a5e3 100644
--- a/youtube/channel.py
+++ b/youtube/channel.py
@@ -181,6 +181,25 @@ def channel_sort_buttons_html(channel_id, tab, current_sort):
)
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
def get_grid_items(response):
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=''):
response = polymer_json[1]['response']
- try:
- 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
+ microformat = get_microformat(response)
channel_url = microformat['urlCanonical'].rstrip('/')
channel_id = channel_url[channel_url.rfind('/')+1:]
+
items = get_grid_items(response)
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):
response = polymer_json[1]['response']
- microformat = response['microformat']['microformatDataRenderer']
+ microformat = get_microformat(response)
channel_url = microformat['urlCanonical'].rstrip('/')
channel_id = channel_url[channel_url.rfind('/')+1:]
@@ -278,7 +281,8 @@ channel_link_template = Template('''
stat_template = Template('''
<li>$stat_value</li>''')
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...
channel_metadata = tab_with_content(polymer_json[1]['response']['contents']['twoColumnBrowseResultsRenderer']['tabs'])['sectionListRenderer']['contents'][0]['itemSectionRenderer']['contents'][0]['channelAboutFullMetadataRenderer']
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=''):
- microformat = polymer_json[1]['response']['microformat']['microformatDataRenderer']
+ response = polymer_json[1]['response']
+ microformat = get_microformat(response)
channel_url = microformat['urlCanonical'].rstrip('/')
channel_id = channel_url[channel_url.rfind('/')+1:]
- response = polymer_json[1]['response']
+
try:
items = tab_with_content(response['contents']['twoColumnBrowseResultsRenderer']['tabs'])['sectionListRenderer']['contents']
except KeyError: