Back to flask server + async
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Michel Roux 2021-01-03 20:41:58 +01:00
parent 26900df6fa
commit a769f7fddf
4 changed files with 23 additions and 22 deletions

View File

@ -1,9 +1,8 @@
import logging
from asyncio import get_event_loop, set_event_loop, SelectorEventLoop
from functools import wraps
from operator import attrgetter, itemgetter
from flask import redirect, render_template, request, url_for, abort
from gevent.pywsgi import WSGIServer
from . import utils
from .config import app, auth, ADMIN_USERNAME, ADMIN_PASSWORD, MYSQL_ENABLED, APP_PORT, IS_DEBUG
@ -52,10 +51,14 @@ def colorify(model):
return get_instance(model.link, model.title.keyword).color
@app.context_processor
def inject_user():
return dict(mysql_disabled=not MYSQL_ENABLED)
@app.route('/')
def home():
return render_template('layout.html', search_form=SearchForm(), title='Animes torrents search engine',
mysql_disabled=not MYSQL_ENABLED)
return render_template('layout.html', search_form=SearchForm(), title='Animes torrents search engine')
@app.route('/search')
@ -64,14 +67,16 @@ def search():
if not query:
return redirect(url_for('home'))
return render_template('search.html', search_form=SearchForm(), connectors=run_all(query),
mysql_disabled=not MYSQL_ENABLED)
set_event_loop(SelectorEventLoop())
return render_template('search.html', search_form=SearchForm(),
connectors=get_event_loop().run_until_complete(run_all(query)))
@app.route('/latest')
@app.route('/latest/<int:page>')
def latest(page=1):
torrents = run_all('', return_type=ConnectorReturn.HISTORY, page=page)
set_event_loop(SelectorEventLoop())
torrents = get_event_loop().run_until_complete(run_all('', return_type=ConnectorReturn.HISTORY, page=page))
results = []
for torrent in torrents:
@ -80,8 +85,7 @@ def latest(page=1):
result['self'] = get_instance(result['href'], '')
results.sort(key=itemgetter('date'), reverse=True)
return render_template('latest.html', search_form=SearchForm(), torrents=results, page=page,
mysql_disabled=not MYSQL_ENABLED)
return render_template('latest.html', search_form=SearchForm(), torrents=results, page=page)
@app.route('/list')
@ -188,6 +192,4 @@ def admin_edit(link_id=None):
def run():
logging.basicConfig(level=logging.DEBUG if IS_DEBUG else logging.INFO)
http_server = WSGIServer(('', APP_PORT), app)
http_server.serve_forever()
app.run('0.0.0.0', APP_PORT, IS_DEBUG)

View File

@ -1,3 +1,5 @@
from asyncio import gather
from .animeultime import AnimeUltime
from .core import Other
from .nyaa import Nyaa
@ -5,14 +7,12 @@ from .pantsu import Pantsu
from .yggtorrent import YggTorrent, YggAnimation
def run_all(*args, **kwargs):
return [
Nyaa(*args, **kwargs).run(),
Pantsu(*args, **kwargs).run(),
YggTorrent(*args, **kwargs).run(),
YggAnimation(*args, **kwargs).run(),
AnimeUltime(*args, **kwargs).run(),
]
async def run_all(*args, **kwargs):
return list(await gather(Nyaa(*args, **kwargs).run(),
Pantsu(*args, **kwargs).run(),
YggTorrent(*args, **kwargs).run(),
YggAnimation(*args, **kwargs).run(),
AnimeUltime(*args, **kwargs).run()))
def get_instance(url, query):

View File

@ -191,7 +191,7 @@ class ConnectorCore(ABC):
else:
return ConnectorLang.JP
def run(self):
async def run(self):
if self.on_error:
if self.return_type is ConnectorReturn.SEARCH:
self.search()

View File

@ -11,4 +11,3 @@ cloudscraper==1.2.50
Js2Py==0.70
polling2==0.4.6
dateparser==1.0.0
gevent==20.12.1