From 75b0c52f5743f837f7dc676fb780882388820e4d Mon Sep 17 00:00:00 2001 From: James Taylor Date: Mon, 17 Dec 2018 02:24:55 -0800 Subject: Ability to sort channel videos by oldest and by views --- youtube/channel.py | 26 +++++++++++++++++++++----- youtube/comments.css | 5 ----- youtube/comments.py | 6 +++--- youtube/shared.css | 5 +++++ youtube/watch.py | 3 --- yt_channel_items_template.html | 28 +++++++++++++++++++++------- 6 files changed, 50 insertions(+), 23 deletions(-) diff --git a/youtube/channel.py b/youtube/channel.py index a2acb23..8bb549e 100644 --- a/youtube/channel.py +++ b/youtube/channel.py @@ -146,7 +146,7 @@ def channel_tabs_html(channel_id, current_tab, search_box_value=''): ) else: result += channel_tab_template.substitute( - href_attribute = 'href="' + URL_ORIGIN + "/channel/" + channel_id + "/" + tab_name.lower() + '"', + href_attribute = ' href="' + URL_ORIGIN + '/channel/' + channel_id + '/' + tab_name.lower() + '"', tab_name = tab_name, ) result += channel_search_template.substitute( @@ -154,11 +154,26 @@ def channel_tabs_html(channel_id, current_tab, search_box_value=''): search_box_value = html.escape(search_box_value), ) return result - - +channel_sort_button_template = Template('''\n$text''') +sorts = {1: 'views', 2: 'oldest', 3: 'newest'} +def channel_sort_buttons_html(channel_id, current_sort): + result = '' + current_sort = int(current_sort) + for i in range(1,4): + if i == current_sort: + result += channel_sort_button_template.substitute( + href_attribute='', + text = 'Sorted by ' + sorts[current_sort] + ) + else: + result += channel_sort_button_template.substitute( + href_attribute=' href="' + URL_ORIGIN + '/channel/' + channel_id + '/videos?sort=' + str(i) + '"', + text = 'Sort by ' + sorts[i] + ) + 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, current_sort=3, number_of_videos = 1000, current_query_string=''): microformat = polymer_json[1]['response']['microformat']['microformatDataRenderer'] channel_url = microformat['urlCanonical'].rstrip('/') channel_id = channel_url[channel_url.rfind('/')+1:] @@ -178,6 +193,7 @@ def channel_videos_html(polymer_json, current_page=1, number_of_videos = 1000, c header = common.get_header(), channel_title = microformat['title'], channel_tabs = channel_tabs_html(channel_id, 'Videos'), + sort_buttons = channel_sort_buttons_html(channel_id, current_sort), avatar = '/' + microformat['thumbnail']['thumbnails'][0]['url'], page_title = microformat['title'] + ' - Channel', items = items_html, @@ -336,7 +352,7 @@ def get_channel_page(url, query_string=''): gevent.joinall(tasks) number_of_videos, polymer_json = tasks[0].value, tasks[1].value - return channel_videos_html(polymer_json, page_number, number_of_videos, query_string) + return channel_videos_html(polymer_json, page_number, sort, number_of_videos, query_string) elif tab == 'about': polymer_json = common.fetch_url('https://www.youtube.com/channel/' + channel_id + '/about?pbj=1', common.desktop_ua + headers_1) polymer_json = json.loads(polymer_json) diff --git a/youtube/comments.css b/youtube/comments.css index bcd5b30..79d88c3 100644 --- a/youtube/comments.css +++ b/youtube/comments.css @@ -43,11 +43,6 @@ grid-column-gap: 10px; justify-content:start; } - .comment-links a{ - background-color: #d0d0d0; - padding: 2px; - justify-self:start; - } .comments{ margin-top:10px; diff --git a/youtube/comments.py b/youtube/comments.py index fe89ac9..abc0a79 100644 --- a/youtube/comments.py +++ b/youtube/comments.py @@ -272,11 +272,11 @@ def get_comments_html(comments): def video_comments(video_id, sort=0, offset=0, lc='', secret_key=''): if settings.enable_comments: post_comment_url = common.URL_ORIGIN + "/post_comment?video_id=" + video_id - post_comment_link = '''Post comment''' + post_comment_link = '''Post comment''' other_sort_url = common.URL_ORIGIN + '/comments?ctoken=' + make_comment_ctoken(video_id, sort=1 - sort, lc=lc) other_sort_name = 'newest' if sort == 0 else 'top' - other_sort_link = '''Sort by ''' + other_sort_name + '''''' + other_sort_link = '''Sort by ''' + other_sort_name + '''''' comment_links = '''