72 lines
2.1 KiB
Python
72 lines
2.1 KiB
Python
import json
|
|
import os
|
|
from typing import List
|
|
|
|
QUEUE_FILE = "translation_queue.json"
|
|
|
|
|
|
def load_queue() -> List[str]:
|
|
if not os.path.exists(QUEUE_FILE):
|
|
return []
|
|
try:
|
|
with open(QUEUE_FILE, "r", encoding="utf-8") as f:
|
|
data = json.load(f)
|
|
return data.get("queue", [])
|
|
except Exception:
|
|
return []
|
|
|
|
|
|
def save_queue(queue: List[str]):
|
|
with open(QUEUE_FILE, "w", encoding="utf-8") as f:
|
|
json.dump({"queue": queue}, f, ensure_ascii=False, indent=2)
|
|
|
|
|
|
def add_to_queue(filename: str):
|
|
queue = load_queue()
|
|
if filename not in queue:
|
|
queue.append(filename)
|
|
save_queue(queue)
|
|
|
|
|
|
def remove_from_queue(filename: str):
|
|
queue = load_queue()
|
|
if filename in queue:
|
|
queue.remove(filename)
|
|
save_queue(queue)
|
|
|
|
|
|
def translate_from_queue(start_translation_func):
|
|
from utils import notify_telegram
|
|
import threading
|
|
import time
|
|
|
|
queue = load_queue()
|
|
if not queue:
|
|
print("[QUEUE] Черга пуста")
|
|
return
|
|
|
|
filename = queue[0]
|
|
try:
|
|
print(f"[QUEUE] Спроба перекладу з черги: {filename}")
|
|
start_translation_func(filename)
|
|
remove_from_queue(filename)
|
|
notify_telegram(f"[✅] Файл з черги перекладено: {filename}")
|
|
|
|
if load_queue():
|
|
threading.Timer(3, translate_from_queue, args=(start_translation_func,)).start()
|
|
|
|
except Exception as e:
|
|
error_text = str(e).lower()
|
|
if (
|
|
"too many requests" in error_text
|
|
or "you are allowed to make" in error_text
|
|
or "5 requests per second" in error_text
|
|
):
|
|
notify_telegram(
|
|
f"[🛑] Знову ліміт при перекладі з черги. Повтор через 30 хвилин.\nФайл: {filename}"
|
|
)
|
|
threading.Timer(1800, translate_from_queue, args=(start_translation_func,)).start()
|
|
else:
|
|
notify_telegram(f"[⚠️] Помилка при перекладі з черги: {filename}\n{str(e)}")
|
|
remove_from_queue(filename)
|