Reformat via black, fix for Python <3.12 and added Flask-Caching to req
This commit is contained in:
parent
d573da7480
commit
a205ebf3a4
5 changed files with 112 additions and 48 deletions
84
app/app.py
84
app/app.py
|
@ -8,21 +8,26 @@ from flask_sqlalchemy import SQLAlchemy
|
||||||
|
|
||||||
|
|
||||||
from toloka2MediaServer.main_logic import (
|
from toloka2MediaServer.main_logic import (
|
||||||
add_release_by_url, update_release_by_name, update_releases,
|
add_release_by_url,
|
||||||
search_torrents, get_torrent as get_torrent_external,
|
update_release_by_name,
|
||||||
add_torrent as add_torrent_external
|
update_releases,
|
||||||
|
search_torrents,
|
||||||
|
get_torrent as get_torrent_external,
|
||||||
|
add_torrent as add_torrent_external,
|
||||||
)
|
)
|
||||||
|
|
||||||
cache = Cache(config={'CACHE_TYPE': 'SimpleCache'})
|
cache = Cache(config={"CACHE_TYPE": "SimpleCache"})
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///t2w.db"
|
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///t2w.db"
|
||||||
cache.init_app(app)
|
cache.init_app(app)
|
||||||
|
|
||||||
from sqlalchemy.orm import DeclarativeBase, MappedAsDataclass
|
from sqlalchemy.orm import DeclarativeBase, MappedAsDataclass
|
||||||
|
|
||||||
|
|
||||||
class Base(DeclarativeBase, MappedAsDataclass):
|
class Base(DeclarativeBase, MappedAsDataclass):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
db = SQLAlchemy(app, model_class=Base)
|
db = SQLAlchemy(app, model_class=Base)
|
||||||
|
|
||||||
from models.db import ImagesCache
|
from models.db import ImagesCache
|
||||||
|
@ -30,6 +35,7 @@ from models.db import ImagesCache
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
db.create_all()
|
db.create_all()
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
@cache.cached(timeout=1)
|
@cache.cached(timeout=1)
|
||||||
def root_route():
|
def root_route():
|
||||||
|
@ -38,13 +44,21 @@ def root_route():
|
||||||
titles = config.titles_config
|
titles = config.titles_config
|
||||||
new_torrent = 0
|
new_torrent = 0
|
||||||
for title in titles.sections():
|
for title in titles.sections():
|
||||||
if request.args.get('query', '') in title:
|
if request.args.get("query", "") in title:
|
||||||
image_cache = db.session.execute(db.select(ImagesCache).filter_by(codename=title)).scalar_one_or_none()
|
image_cache = db.session.execute(
|
||||||
|
db.select(ImagesCache).filter_by(codename=title)
|
||||||
|
).scalar_one_or_none()
|
||||||
if image_cache:
|
if image_cache:
|
||||||
titles[title]["image"] = image_cache.image
|
titles[title]["image"] = image_cache.image
|
||||||
else:
|
else:
|
||||||
toloka_torrent = config.toloka.get_torrent(f"https://toloka.to/{titles[title]["guid"]}")
|
toloka_torrent = config.toloka.get_torrent(
|
||||||
toloka_img = f"https:{toloka_torrent.img}" if toloka_torrent.img.startswith("//") else toloka_torrent.img
|
f"https://toloka.to/{titles[title]['guid']}"
|
||||||
|
)
|
||||||
|
toloka_img = (
|
||||||
|
f"https:{toloka_torrent.img}"
|
||||||
|
if toloka_torrent.img.startswith("//")
|
||||||
|
else toloka_torrent.img
|
||||||
|
)
|
||||||
db.session.add(ImagesCache(title, toloka_img))
|
db.session.add(ImagesCache(title, toloka_img))
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
|
@ -52,42 +66,59 @@ def root_route():
|
||||||
|
|
||||||
# Config data
|
# Config data
|
||||||
titles[title]["codename"] = title
|
titles[title]["codename"] = title
|
||||||
titles[title]["torrent_name"] = titles[title]["torrent_name"].replace("\"", "")
|
titles[title]["torrent_name"] = titles[title]["torrent_name"].replace(
|
||||||
|
'"', ""
|
||||||
|
)
|
||||||
sections.append(titles[title])
|
sections.append(titles[title])
|
||||||
|
|
||||||
return render_template('index.html', titles = sections, new_torrent = new_torrent)
|
return render_template("index.html", titles=sections, new_torrent=new_torrent)
|
||||||
|
|
||||||
|
|
||||||
# First stage
|
# First stage
|
||||||
@app.route("/add")
|
@app.route("/add")
|
||||||
def add_route():
|
def add_route():
|
||||||
config = initiate_config()
|
config = initiate_config()
|
||||||
if request.args.get('query'):
|
if request.args.get("query"):
|
||||||
torrent = config.toloka.get_torrent(request.args.get('query'))
|
torrent = config.toloka.get_torrent(request.args.get("query"))
|
||||||
return render_template('add.html', torrent = torrent, episode_integers = [i for i in ''.join((ch if ch.isdigit() else ' ') for ch in f"{torrent.files[0].folder_name}/{torrent.files[0].file_name}").split()], default_dir = config.app_config.get("Toloka", "default_download_dir"))
|
return render_template(
|
||||||
|
"add.html",
|
||||||
|
torrent=torrent,
|
||||||
|
episode_integers=[
|
||||||
|
i
|
||||||
|
for i in "".join(
|
||||||
|
(ch if ch.isdigit() else " ")
|
||||||
|
for ch in f"{torrent.files[0].folder_name}/{torrent.files[0].file_name}"
|
||||||
|
).split()
|
||||||
|
],
|
||||||
|
default_dir=config.app_config.get("Toloka", "default_download_dir"),
|
||||||
|
)
|
||||||
|
|
||||||
if len(request.args) == 6:
|
if len(request.args) == 6:
|
||||||
requestData = RequestData(
|
requestData = RequestData(
|
||||||
url = request.args["toloka_url"],
|
url=request.args["toloka_url"],
|
||||||
season = request.args["season-index"],
|
season=request.args["season-index"],
|
||||||
index = int(request.args["episode-index"]),
|
index=int(request.args["episode-index"]),
|
||||||
correction = int(request.args['adjusted-episode-number']),
|
correction=int(request.args["adjusted-episode-number"]),
|
||||||
title = request.args["dirname"],
|
title=request.args["dirname"],
|
||||||
)
|
)
|
||||||
|
|
||||||
config.args = requestData
|
config.args = requestData
|
||||||
operation_result = add_release_by_url(config)
|
operation_result = add_release_by_url(config)
|
||||||
output = serialize_operation_result(operation_result)
|
output = serialize_operation_result(operation_result)
|
||||||
return redirect(url_for('root_route'))
|
return redirect(url_for("root_route"))
|
||||||
|
|
||||||
|
return render_template("add.html")
|
||||||
|
|
||||||
return render_template('add.html')
|
|
||||||
|
|
||||||
@app.route("/about")
|
@app.route("/about")
|
||||||
def about_route():
|
def about_route():
|
||||||
return render_template('about.html')
|
return render_template("about.html")
|
||||||
|
|
||||||
|
|
||||||
@app.route("/settings")
|
@app.route("/settings")
|
||||||
def settings_route():
|
def settings_route():
|
||||||
return render_template('settings.html')
|
return render_template("settings.html")
|
||||||
|
|
||||||
|
|
||||||
@app.route("/delete/<codename>")
|
@app.route("/delete/<codename>")
|
||||||
def delete_route(codename):
|
def delete_route(codename):
|
||||||
|
@ -99,15 +130,14 @@ def delete_route(codename):
|
||||||
|
|
||||||
return f"{codename} успішно видалений."
|
return f"{codename} успішно видалений."
|
||||||
|
|
||||||
@app.route("/update/", defaults={'codename': None})
|
|
||||||
|
@app.route("/update/", defaults={"codename": None})
|
||||||
@app.route("/update/<codename>")
|
@app.route("/update/<codename>")
|
||||||
def update_route(codename):
|
def update_route(codename):
|
||||||
# Process the name to update release
|
# Process the name to update release
|
||||||
try:
|
try:
|
||||||
config = initiate_config()
|
config = initiate_config()
|
||||||
requestData = RequestData(
|
requestData = RequestData(codename=codename)
|
||||||
codename = codename
|
|
||||||
)
|
|
||||||
if codename:
|
if codename:
|
||||||
config.args = requestData
|
config.args = requestData
|
||||||
operation_result = update_release_by_name(config)
|
operation_result = update_release_by_name(config)
|
||||||
|
@ -118,5 +148,5 @@ def update_route(codename):
|
||||||
output = serialize_operation_result(operation_result)
|
output = serialize_operation_result(operation_result)
|
||||||
return output
|
return output
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
message = f'Error: {str(e)}'
|
message = f"Error: {str(e)}"
|
||||||
return {"error": message}
|
return {"error": message}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
from app import db
|
from app import db
|
||||||
from sqlalchemy.orm import Mapped, mapped_column
|
from sqlalchemy.orm import Mapped, mapped_column
|
||||||
|
|
||||||
|
|
||||||
class ImagesCache(db.Model):
|
class ImagesCache(db.Model):
|
||||||
__tablename__ = "image_cache"
|
__tablename__ = "image_cache"
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,17 @@ class RequestData:
|
||||||
title: str = ""
|
title: str = ""
|
||||||
codename: str = ""
|
codename: str = ""
|
||||||
force: bool = False
|
force: bool = False
|
||||||
def __init__(self, url = "", season = 0, index = 0, correction = 0, title = "", codename = "", force=False):
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
url="",
|
||||||
|
season=0,
|
||||||
|
index=0,
|
||||||
|
correction=0,
|
||||||
|
title="",
|
||||||
|
codename="",
|
||||||
|
force=False,
|
||||||
|
):
|
||||||
self.url = url
|
self.url = url
|
||||||
self.season = season
|
self.season = season
|
||||||
self.index = index
|
self.index = index
|
||||||
|
|
|
@ -3,21 +3,24 @@ from toloka2MediaServer.logger_setup import setup_logging
|
||||||
from toloka2MediaServer.models.config import Config
|
from toloka2MediaServer.models.config import Config
|
||||||
from toloka2MediaServer.clients.dynamic import dynamic_client_init
|
from toloka2MediaServer.clients.dynamic import dynamic_client_init
|
||||||
|
|
||||||
def initiate_config():
|
|
||||||
app_config_path='data/app.ini'
|
|
||||||
title_config_path='data/titles.ini'
|
|
||||||
logger_path = 'data/app_web.log'
|
|
||||||
|
|
||||||
app_config, titles_config, application_config = load_configurations(app_config_path,title_config_path)
|
def initiate_config():
|
||||||
toloka=get_toloka_client(application_config)
|
app_config_path = "data/app.ini"
|
||||||
logger=setup_logging(logger_path)
|
title_config_path = "data/titles.ini"
|
||||||
|
logger_path = "data/app_web.log"
|
||||||
|
|
||||||
|
app_config, titles_config, application_config = load_configurations(
|
||||||
|
app_config_path, title_config_path
|
||||||
|
)
|
||||||
|
toloka = get_toloka_client(application_config)
|
||||||
|
logger = setup_logging(logger_path)
|
||||||
|
|
||||||
config = Config(
|
config = Config(
|
||||||
logger=logger,
|
logger=logger,
|
||||||
toloka=toloka,
|
toloka=toloka,
|
||||||
app_config=app_config,
|
app_config=app_config,
|
||||||
titles_config=titles_config,
|
titles_config=titles_config,
|
||||||
application_config=application_config
|
application_config=application_config,
|
||||||
)
|
)
|
||||||
|
|
||||||
client = dynamic_client_init(config)
|
client = dynamic_client_init(config)
|
||||||
|
@ -25,14 +28,33 @@ def initiate_config():
|
||||||
|
|
||||||
return config
|
return config
|
||||||
|
|
||||||
|
|
||||||
def serialize_operation_result(operation_result):
|
def serialize_operation_result(operation_result):
|
||||||
return {
|
return {
|
||||||
"operation_type": operation_result.operation_type.name if operation_result.operation_type else None,
|
"operation_type": (
|
||||||
"torrent_references": [str(torrent) for torrent in operation_result.torrent_references],
|
operation_result.operation_type.name
|
||||||
"titles_references": [str(titles) for titles in operation_result.titles_references],
|
if operation_result.operation_type
|
||||||
|
else None
|
||||||
|
),
|
||||||
|
"torrent_references": [
|
||||||
|
str(torrent) for torrent in operation_result.torrent_references
|
||||||
|
],
|
||||||
|
"titles_references": [
|
||||||
|
str(titles) for titles in operation_result.titles_references
|
||||||
|
],
|
||||||
"status_message": operation_result.status_message,
|
"status_message": operation_result.status_message,
|
||||||
"response_code": operation_result.response_code.name if operation_result.response_code else None,
|
"response_code": (
|
||||||
|
operation_result.response_code.name
|
||||||
|
if operation_result.response_code
|
||||||
|
else None
|
||||||
|
),
|
||||||
"operation_logs": operation_result.operation_logs,
|
"operation_logs": operation_result.operation_logs,
|
||||||
"start_time": operation_result.start_time.isoformat() if operation_result.start_time else None,
|
"start_time": (
|
||||||
"end_time": operation_result.end_time.isoformat() if operation_result.end_time else None
|
operation_result.start_time.isoformat()
|
||||||
|
if operation_result.start_time
|
||||||
|
else None
|
||||||
|
),
|
||||||
|
"end_time": (
|
||||||
|
operation_result.end_time.isoformat() if operation_result.end_time else None
|
||||||
|
),
|
||||||
}
|
}
|
|
@ -2,6 +2,7 @@ git+https://github.com/CakesTwix/toloka2python
|
||||||
git+https://github.com/CakesTwix/Toloka2MediaServer
|
git+https://github.com/CakesTwix/Toloka2MediaServer
|
||||||
Flask
|
Flask
|
||||||
Flask-SQLAlchemy
|
Flask-SQLAlchemy
|
||||||
|
Flask-Caching
|
||||||
SQLAlchemy
|
SQLAlchemy
|
||||||
requests
|
requests
|
||||||
configparser
|
configparser
|
||||||
|
|
Loading…
Reference in a new issue