Ability to sort channel videos by oldest and by views

This commit is contained in:
James Taylor 2018-12-17 02:24:55 -08:00
parent 4825337f3b
commit 75b0c52f57
6 changed files with 50 additions and 23 deletions

View File

@ -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<a class="sort-button"$href_attribute>$text</a>''')
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)

View File

@ -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;

View File

@ -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 = '''<a class="post-comment-link" href="''' + post_comment_url + '''">Post comment</a>'''
post_comment_link = '''<a class="sort-button" href="''' + post_comment_url + '''">Post comment</a>'''
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 = '''<a href="''' + other_sort_url + '''">Sort by ''' + other_sort_name + '''</a>'''
other_sort_link = '''<a class="sort-button" href="''' + other_sort_url + '''">Sort by ''' + other_sort_name + '''</a>'''
comment_links = '''<div class="comment-links">\n'''
comment_links += other_sort_link + '\n' + post_comment_link + '\n'
@ -355,7 +355,7 @@ def get_comments_page(query_string):
other_sort_url = common.URL_ORIGIN + '/comments?ctoken=' + make_comment_ctoken(metadata['video_id'], sort=1 - metadata['sort'])
other_sort_name = 'newest' if metadata['sort'] == 0 else 'top'
other_sort_link = '''<a href="''' + other_sort_url + '''">Sort by ''' + other_sort_name + '''</a>'''
other_sort_link = '''<a class="sort-button" href="''' + other_sort_url + '''">Sort by ''' + other_sort_name + '''</a>'''
comment_links = '''<div class="comment-links">\n'''

View File

@ -365,3 +365,8 @@ address{
font-weight: bold;
text-align: center;
}
.sort-button{
background-color: #d0d0d0;
padding: 2px;
justify-self: start;
}

View File

@ -325,9 +325,6 @@ def get_watch_page(query_string):
note = html.escape(downloader._format_note(format)),
)
post_comment_url = common.URL_ORIGIN + "/post_comment?v=" + id
post_comment_link = '''<a class="post-comment-link" href="''' + post_comment_url + '''">Post comment</a>'''
page = yt_watch_template.substitute(
video_title = html.escape(info["title"]),

View File

@ -9,8 +9,8 @@
<style type="text/css">
main{
display:grid;
grid-template-rows: 0fr 0fr 0fr;
grid-template-columns: 0fr 1fr;
grid-template-rows: repeat(5, 0fr);
grid-template-columns: auto 1fr;
}
main .avatar{
grid-row:1;
@ -33,6 +33,20 @@
background-color: #aaaaaa;
padding: 3px;
}
#links-metadata{
display: grid;
grid-auto-flow: column;
grid-column-gap: 10px;
grid-column: 1/span 2;
justify-content: start;
padding-top: 8px;
padding-bottom: 8px;
background-color: #bababa;
margin-bottom: 10px;
}
#number-of-results{
font-weight:bold;
}
.item-grid{
grid-row:4;
grid-column: 1 / span 2;
@ -43,10 +57,7 @@
.tab{
padding: 5px 75px;
}
#number_of_results{
font-weight:bold;
grid-row:3;
}
</style>
</head>
<body>
@ -57,7 +68,10 @@ $header
<nav class="channel-tabs">
$channel_tabs
</nav>
<div id="number-of-results">$number_of_results</div>
<div id="links-metadata">
<div id="number-of-results">$number_of_results</div>
$sort_buttons
</div>
$items
<nav class="page-button-row">
$page_buttons