Improve youtube search

Adds pagination, sort modes, etc
This commit is contained in:
pluja 2020-10-05 18:10:29 +02:00
parent 1638252d22
commit d506d556b8
3 changed files with 52 additions and 23 deletions

View File

@ -15,7 +15,7 @@ class SearchForm(FlaskForm):
submit = SubmitField('Search') submit = SubmitField('Search')
class ChannelForm(FlaskForm): class ChannelForm(FlaskForm):
channelId = StringField('') search = StringField('')
submit = SubmitField('Search') submit = SubmitField('Search')

View File

@ -267,26 +267,41 @@ def ytfollowing():
return render_template('ytfollowing.html', form=form, channelList=channelList, channelCount=channelCount, config=config) return render_template('ytfollowing.html', form=form, channelList=channelList, channelCount=channelCount, config=config)
@app.route('/ytsearch', methods=['GET', 'POST']) @app.route('/ytsearch', methods=['GET', 'POST'])
@login_required @login_required
def ytsearch(): def ytsearch():
form = ChannelForm() form = ChannelForm()
button_form = EmptyForm() button_form = EmptyForm()
if form.validate_on_submit(): query = request.args.get('q', None)
searchTerms = form.channelId.data sort = request.args.get('s', None)
page = 1 if sort != None:
autocorrect = 1 sort = int(sort)
else:
sort = 0 sort = 0
page = request.args.get('p', None)
if page == None:
page = 1
if query:
autocorrect = 1
filters = {"time":0, "type":0, "duration":0} filters = {"time":0, "type":0, "duration":0}
results = yts.search_by_terms(searchTerms, page, autocorrect, sort, filters) results = yts.search_by_terms(query, page, autocorrect, sort, filters)
next_page = "/ytsearch?q={q}&s={s}&p={p}".format(q=query, s=sort, p=int(page)+1)
if int(page) == 1:
prev_page = "/ytsearch?q={q}&s={s}&p={p}".format(q=query, s=sort, p=1)
else:
prev_page = "/ytsearch?q={q}&s={s}&p={p}".format(q=query, s=sort, p=int(page)-1)
for channel in results['channels']: for channel in results['channels']:
if config['nginxVideoStream']: if config['nginxVideoStream']:
channel['thumbnail'] = channel['thumbnail'].replace("~", "/") channel['thumbnail'] = channel['thumbnail'].replace("~", "/")
hostName = urllib.parse.urlparse(channel['thumbnail']).netloc hostName = urllib.parse.urlparse(channel['thumbnail']).netloc
channel['thumbnail'] = channel['thumbnail'].replace("https://{}".format(hostName), "")+"?host="+hostName channel['thumbnail'] = channel['thumbnail'].replace("https://{}".format(hostName), "")+"?host="+hostName
print(channel['thumbnail']) print(channel['thumbnail'])
return render_template('ytsearch.html', form=form, btform=button_form, results=results, restricted=config['restrictPublicUsage'], config=config) return render_template('ytsearch.html', form=form, btform=button_form, results=results, restricted=config['restrictPublicUsage'], config=config, npage=next_page, ppage=prev_page)
else: else:
return render_template('ytsearch.html', form=form, results=False) return render_template('ytsearch.html', form=form, results=False)

View File

@ -1,24 +1,25 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block content %} {% block content %}
<div class="ui text container"> <div class="ui center aligned text container">
<form class="ui form" action="" method="post" novalidate> <form action="{{url_for('ytsearch', _method='GET')}}">
{{ form.hidden_tag() }} <div class="ui search">
<p> <input class="prompt" name="q" type="text" placeholder="Search...">
{{ form.channelId.label }}<br> <select name="s" id="sort">
{{ form.channelId(size=32) }}<br> <option value="0">Relevance</option>
{% for error in form.channelId.errors %} <option value="3">Views</option>
<span style="color: red;">[{{ error }}]</span> <option value="2">Date</option>
{% endfor %} <option value="1">Rating</option>
</p> </select>
<p>{{ form.submit() }}</p> </div>
</form> </form>
{% if results %} {% if results %}
<h3 class="ui dividing header">Users</h3> {% if results.channels %}
<div class="ui relaxed divided list"> <h3 class="ui dividing header">Users</h3>
{% endif %}
{% for res in results.channels %} <div class="ui relaxed divided list">
{% for res in results.channels %}
<div class="item"> <div class="item">
<div class="image"> <div class="image">
{% if config.nginxVideoStream %} {% if config.nginxVideoStream %}
@ -77,6 +78,19 @@
</div> </div>
{% endif %} {% endif %}
</div> </div>
{%if ppage%}
<div class="ui text container center aligned">
<a href="{{ppage}}"><button class="ui labeled icon button">
<i class="left arrow icon"></i>
Prev
</button></a>
<a href="{{npage}}"><button class="ui right labeled icon button">
<i class="right arrow icon"></i>
Next
</button></a>
</div>
{%endif%}
</div> </div>
{% endblock %} {% endblock %}