This repository has been archived on 2023-10-01. You can view files and clone it, but cannot push or open issues or pull requests.
PyNyaaTa/pynyaata/__init__.py
2023-01-05 00:49:54 +01:00

74 lines
1.6 KiB
Python

import asyncio
from datetime import datetime
from os import getenv
from secrets import token_hex
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
from sentry_sdk import init
from sentry_sdk.integrations.asyncio import AsyncioIntegration
from sentry_sdk.integrations.flask import FlaskIntegration
from sentry_sdk.integrations.redis import RedisIntegration
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
SENTRY_DNS = getenv("SENTRY_DSN")
if SENTRY_DNS:
init(
SENTRY_DNS,
integrations=[
AsyncioIntegration(),
FlaskIntegration(),
RedisIntegration(),
SqlalchemyIntegration(),
],
)
app = Flask(__name__)
app.config["SECRET_KEY"] = token_hex()
@app.context_processor
def utility_processor():
current_date = datetime.now()
apocalypse_left = (
datetime(current_date.year + 5 - (current_date.year % 5), 6, 4, 0, 1)
- current_date
)
return dict(
_=i18n,
current_lang=current_lang(),
search_form=SearchForm(),
apocalypse_left=apocalypse_left,
)
@app.route("/")
def index():
return render_template("index.html.j2")
@app.route("/search")
def search():
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():
app.run("0.0.0.0")
if __name__ == "__main__":
run()