2024-09-23 12:15:53 -04:00
|
|
|
import os
|
|
|
|
from flask import Blueprint, jsonify, request, current_app
|
|
|
|
from app.services.torrents import initiate_config, serialize_operation_result
|
|
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
|
|
from app.db import db, ImagesCache
|
|
|
|
from app.models.request_data import RequestData
|
|
|
|
from . import api_bp
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@api_bp.route("/api/titles")
|
|
|
|
def titles_route():
|
|
|
|
sections = []
|
|
|
|
config = initiate_config()
|
|
|
|
titles = config.titles_config
|
2024-09-23 12:17:25 -04:00
|
|
|
|
2024-09-23 12:15:53 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
titles[title]["codename"] = title
|
|
|
|
titles[title]["torrent_name"] = titles[title]["torrent_name"].replace(
|
|
|
|
'"', ""
|
|
|
|
)
|
|
|
|
sections.append(dict(titles[title]))
|
2024-09-23 12:17:25 -04:00
|
|
|
|
2024-09-23 12:15:53 -04:00
|
|
|
return jsonify(sections)
|
|
|
|
|
2024-09-23 12:17:25 -04:00
|
|
|
|
|
|
|
@api_bp.route("/api/add", methods=["POST"])
|
2024-09-23 12:15:53 -04:00
|
|
|
def add_route():
|
|
|
|
config = initiate_config()
|
|
|
|
|
|
|
|
requestData = RequestData(
|
|
|
|
url=request.json["tolokaUrl"],
|
|
|
|
season=request.json["seasonIndex"],
|
2024-09-23 12:17:25 -04:00
|
|
|
index=int(request.json["episodeIndex"].split(".")[0]),
|
2024-09-23 12:15:53 -04:00
|
|
|
correction=int(request.json["adjustedEpisodeNumber"]),
|
|
|
|
title=request.json["dirname"],
|
|
|
|
)
|
|
|
|
|
|
|
|
config.args = requestData
|
|
|
|
operation_result = add_release_by_url(config)
|
|
|
|
output = serialize_operation_result(operation_result)
|
|
|
|
|
|
|
|
return jsonify({"result": True})
|
|
|
|
|
|
|
|
|
|
|
|
@api_bp.route("/api/delete/<codename>")
|
|
|
|
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} успішно видалений."
|
|
|
|
|
|
|
|
|
|
|
|
@api_bp.route("/api/update/", defaults={"codename": None})
|
|
|
|
@api_bp.route("/api/update/<codename>")
|
|
|
|
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}
|