channel playlists: fix error when 0 playlists, correct stats for playlist

This commit is contained in:
James Taylor 2018-07-06 18:34:11 -07:00
parent 237d19d834
commit 2ba4fd8994
2 changed files with 19 additions and 4 deletions

View File

@ -178,7 +178,15 @@ def channel_playlists_html(polymer_json):
except KeyError: except KeyError:
items = [] items = []
else: else:
items = contents['twoColumnBrowseResultsRenderer']['tabs'][2]['tabRenderer']['content']['sectionListRenderer']['contents'][0]['itemSectionRenderer']['contents'][0]['gridRenderer']['items'] item_section = contents['twoColumnBrowseResultsRenderer']['tabs'][2]['tabRenderer']['content']['sectionListRenderer']['contents'][0]['itemSectionRenderer']['contents'][0]
try:
items = item_section['gridRenderer']['items']
except KeyError:
if "messageRenderer" in item_section:
items = []
else:
raise
items_html = grid_items_html(items, {'author': microformat['title']}) items_html = grid_items_html(items, {'author': microformat['title']})
return yt_channel_items_template.substitute( return yt_channel_items_template.substitute(
@ -256,6 +264,8 @@ def get_channel_page(url, query_string=''):
return channel_about_page(polymer_json) return channel_about_page(polymer_json)
elif tab == 'playlists': elif tab == 'playlists':
polymer_json = common.fetch_url('https://www.youtube.com/channel/' + channel_id + '/playlists?pbj=1', headers_1) polymer_json = common.fetch_url('https://www.youtube.com/channel/' + channel_id + '/playlists?pbj=1', headers_1)
'''with open('debug/channel_playlists_debug', 'wb') as f:
f.write(polymer_json)'''
polymer_json = json.loads(polymer_json) polymer_json = json.loads(polymer_json)
return channel_playlists_html(polymer_json) return channel_playlists_html(polymer_json)
else: else:

View File

@ -60,7 +60,7 @@ medium_playlist_item_template = Template('''
<a class="title" href="$url" title="$title">$title</a> <a class="title" href="$url" title="$title">$title</a>
<address><a href="$author_url">$author</a></address> <div class="stats">$stats</div>
</div> </div>
''') ''')
medium_video_item_template = Template(''' medium_video_item_template = Template('''
@ -446,7 +446,7 @@ stat_templates = (
Template('''<span class="views">$views</span>'''), Template('''<span class="views">$views</span>'''),
Template('''<time datetime="$datetime">$published</time>'''), Template('''<time datetime="$datetime">$published</time>'''),
) )
def get_video_stats(html_ready): def get_stats(html_ready):
stats = [] stats = []
if 'author' in html_ready: if 'author' in html_ready:
if 'author_url' in html_ready: if 'author_url' in html_ready:
@ -479,7 +479,7 @@ def video_item_html(item, template, html_exclude=set()):
for key in html_exclude: for key in html_exclude:
del html_ready[key] del html_ready[key]
html_ready['stats'] = get_video_stats(html_ready) html_ready['stats'] = get_stats(html_ready)
return template.substitute(html_ready) return template.substitute(html_ready)
@ -489,6 +489,11 @@ def playlist_item_html(item, template, html_exclude=set()):
html_ready['url'] = URL_ORIGIN + "/playlist?list=" + html_ready['id'] html_ready['url'] = URL_ORIGIN + "/playlist?list=" + html_ready['id']
html_ready['datetime'] = '' #TODO html_ready['datetime'] = '' #TODO
for key in html_exclude:
del html_ready[key]
html_ready['stats'] = get_stats(html_ready)
return template.substitute(html_ready) return template.substitute(html_ready)