diff --git a/pynyaata/__init__.py b/pynyaata/__init__.py index 7d99e2d..7bb03d2 100644 --- a/pynyaata/__init__.py +++ b/pynyaata/__init__.py @@ -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/') 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) diff --git a/pynyaata/connectors/__init__.py b/pynyaata/connectors/__init__.py index f4de0d7..9f8d1db 100644 --- a/pynyaata/connectors/__init__.py +++ b/pynyaata/connectors/__init__.py @@ -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): diff --git a/pynyaata/connectors/core.py b/pynyaata/connectors/core.py index 3f62969..02f1a1e 100644 --- a/pynyaata/connectors/core.py +++ b/pynyaata/connectors/core.py @@ -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() diff --git a/requirements.txt b/requirements.txt index f17aa2d..9760648 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,4 +11,3 @@ cloudscraper==1.2.50 Js2Py==0.70 polling2==0.4.6 dateparser==1.0.0 -gevent==20.12.1