diff options
author | James Taylor <user234683@users.noreply.github.com> | 2018-07-06 17:41:16 -0700 |
---|---|---|
committer | James Taylor <user234683@users.noreply.github.com> | 2018-07-06 17:41:16 -0700 |
commit | 9cefa46bc53d94ee53940449c4d3af0ad6faec0e (patch) | |
tree | 39fc7c8dea15b20203a4cd8f4d110702f49452ae | |
parent | edbc305da9c4f32b18b25b635e5c4b3b16fb107c (diff) | |
download | yt-local-9cefa46bc53d94ee53940449c4d3af0ad6faec0e.tar.lz yt-local-9cefa46bc53d94ee53940449c4d3af0ad6faec0e.tar.xz yt-local-9cefa46bc53d94ee53940449c4d3af0ad6faec0e.zip |
add playlist tab to channels
-rw-r--r-- | youtube/channel.py | 58 | ||||
-rw-r--r-- | youtube/common.py | 8 | ||||
-rw-r--r-- | yt_channel_about_template.html | 3 | ||||
-rw-r--r-- | yt_channel_items_template.html | 6 |
4 files changed, 63 insertions, 12 deletions
diff --git a/youtube/channel.py b/youtube/channel.py index 98014a3..f04ac45 100644 --- a/youtube/channel.py +++ b/youtube/channel.py @@ -158,6 +158,26 @@ def grid_items_html(items, additional_info={}): result += '''\n</nav>''' 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=''): microformat = polymer_json[1]['response']['microformat']['microformatDataRenderer'] 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( 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'], page_title = microformat['title'] + ' - Channel', 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", ) +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(''' <a href="$url">$text</a>''') stat_template = Template(''' @@ -218,7 +264,7 @@ def channel_about_page(polymer_json): description = description, links = channel_links, 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=''): @@ -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 = json.loads(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: 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 = json.loads(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: raise ValueError('Unknown channel page: ' + page)
\ No newline at end of file diff --git a/youtube/common.py b/youtube/common.py index b8fe751..6994064 100644 --- a/youtube/common.py +++ b/youtube/common.py @@ -58,7 +58,7 @@ medium_playlist_item_template = Template(''' </div> </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> </div> @@ -73,10 +73,6 @@ medium_video_item_template = Template(''' <a class="title" href="$url">$title</a> <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="badges">$badges</span> @@ -477,7 +473,7 @@ def video_item_html(item, template, html_exclude=set()): 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['url'] = URL_ORIGIN + "/playlist?list=" + html_ready['id'] diff --git a/yt_channel_about_template.html b/yt_channel_about_template.html index e034ddf..7eba673 100644 --- a/yt_channel_about_template.html +++ b/yt_channel_about_template.html @@ -104,8 +104,7 @@ <img class="avatar" src="$avatar"> <h2 class="title">$channel_title</h2> <nav class="channel-tabs"> - <a class="tab page-button" href="$channel_videos_url">Videos</a> - <a class="tab page-button">About</a> +$channel_tabs </nav> <div class="channel-info"> <ul> diff --git a/yt_channel_items_template.html b/yt_channel_items_template.html index 24ddc30..eb9b1e1 100644 --- a/yt_channel_items_template.html +++ b/yt_channel_items_template.html @@ -82,6 +82,9 @@ .item-grid .video-thumbnail-img{ width:168px; } + .item-grid .playlist-thumbnail-img{ + width:168px; + } .page-button-row{ grid-column: 1 / span 2; } @@ -117,8 +120,7 @@ <img class="avatar" src="$avatar"> <h2 class="title">$channel_title</h2> <nav class="channel-tabs"> - <a class="tab page-button">Videos</a> - <a class="tab page-button" href="$channel_about_url">About</a> +$channel_tabs </nav> <div id="number-of-results">$number_of_results</div> $items |