from flask import Flask, render_template, request, jsonify, redirect, url_for import time from services.torrents import initiate_config, serialize_operation_result from models.request_data import RequestData from flask_caching import Cache from flask_sqlalchemy import SQLAlchemy from toloka2MediaServer.main_logic import ( add_release_by_url, update_release_by_name, update_releases, search_torrents, get_torrent as get_torrent_external, add_torrent as add_torrent_external, ) cache = Cache(config={"CACHE_TYPE": "SimpleCache"}) app = Flask(__name__) app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///t2w.db" cache.init_app(app) from sqlalchemy.orm import DeclarativeBase, MappedAsDataclass class Base(DeclarativeBase, MappedAsDataclass): pass db = SQLAlchemy(app, model_class=Base) from models.db import ImagesCache with app.app_context(): db.create_all() @app.route("/") @cache.cached(timeout=1) def root_route(): sections = [] config = initiate_config() titles = config.titles_config new_torrent = 0 for title in titles.sections(): if request.args.get("query", "") in title: image_cache = db.session.execute( db.select(ImagesCache).filter_by(codename=title) ).scalar_one_or_none() if image_cache: titles[title]["image"] = image_cache.image else: toloka_torrent = config.toloka.get_torrent( 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.commit() titles[title]["image"] = toloka_img # Config data titles[title]["codename"] = title titles[title]["torrent_name"] = titles[title]["torrent_name"].replace( '"', "" ) sections.append(titles[title]) return render_template("index.html", titles=sections, new_torrent=new_torrent) # First stage @app.route("/add") def add_route(): config = initiate_config() if 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"), ) if len(request.args) == 6: requestData = RequestData( url=request.args["toloka_url"], season=request.args["season-index"], index=int(request.args["episode-index"]), correction=int(request.args["adjusted-episode-number"]), title=request.args["dirname"], ) config.args = requestData operation_result = add_release_by_url(config) output = serialize_operation_result(operation_result) return redirect(url_for("root_route")) return render_template("add.html") @app.route("/about") def about_route(): return render_template("about.html") @app.route("/settings") def settings_route(): return render_template("settings.html") @app.route("/delete/") def delete_route(codename): config = initiate_config() titles = config.titles_config titles.remove_section(codename) with open("data/titles.ini", "w") as f: titles.write(f) return f"{codename} успішно видалений." @app.route("/update/", defaults={"codename": None}) @app.route("/update/") def update_route(codename): # Process the name to update release try: config = initiate_config() requestData = RequestData(codename=codename) if codename: config.args = requestData operation_result = update_release_by_name(config) else: config.args = RequestData() operation_result = update_releases(config) output = serialize_operation_result(operation_result) return output except Exception as e: message = f"Error: {str(e)}" return {"error": message}