Search no longer depends on Invidious

This commit is contained in:
pluja 2020-08-17 11:02:18 +02:00
parent b82ba31661
commit 41f86a6892
3 changed files with 80 additions and 85 deletions

View File

@ -1,10 +1,11 @@
from flask import render_template, flash, redirect, url_for, request, send_from_directory, Markup from flask import render_template, flash, redirect, url_for, request, send_from_directory, Markup
from app.forms import LoginForm, RegistrationForm, EmptyForm, SearchForm, ChannelForm from app.forms import LoginForm, RegistrationForm, EmptyForm, SearchForm, ChannelForm
from app.models import User, twitterPost, ytPost, Post, invidiousFollow
from flask_login import login_user, logout_user, current_user, login_required from flask_login import login_user, logout_user, current_user, login_required
from app.models import User, twitterPost, ytPost, Post, invidiousFollow
from flask import Flask, Response, stream_with_context from flask import Flask, Response, stream_with_context
from requests_futures.sessions import FuturesSession from requests_futures.sessions import FuturesSession
from concurrent.futures import as_completed from concurrent.futures import as_completed
from youtube_search import YoutubeSearch
from werkzeug.urls import url_parse from werkzeug.urls import url_parse
from youtube_dl import YoutubeDL from youtube_dl import YoutubeDL
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
@ -215,38 +216,38 @@ def ytsearch():
form = ChannelForm() form = ChannelForm()
button_form = EmptyForm() button_form = EmptyForm()
if form.validate_on_submit(): if form.validate_on_submit():
ydl = YoutubeDL() channels = []
data = ydl videos = []
channelId = form.channelId.data
c = requests.get('https://{instance}/api/v1/search?type=channel&q={cid}'.format(instance=invidiousInstance, cid=channelId)) searchTerm = form.channelId.data
v = requests.get('https://{instance}/api/v1/search?type=video&q={cid}'.format(instance=invidiousInstance, cid=channelId)) search = YoutubeSearch(searchTerm)
if c.status_code == 200 and v.status_code == 200: chnns = search.channels_to_dict()
results = json.loads(c.content) vids = search.videos_to_dict()
channels = []
videos = [] for v in vids:
for res in results: videos.append({
channels.append({ 'author':v['channel'],
'username':res['author'], 'videoTitle':v['title'],
'channelId':res['authorId'], 'description':Markup(v['long_desc']),
'thumbnail':res['authorThumbnails'][0]['url'], 'id':v['id'],
'subCount':letterify(res['subCount']) 'videoThumb': v['thumbnails'][-1],
}) 'channelUrl':v['url_suffix'],
'views':v['views'],
results = json.loads(v.content) 'timeStamp':v['publishedText']
for data in results: })
videos.append({
'instance':invidiousInstance, for c in chnns:
'author':data['author'], channels.append({
'videoTitle':data['title'], 'username':c['name'],
'description':Markup(data['description'][0:125]+'...'), 'channelId':c['id'],
'id':data['videoId'], 'thumbnail':'https:{}'.format(c['thumbnails'][0]),
'videoThumb': data['videoThumbnails'][4]['url'], 'subCount':letterify(c['suscriberCountText'])
'channelUrl':data['authorUrl'], })
'views':data['viewCount'],
'timeStamp':data['publishedText'] print(channels)
}) print(videos)
return render_template('ytsearch.html', form=form, btform=button_form, channels=channels, videos=videos)
return render_template('ytsearch.html', form=form, btform=button_form, results=channels, videos=videos)
else: else:
return render_template('ytsearch.html', form=form) return render_template('ytsearch.html', form=form)

View File

@ -14,64 +14,56 @@
<p>{{ form.submit() }}</p> <p>{{ form.submit() }}</p>
</form> </form>
<div class="ui one column middle aligned grid">
<div class="ui message">
<div class="header">
Tip: Videos are shown below channels.
</div>
<p>Just scroll down!</p>
</div>
</div>
{% if results %}
<div class="ui one column centered grid"> <div class="ui one column centered grid">
<div class="ui middle aligned divided list"> {% if channels %}
<h3 class="ui dividing header">Users</h3> <div class="ui middle aligned divided list">
{% for res in results %} <h3 class="ui dividing header">Users</h3>
<div class="item">
<div class="right floated content"> {% for res in channels %}
{% if not current_user.is_following_yt(res.channelId) %} <div class="item">
<p> <div class="right floated content">
<form action="{{ url_for('ytfollow', channelId=res.channelId) }}" method="post"> {% if not current_user.is_following_yt(res.channelId) %}
{{ btform.hidden_tag() }} <p>
{{ btform.submit(value='Follow') }} <form action="{{ url_for('ytfollow', channelId=res.channelId) }}" method="post">
</form> {{ btform.hidden_tag() }}
</p> {{ btform.submit(value='Follow') }}
{% else %} </form>
<p> </p>
<form action="{{ url_for('ytunfollow', channelId=res.channelId) }}" method="post"> {% else %}
{{ btform.hidden_tag() }} <p>
{{ btform.submit(value='Unfollow') }} <form action="{{ url_for('ytunfollow', channelId=res.channelId) }}" method="post">
</form> {{ btform.hidden_tag() }}
</p> {{ btform.submit(value='Unfollow') }}
{% endif %} </form>
</div> </p>
<img class="ui avatar image" src="{{ res.thumbnail }}"> {% endif %}
<div class="content"> </div>
{{res.username}} <img class="ui avatar image" src="{{ res.thumbnail }}">
<div class="ui label"> <div class="content">
<i class="user icon"></i> {{res.subCount}} {{res.username}}
<div class="ui label">
<i class="user icon"></i> {{res.subCount}}
</div>
</div>
</div> </div>
</div>
</div>
{% endfor %}
</div>
<div class="ui middle aligned divided list">
<h3 class="ui dividing header">Videos</h3>
{% if videos %}
<div class="ui centered cards">
{% for video in videos %}
{% include '_video_item.html' %}
{% endfor %} {% endfor %}
</div> </div>
{% else %}
{% include '_empty_feed.html' %}
{% endif %} {% endif %}
</div>
<div class="ui middle aligned divided list">
<h3 class="ui dividing header">Videos</h3>
{% if videos %}
<div class="ui centered cards">
{% for video in videos %}
{% include '_video_item.html' %}
{% endfor %}
</div>
{% else %}
{% include '_empty_feed.html' %}
{% endif %}
</div>
</div> </div>
{% endif %}
</div> </div>
{% endblock %} {% endblock %}

View File

@ -44,5 +44,7 @@ webencodings==0.5.1
Werkzeug==1.0.1 Werkzeug==1.0.1
WTForms==2.3.1 WTForms==2.3.1
yarl==1.4.2 yarl==1.4.2
youtube-dl==2020.7.28
youtube-search-fork==1.1.0
zope.event==4.4 zope.event==4.4
zope.interface==5.1.0 zope.interface==5.1.0