Channel: Change search results to use next and previous page buttons
Because youtube doesn't give the number of search results, so previous behavior would give an error if a page number out of range was selected.
This commit is contained in:
parent
c56fc56fa6
commit
777ed756dc
@ -192,16 +192,10 @@ def get_channel_page_general_url(base_url, tab, request, channel_id=None):
|
|||||||
elif tab == 'playlists':
|
elif tab == 'playlists':
|
||||||
polymer_json = util.fetch_url(base_url+ '/playlists?pbj=1&view=1&sort=' + playlist_sort_codes[sort], util.desktop_ua + headers_1, debug_name='gen_channel_playlists')
|
polymer_json = util.fetch_url(base_url+ '/playlists?pbj=1&view=1&sort=' + playlist_sort_codes[sort], util.desktop_ua + headers_1, debug_name='gen_channel_playlists')
|
||||||
elif tab == 'search' and channel_id:
|
elif tab == 'search' and channel_id:
|
||||||
tasks = (
|
polymer_json = get_channel_search_json(channel_id, query, page_number)
|
||||||
gevent.spawn(get_number_of_videos_channel, channel_id ),
|
|
||||||
gevent.spawn(get_channel_search_json, channel_id, query, page_number)
|
|
||||||
)
|
|
||||||
gevent.joinall(tasks)
|
|
||||||
number_of_videos, polymer_json = tasks[0].value, tasks[1].value
|
|
||||||
elif tab == 'search':
|
elif tab == 'search':
|
||||||
url = base_url + '/search?pbj=1&query=' + urllib.parse.quote(query, safe='')
|
url = base_url + '/search?pbj=1&query=' + urllib.parse.quote(query, safe='')
|
||||||
polymer_json = util.fetch_url(url, util.desktop_ua + headers_1, debug_name='gen_channel_search')
|
polymer_json = util.fetch_url(url, util.desktop_ua + headers_1, debug_name='gen_channel_search')
|
||||||
number_of_videos = 1000
|
|
||||||
else:
|
else:
|
||||||
flask.abort(404, 'Unknown channel tab: ' + tab)
|
flask.abort(404, 'Unknown channel tab: ' + tab)
|
||||||
|
|
||||||
@ -211,7 +205,7 @@ def get_channel_page_general_url(base_url, tab, request, channel_id=None):
|
|||||||
return flask.render_template('error.html', error_message = info['error'])
|
return flask.render_template('error.html', error_message = info['error'])
|
||||||
|
|
||||||
post_process_channel_info(info)
|
post_process_channel_info(info)
|
||||||
if tab in ('videos', 'search'):
|
if tab == 'videos':
|
||||||
info['number_of_videos'] = number_of_videos
|
info['number_of_videos'] = number_of_videos
|
||||||
info['number_of_pages'] = math.ceil(number_of_videos/30)
|
info['number_of_pages'] = math.ceil(number_of_videos/30)
|
||||||
if tab in ('videos', 'playlists'):
|
if tab in ('videos', 'playlists'):
|
||||||
@ -219,6 +213,7 @@ def get_channel_page_general_url(base_url, tab, request, channel_id=None):
|
|||||||
elif tab == 'search':
|
elif tab == 'search':
|
||||||
info['search_box_value'] = query
|
info['search_box_value'] = query
|
||||||
info['header_playlist_names'] = local_playlist.get_playlist_names()
|
info['header_playlist_names'] = local_playlist.get_playlist_names()
|
||||||
|
info['page_number'] = page_number
|
||||||
info['subscribed'] = subscriptions.is_subscribed(info['channel_id'])
|
info['subscribed'] = subscriptions.is_subscribed(info['channel_id'])
|
||||||
|
|
||||||
return flask.render_template('channel.html',
|
return flask.render_template('channel.html',
|
||||||
|
@ -298,6 +298,13 @@ body{
|
|||||||
grid-auto-columns: 40px;
|
grid-auto-columns: 40px;
|
||||||
grid-auto-flow: column;
|
grid-auto-flow: column;
|
||||||
height: 40px;
|
height: 40px;
|
||||||
|
}
|
||||||
|
.next-previous-button-row{
|
||||||
|
margin: 10px 0px;
|
||||||
|
display: flex;
|
||||||
|
justify-self:center;
|
||||||
|
justify-content: center;
|
||||||
|
height: 40px;
|
||||||
}
|
}
|
||||||
.page-button{
|
.page-button{
|
||||||
background-color: var(--interface-color);
|
background-color: var(--interface-color);
|
||||||
@ -305,6 +312,10 @@ body{
|
|||||||
border-width: 2px;
|
border-width: 2px;
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
|
padding: 5px;
|
||||||
|
}
|
||||||
|
.next-page:nth-child(2){ /* only if there's also a previous page button */
|
||||||
|
margin-left: 10px;
|
||||||
}
|
}
|
||||||
.sort-button{
|
.sort-button{
|
||||||
background-color: var(--interface-color);
|
background-color: var(--interface-color);
|
||||||
|
@ -1,4 +1,9 @@
|
|||||||
{% set page_title = channel_name + ' - Channel' %}
|
{% if current_tab == 'search' %}
|
||||||
|
{% set page_title = search_box_value + ' - Page ' + page_number|string %}
|
||||||
|
{% else %}
|
||||||
|
{% set page_title = channel_name + ' - Channel' %}
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
{% extends "base.html" %}
|
{% extends "base.html" %}
|
||||||
{% import "common_elements.html" as common_elements %}
|
{% import "common_elements.html" as common_elements %}
|
||||||
{% block style %}
|
{% block style %}
|
||||||
@ -52,18 +57,29 @@
|
|||||||
#number-of-results{
|
#number-of-results{
|
||||||
font-weight:bold;
|
font-weight:bold;
|
||||||
}
|
}
|
||||||
.item-grid{
|
.content{
|
||||||
padding-left: 20px;
|
grid-row: 4;
|
||||||
grid-row:4;
|
|
||||||
grid-column: 1 / span 2;
|
grid-column: 1 / span 2;
|
||||||
}
|
}
|
||||||
.item-list{
|
.search-content{
|
||||||
width:1000px;
|
width: 800px;
|
||||||
grid-column: 1 / span 2;
|
margin-left: 10px;
|
||||||
}
|
|
||||||
.page-button-row{
|
|
||||||
grid-column: 1 / span 2;
|
|
||||||
}
|
}
|
||||||
|
.item-grid{
|
||||||
|
padding-left: 20px;
|
||||||
|
}
|
||||||
|
.item-list{
|
||||||
|
width:800px;
|
||||||
|
margin: auto;
|
||||||
|
}
|
||||||
|
.page-button-row{
|
||||||
|
margin-left: auto;
|
||||||
|
margin-right: auto;
|
||||||
|
}
|
||||||
|
.next-previous-button-row{
|
||||||
|
margin-left: auto;
|
||||||
|
margin-right: auto;
|
||||||
|
}
|
||||||
.tab{
|
.tab{
|
||||||
padding: 5px 75px;
|
padding: 5px 75px;
|
||||||
}
|
}
|
||||||
@ -137,38 +153,43 @@
|
|||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
{% else %}
|
{% else %}
|
||||||
<div id="links-metadata">
|
<div class="content {{ current_tab + '-content'}}">
|
||||||
{% if current_tab == 'videos' %}
|
<div id="links-metadata">
|
||||||
{% set sorts = [('1', 'views'), ('2', 'oldest'), ('3', 'newest')] %}
|
{% if current_tab == 'videos' %}
|
||||||
<div id="number-of-results">{{ number_of_videos }} videos</div>
|
{% set sorts = [('1', 'views'), ('2', 'oldest'), ('3', 'newest')] %}
|
||||||
{% elif current_tab == 'playlists' %}
|
<div id="number-of-results">{{ number_of_videos }} videos</div>
|
||||||
{% set sorts = [('2', 'oldest'), ('3', 'newest'), ('4', 'last video added')] %}
|
{% elif current_tab == 'playlists' %}
|
||||||
{% else %}
|
{% set sorts = [('2', 'oldest'), ('3', 'newest'), ('4', 'last video added')] %}
|
||||||
{% set sorts = [] %}
|
{% elif current_tab == 'search' %}
|
||||||
{% endif %}
|
<h2 class="page-number">Page {{ page_number }}</h2>
|
||||||
|
|
||||||
{% for sort_number, sort_name in sorts %}
|
|
||||||
{% if sort_number == current_sort.__str__() %}
|
|
||||||
<a class="sort-button">{{ 'Sorted by ' + sort_name }}</a>
|
|
||||||
{% else %}
|
{% else %}
|
||||||
<a class="sort-button" href="{{ channel_url + '/' + current_tab + '?sort=' + sort_number }}">{{ 'Sort by ' + sort_name }}</a>
|
{% set sorts = [] %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endfor %}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{% if current_tab != 'about' %}
|
{% for sort_number, sort_name in sorts %}
|
||||||
|
{% if sort_number == current_sort.__str__() %}
|
||||||
|
<a class="sort-button">{{ 'Sorted by ' + sort_name }}</a>
|
||||||
|
{% else %}
|
||||||
|
<a class="sort-button" href="{{ channel_url + '/' + current_tab + '?sort=' + sort_number }}">{{ 'Sort by ' + sort_name }}</a>
|
||||||
|
{% endif %}
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
|
||||||
<nav class="{{ 'item-list' if current_tab == 'search' else 'item-grid' }}">
|
<nav class="{{ 'item-list' if current_tab == 'search' else 'item-grid' }}">
|
||||||
{% for item_info in items %}
|
{% for item_info in items %}
|
||||||
{{ common_elements.item(item_info, include_author=false) }}
|
{{ common_elements.item(item_info, include_author=false) }}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</nav>
|
</nav>
|
||||||
|
|
||||||
{% if current_tab != 'playlists' %}
|
{% if current_tab == 'videos' %}
|
||||||
<nav class="page-button-row">
|
<nav class="page-button-row">
|
||||||
{{ common_elements.page_buttons(number_of_pages, channel_url + '/' + current_tab, parameters_dictionary) }}
|
{{ common_elements.page_buttons(number_of_pages, channel_url + '/' + current_tab, parameters_dictionary) }}
|
||||||
</nav>
|
</nav>
|
||||||
|
{% elif current_tab == 'search' %}
|
||||||
|
<nav class="next-previous-button-row">
|
||||||
|
{{ common_elements.next_previous_buttons(is_last_page, channel_url + '/' + current_tab, parameters_dictionary) }}
|
||||||
|
</nav>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endif %}
|
</div>
|
||||||
|
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endblock main %}
|
{% endblock main %}
|
||||||
|
@ -90,3 +90,18 @@
|
|||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
{% endmacro %}
|
{% endmacro %}
|
||||||
|
|
||||||
|
{% macro next_previous_buttons(is_last_page, url, parameters_dictionary) %}
|
||||||
|
{% set current_page = parameters_dictionary.get('page', 1)|int %}
|
||||||
|
{% set parameters_dictionary = parameters_dictionary.to_dict() %}
|
||||||
|
|
||||||
|
{% if current_page != 1 %}
|
||||||
|
{% set _ = parameters_dictionary.__setitem__('page', current_page - 1) %}
|
||||||
|
<a class="page-button previous-page" href="{{ url + '?' + parameters_dictionary|urlencode }}">Previous page</a>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
{% if not is_last_page %}
|
||||||
|
{% set _ = parameters_dictionary.__setitem__('page', current_page + 1) %}
|
||||||
|
<a class="page-button next-page" href="{{ url + '?' + parameters_dictionary|urlencode }}">Next page</a>
|
||||||
|
{% endif %}
|
||||||
|
{% endmacro %}
|
||||||
|
@ -59,9 +59,11 @@ def extract_channel_info(polymer_json, tab):
|
|||||||
# get items
|
# get items
|
||||||
info['items'] = []
|
info['items'] = []
|
||||||
if tab in ('videos', 'playlists', 'search'):
|
if tab in ('videos', 'playlists', 'search'):
|
||||||
items, _ = extract_items(response)
|
items, ctoken = extract_items(response)
|
||||||
additional_info = {'author': info['channel_name'], 'author_url': info['channel_url']}
|
additional_info = {'author': info['channel_name'], 'author_url': info['channel_url']}
|
||||||
info['items'] = [extract_item_info(renderer, additional_info) for renderer in items]
|
info['items'] = [extract_item_info(renderer, additional_info) for renderer in items]
|
||||||
|
if tab == 'search':
|
||||||
|
info['is_last_page'] = (ctoken is None)
|
||||||
elif tab == 'about':
|
elif tab == 'about':
|
||||||
items, _ = extract_items(response, item_types={'channelAboutFullMetadataRenderer'})
|
items, _ = extract_items(response, item_types={'channelAboutFullMetadataRenderer'})
|
||||||
if not items:
|
if not items:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user