From 41f86a6892f831c3eea03da0e21dbd98a7529c17 Mon Sep 17 00:00:00 2001 From: pluja Date: Mon, 17 Aug 2020 11:02:18 +0200 Subject: [PATCH] Search no longer depends on Invidious --- app/routes.py | 67 +++++++++++++------------- app/templates/ytsearch.html | 96 +++++++++++++++++-------------------- requirements.txt | 2 + 3 files changed, 80 insertions(+), 85 deletions(-) diff --git a/app/routes.py b/app/routes.py index b25cb52..a4d0bf6 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,10 +1,11 @@ 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.models import User, twitterPost, ytPost, Post, invidiousFollow 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 requests_futures.sessions import FuturesSession from concurrent.futures import as_completed +from youtube_search import YoutubeSearch from werkzeug.urls import url_parse from youtube_dl import YoutubeDL from bs4 import BeautifulSoup @@ -215,38 +216,38 @@ def ytsearch(): form = ChannelForm() button_form = EmptyForm() if form.validate_on_submit(): - ydl = YoutubeDL() - data = ydl - channelId = form.channelId.data - c = requests.get('https://{instance}/api/v1/search?type=channel&q={cid}'.format(instance=invidiousInstance, cid=channelId)) - v = requests.get('https://{instance}/api/v1/search?type=video&q={cid}'.format(instance=invidiousInstance, cid=channelId)) - if c.status_code == 200 and v.status_code == 200: - results = json.loads(c.content) - channels = [] - videos = [] - for res in results: - channels.append({ - 'username':res['author'], - 'channelId':res['authorId'], - 'thumbnail':res['authorThumbnails'][0]['url'], - 'subCount':letterify(res['subCount']) - }) - - results = json.loads(v.content) - for data in results: - videos.append({ - 'instance':invidiousInstance, - 'author':data['author'], - 'videoTitle':data['title'], - 'description':Markup(data['description'][0:125]+'...'), - 'id':data['videoId'], - 'videoThumb': data['videoThumbnails'][4]['url'], - 'channelUrl':data['authorUrl'], - 'views':data['viewCount'], - 'timeStamp':data['publishedText'] - }) - - return render_template('ytsearch.html', form=form, btform=button_form, results=channels, videos=videos) + channels = [] + videos = [] + + searchTerm = form.channelId.data + search = YoutubeSearch(searchTerm) + chnns = search.channels_to_dict() + vids = search.videos_to_dict() + + for v in vids: + videos.append({ + 'author':v['channel'], + 'videoTitle':v['title'], + 'description':Markup(v['long_desc']), + 'id':v['id'], + 'videoThumb': v['thumbnails'][-1], + 'channelUrl':v['url_suffix'], + 'views':v['views'], + 'timeStamp':v['publishedText'] + }) + + for c in chnns: + channels.append({ + 'username':c['name'], + 'channelId':c['id'], + 'thumbnail':'https:{}'.format(c['thumbnails'][0]), + 'subCount':letterify(c['suscriberCountText']) + }) + + print(channels) + print(videos) + return render_template('ytsearch.html', form=form, btform=button_form, channels=channels, videos=videos) + else: return render_template('ytsearch.html', form=form) diff --git a/app/templates/ytsearch.html b/app/templates/ytsearch.html index 9d966f8..ad2749b 100644 --- a/app/templates/ytsearch.html +++ b/app/templates/ytsearch.html @@ -14,64 +14,56 @@

{{ form.submit() }}

-
-
-
- Tip: Videos are shown below channels. -
-

Just scroll down!

-
-
- - {% if results %}
-
-

Users

- {% for res in results %} -
-
- {% if not current_user.is_following_yt(res.channelId) %} -

-

- {{ btform.hidden_tag() }} - {{ btform.submit(value='Follow') }} -
-

- {% else %} -

-

- {{ btform.hidden_tag() }} - {{ btform.submit(value='Unfollow') }} -
-

- {% endif %} -
- -
- {{res.username}} -
- {{res.subCount}} + {% if channels %} +
+

Users

+ + {% for res in channels %} +
+
+ {% if not current_user.is_following_yt(res.channelId) %} +

+

+ {{ btform.hidden_tag() }} + {{ btform.submit(value='Follow') }} +
+

+ {% else %} +

+

+ {{ btform.hidden_tag() }} + {{ btform.submit(value='Unfollow') }} +
+

+ {% endif %} +
+ +
+ {{res.username}} +
+ {{res.subCount}} +
+
-
-
- {% endfor %} -
- - -
-

Videos

- {% if videos %} -
- {% for video in videos %} - {% include '_video_item.html' %} {% endfor %}
- {% else %} - {% include '_empty_feed.html' %} {% endif %} -
+ + +
+

Videos

+ {% if videos %} +
+ {% for video in videos %} + {% include '_video_item.html' %} + {% endfor %} +
+ {% else %} + {% include '_empty_feed.html' %} + {% endif %} +
- {% endif %}
{% endblock %} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index a0184a5..80b3a72 100644 --- a/requirements.txt +++ b/requirements.txt @@ -44,5 +44,7 @@ webencodings==0.5.1 Werkzeug==1.0.1 WTForms==2.3.1 yarl==1.4.2 +youtube-dl==2020.7.28 +youtube-search-fork==1.1.0 zope.event==4.4 zope.interface==5.1.0