add playlist tab to channels

This commit is contained in:
James Taylor 2018-07-06 17:41:16 -07:00
parent edbc305da9
commit 9cefa46bc5
4 changed files with 63 additions and 12 deletions

View File

@ -158,6 +158,26 @@ def grid_items_html(items, additional_info={}):
result += '''\n</nav>''' result += '''\n</nav>'''
return result return result
channel_tab_template = Template('''\n<a class="tab page-button"$href_attribute>$tab_name</a>''')
tabs = ('Videos', 'Playlists', 'About')
def channel_tabs_html(channel_id, current_tab):
result = ''
for tab_name in tabs:
if tab_name == current_tab:
result += channel_tab_template.substitute(
href_attribute = '',
tab_name = tab_name,
)
else:
result += channel_tab_template.substitute(
href_attribute = 'href="' + URL_ORIGIN + "/channel/" + channel_id + "/" + tab_name.lower() + '"',
tab_name = tab_name,
)
return result
def channel_videos_html(polymer_json, current_page=1, number_of_videos = 1000, current_query_string=''): def channel_videos_html(polymer_json, current_page=1, number_of_videos = 1000, current_query_string=''):
microformat = polymer_json[1]['response']['microformat']['microformatDataRenderer'] microformat = polymer_json[1]['response']['microformat']['microformatDataRenderer']
channel_url = microformat['urlCanonical'].rstrip('/') channel_url = microformat['urlCanonical'].rstrip('/')
@ -176,7 +196,7 @@ def channel_videos_html(polymer_json, current_page=1, number_of_videos = 1000, c
return yt_channel_items_template.substitute( return yt_channel_items_template.substitute(
channel_title = microformat['title'], channel_title = microformat['title'],
channel_about_url = URL_ORIGIN + "/channel/" + channel_id + "/about", channel_tabs = channel_tabs_html(channel_id, 'Videos'),
avatar = '/' + microformat['thumbnail']['thumbnails'][0]['url'], avatar = '/' + microformat['thumbnail']['thumbnails'][0]['url'],
page_title = microformat['title'] + ' - Channel', page_title = microformat['title'] + ' - Channel',
items = items_html, items = items_html,
@ -184,6 +204,32 @@ def channel_videos_html(polymer_json, current_page=1, number_of_videos = 1000, c
number_of_results = '{:,}'.format(number_of_videos) + " videos", number_of_results = '{:,}'.format(number_of_videos) + " videos",
) )
def channel_playlists_html(polymer_json):
microformat = polymer_json[1]['response']['microformat']['microformatDataRenderer']
channel_url = microformat['urlCanonical'].rstrip('/')
channel_id = channel_url[channel_url.rfind('/')+1:]
try:
items = polymer_json[1]['response']['continuationContents']['gridContinuation']['items']
except KeyError:
response = polymer_json[1]['response']
try:
contents = response['contents']
except KeyError:
items = []
else:
items = contents['twoColumnBrowseResultsRenderer']['tabs'][2]['tabRenderer']['content']['sectionListRenderer']['contents'][0]['itemSectionRenderer']['contents'][0]['gridRenderer']['items']
items_html = grid_items_html(items, {'author': microformat['title']})
return yt_channel_items_template.substitute(
channel_title = microformat['title'],
channel_tabs = channel_tabs_html(channel_id, 'Playlists'),
avatar = '/' + microformat['thumbnail']['thumbnails'][0]['url'],
page_title = microformat['title'] + ' - Channel',
items = items_html,
page_buttons = '',
number_of_results = '',
)
channel_link_template = Template(''' channel_link_template = Template('''
<a href="$url">$text</a>''') <a href="$url">$text</a>''')
stat_template = Template(''' stat_template = Template('''
@ -218,7 +264,7 @@ def channel_about_page(polymer_json):
description = description, description = description,
links = channel_links, links = channel_links,
stats = stats, stats = stats,
channel_videos_url = common.URL_ORIGIN + '/channel/' + channel_metadata['channelId'] + '/videos', channel_tabs = channel_tabs_html(channel_metadata['channelId'], 'About'),
) )
def get_channel_page(url, query_string=''): def get_channel_page(url, query_string=''):
@ -247,6 +293,10 @@ def get_channel_page(url, query_string=''):
polymer_json = common.fetch_url('https://www.youtube.com/channel/' + channel_id + '/about?pbj=1', headers_1) polymer_json = common.fetch_url('https://www.youtube.com/channel/' + channel_id + '/about?pbj=1', headers_1)
polymer_json = json.loads(polymer_json) polymer_json = json.loads(polymer_json)
return channel_about_page(polymer_json) return channel_about_page(polymer_json)
elif tab == 'playlists':
polymer_json = common.fetch_url('https://www.youtube.com/channel/' + channel_id + '/playlists?pbj=1', headers_1)
polymer_json = json.loads(polymer_json)
return channel_playlists_html(polymer_json)
else: else:
raise ValueError('Unknown channel tab: ' + tab) raise ValueError('Unknown channel tab: ' + tab)
@ -265,5 +315,9 @@ def get_user_page(url, query_string=''):
polymer_json = common.fetch_url('https://www.youtube.com/user/' + username + '/about?pbj=1', headers_1) polymer_json = common.fetch_url('https://www.youtube.com/user/' + username + '/about?pbj=1', headers_1)
polymer_json = json.loads(polymer_json) polymer_json = json.loads(polymer_json)
return channel_about_page(polymer_json) return channel_about_page(polymer_json)
elif page == 'playlists':
polymer_json = common.fetch_url('https://www.youtube.com/user/' + username + '/playlists?pbj=1', headers_1)
polymer_json = json.loads(polymer_json)
return channel_playlists_html(polymer_json)
else: else:
raise ValueError('Unknown channel page: ' + page) raise ValueError('Unknown channel page: ' + page)

View File

@ -58,7 +58,7 @@ medium_playlist_item_template = Template('''
</div> </div>
</a> </a>
<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> <address><a href="$author_url">$author</a></address>
</div> </div>
@ -73,10 +73,6 @@ medium_video_item_template = Template('''
<a class="title" href="$url">$title</a> <a class="title" href="$url">$title</a>
<div class="stats">$stats</div> <div class="stats">$stats</div>
<!--
<address><a href="$author_url">$author</a></address>
<span class="views">$views</span>
<time datetime="$datetime">Uploaded $published</time>-->
<span class="description">$description</span> <span class="description">$description</span>
<span class="badges">$badges</span> <span class="badges">$badges</span>
@ -477,7 +473,7 @@ def video_item_html(item, template, html_exclude=set()):
return template.substitute(html_ready) return template.substitute(html_ready)
def playlist_item_html(item, template, html_exlude=set()): def playlist_item_html(item, template, html_exclude=set()):
html_ready = get_html_ready(item) html_ready = get_html_ready(item)
html_ready['url'] = URL_ORIGIN + "/playlist?list=" + html_ready['id'] html_ready['url'] = URL_ORIGIN + "/playlist?list=" + html_ready['id']

View File

@ -104,8 +104,7 @@
<img class="avatar" src="$avatar"> <img class="avatar" src="$avatar">
<h2 class="title">$channel_title</h2> <h2 class="title">$channel_title</h2>
<nav class="channel-tabs"> <nav class="channel-tabs">
<a class="tab page-button" href="$channel_videos_url">Videos</a> $channel_tabs
<a class="tab page-button">About</a>
</nav> </nav>
<div class="channel-info"> <div class="channel-info">
<ul> <ul>

View File

@ -82,6 +82,9 @@
.item-grid .video-thumbnail-img{ .item-grid .video-thumbnail-img{
width:168px; width:168px;
} }
.item-grid .playlist-thumbnail-img{
width:168px;
}
.page-button-row{ .page-button-row{
grid-column: 1 / span 2; grid-column: 1 / span 2;
} }
@ -117,8 +120,7 @@
<img class="avatar" src="$avatar"> <img class="avatar" src="$avatar">
<h2 class="title">$channel_title</h2> <h2 class="title">$channel_title</h2>
<nav class="channel-tabs"> <nav class="channel-tabs">
<a class="tab page-button">Videos</a> $channel_tabs
<a class="tab page-button" href="$channel_about_url">About</a>
</nav> </nav>
<div id="number-of-results">$number_of_results</div> <div id="number-of-results">$number_of_results</div>
$items $items