diff --git a/pynyaata/__init__.py b/pynyaata/__init__.py index c87b6af..ab352a1 100644 --- a/pynyaata/__init__.py +++ b/pynyaata/__init__.py @@ -1,11 +1,14 @@ +import asyncio from datetime import datetime from secrets import token_hex -from flask import Flask, render_template +from flask import Flask, redirect, render_template, request, url_for +from pynyaata.bridge import search_all from pynyaata.forms import SearchForm from pynyaata.translations import current_lang, i18n + app = Flask(__name__) app.config["SECRET_KEY"] = token_hex() @@ -33,7 +36,13 @@ def index(): @app.route("/search") def search(): - return render_template("search.html.j2") + query = request.args.get("q") + if not query: + return redirect(url_for("index")) + + torrents = asyncio.run(search_all(query)) + + return render_template("search.html.j2", torrents=torrents) def run(): diff --git a/pynyaata/bridge/__init__.py b/pynyaata/bridge/__init__.py index 0cb5850..97912ff 100644 --- a/pynyaata/bridge/__init__.py +++ b/pynyaata/bridge/__init__.py @@ -1,6 +1,25 @@ +from asyncio import create_task, gather +from typing import List + from pynyaata.bridge.animeultime import AnimeUltime from pynyaata.bridge.nyaa import EraiRaws, Nyaa from pynyaata.bridge.yggtorrent import YggAnimation, YggTorrent +from pynyaata.types import Bridge, RemoteFile -BRIDGES = [Nyaa(), EraiRaws(), YggTorrent(), YggAnimation(), AnimeUltime()] +BRIDGES: List[Bridge] = [ + Nyaa(), + EraiRaws(), + YggTorrent(), + YggAnimation(), + AnimeUltime(), +] + + +async def search_all(query: str = "", page: int = 1) -> List[RemoteFile]: + tasks = [] + + for bridge in BRIDGES: + tasks.append(create_task(bridge.search(query, page))) + + return await gather(*tasks) diff --git a/pynyaata/bridge/animeultime.py b/pynyaata/bridge/animeultime.py index a792f50..2183a50 100644 --- a/pynyaata/bridge/animeultime.py +++ b/pynyaata/bridge/animeultime.py @@ -35,7 +35,7 @@ class AnimeUltime(Bridge): @cache_data @filter_data - def search(self, query: str = "", page: int = 1) -> List[RemoteFile]: + async def search(self, query: str = "", page: int = 1) -> List[RemoteFile]: response = ( requests.post(self.search_url(query, page), {"search": query}) if query diff --git a/pynyaata/bridge/nyaa.py b/pynyaata/bridge/nyaa.py index 92f4842..1b7707c 100644 --- a/pynyaata/bridge/nyaa.py +++ b/pynyaata/bridge/nyaa.py @@ -39,7 +39,7 @@ class Nyaa(Bridge): @cache_data @filter_data - def search(self, query: str = "", page: int = 1) -> List[RemoteFile]: + async def search(self, query: str = "", page: int = 1) -> List[RemoteFile]: response = requests.get(self.search_url(query, page)) if response.status_code != 200: diff --git a/pynyaata/bridge/yggtorrent.py b/pynyaata/bridge/yggtorrent.py index 8556b99..b322c68 100644 --- a/pynyaata/bridge/yggtorrent.py +++ b/pynyaata/bridge/yggtorrent.py @@ -34,7 +34,7 @@ class YggTorrent(Bridge): @cache_data @filter_data - def search(self, query: str = "", page: int = 1) -> List[RemoteFile]: + async def search(self, query: str = "", page: int = 1) -> List[RemoteFile]: response = FlareRequests().get(self.search_url(query, page)) if response.status_code != 200: diff --git a/pynyaata/cache/__init__.py b/pynyaata/cache/__init__.py index ed5ab5e..b902898 100644 --- a/pynyaata/cache/__init__.py +++ b/pynyaata/cache/__init__.py @@ -9,7 +9,6 @@ from pynyaata.types import Cache from redis import RedisError -CACHE_TIMEOUT = int(getenv("CACHE_TIMEOUT", 60 * 60)) REDIS_URL: Optional[str] = getenv("REDIS_URL") client: Cache = SimpleCache() @@ -25,15 +24,17 @@ if REDIS_URL: def cache_data(f): @wraps(f) - def wrapper(*args, **kwds): + async def wrapper(*args, **kwds): bridge = args[0] - key = f"pynyaata.{bridge.__class__.__name__}.{f.__name__}.{bridge.query}.{bridge.page}" + query = args[1] + page = args[2] + key = f"pynyaata.{bridge.__class__.__name__}.{f.__name__}.{query}.{page}" ret = client.get(key) if ret: return ret - ret = f(*args, **kwds) + ret = await f(*args, **kwds) client.set(key, ret) return ret diff --git a/pynyaata/cache/redis.py b/pynyaata/cache/redis.py index b4fb507..ffaafb2 100644 --- a/pynyaata/cache/redis.py +++ b/pynyaata/cache/redis.py @@ -1,12 +1,15 @@ from json import dumps, loads +from os import getenv from typing import List, Optional -from pynyaata.cache import CACHE_TIMEOUT, REDIS_URL from pynyaata.types import Cache, RemoteFile from redis import ConnectionError, Redis +CACHE_TIMEOUT = int(getenv("CACHE_TIMEOUT", 60 * 60)) +REDIS_URL: Optional[str] = getenv("REDIS_URL") + if not REDIS_URL: raise ConnectionError(f"Invalid REDIS_URL: {REDIS_URL}") diff --git a/pynyaata/cache/simple.py b/pynyaata/cache/simple.py index b6655c8..9987f38 100644 --- a/pynyaata/cache/simple.py +++ b/pynyaata/cache/simple.py @@ -1,10 +1,11 @@ from datetime import datetime, timedelta +from os import getenv from typing import Dict, List, Optional, Tuple -from pynyaata.cache import CACHE_TIMEOUT from pynyaata.types import Cache, RemoteFile +CACHE_TIMEOUT = int(getenv("CACHE_TIMEOUT", 60 * 60)) CACHE_DATA: Dict[str, Tuple[List[RemoteFile], datetime]] = {} diff --git a/pynyaata/filters.py b/pynyaata/filters.py index d32c314..147a1b9 100644 --- a/pynyaata/filters.py +++ b/pynyaata/filters.py @@ -50,8 +50,8 @@ def danger(remotes: List[RemoteFile]) -> List[RemoteFile]: def filter_data(f): @wraps(f) - def wrapper(*args, **kwds): - ret = f(*args, **kwds) + async def wrapper(*args, **kwds): + ret = await f(*args, **kwds) ret = duplicate(ret) ret = inactive(ret) diff --git a/pynyaata/static/css/styles.css b/pynyaata/static/css/styles.css index a6bc531..372fbb4 100644 --- a/pynyaata/static/css/styles.css +++ b/pynyaata/static/css/styles.css @@ -1,3 +1,7 @@ .title { letter-spacing: 0.5rem; } + +.navbar-menu { + box-shadow: none; +} diff --git a/pynyaata/templates/index.html.j2 b/pynyaata/templates/index.html.j2 index 6eb049d..d568d18 100644 --- a/pynyaata/templates/index.html.j2 +++ b/pynyaata/templates/index.html.j2 @@ -3,7 +3,7 @@

{{ _("𝚷😼た") }}

-
+
{{ search_form.q(placeholder=_("Search animes ..."), class="input", value=request.args.get("q", "")) }} diff --git a/pynyaata/templates/layout.html.j2 b/pynyaata/templates/layout.html.j2 index 49c031a..055fd85 100644 --- a/pynyaata/templates/layout.html.j2 +++ b/pynyaata/templates/layout.html.j2 @@ -24,25 +24,27 @@ {% if request.endpoint != "index" %} {% endif %} diff --git a/pynyaata/translations/__init__.py b/pynyaata/translations/__init__.py index 101fb5b..5019b8e 100644 --- a/pynyaata/translations/__init__.py +++ b/pynyaata/translations/__init__.py @@ -3,6 +3,7 @@ from os import path from flask import request + CATALOG_CACHE = {} diff --git a/pynyaata/types.py b/pynyaata/types.py index 23ca567..6b06bfb 100644 --- a/pynyaata/types.py +++ b/pynyaata/types.py @@ -50,7 +50,7 @@ class Bridge(BaseModel, ABC): pass @abstractmethod - def search(self, query: str = "", page: int = 1) -> List[RemoteFile]: + async def search(self, query: str = "", page: int = 1) -> List[RemoteFile]: pass