Загрузка данных


import json
import os
import time
import subprocess
from pathlib import Path

TASKS_DIR = "/data/tasks"
DOWNLOADS_DIR = "/data/downloads"

def process_task(task_file):
    """Обрабатывает одну задачу"""
    try:
        with open(task_file, "r") as f:
            task = json.load(f)
        
        task_id = task["task_id"]
        url = task["url"]
        quality = task["quality"]
        
        print(f"[Worker 1] Обрабатываю задачу {task_id}")
        
        # Получаем название видео через yt-dlp
        result = subprocess.run(
            ["yt-dlp", "--dump-json", url],
            capture_output=True,
            text=True,
            timeout=30
        )
        
        if result.returncode != 0:
            task["status"] = "failed"
            task["error"] = "Не удалось получить информацию о видео"
            with open(task_file, "w") as f:
                json.dump(task, f)
            return
        
        video_data = json.loads(result.stdout)
        video_title = video_data.get("title", "Unknown")
        
        # Создаём папку для видео
        video_dir = os.path.join(DOWNLOADS_DIR, video_title)
        os.makedirs(video_dir, exist_ok=True)
        
        # Проверяем есть ли уже файл нужного качества
        if quality == "best":
            quality_str = "best"
        else:
            quality_str = quality
        
        output_template = os.path.join(video_dir, f"%(title)s.%(ext)s")
        
        # Скачиваем видео
        print(f"[Worker 1] Скачиваю видео {video_title} качество {quality_str}")
        
        cmd = [
            "yt-dlp",
            "-f", f"best[height<={quality_str.replace('p', '')}]" if quality != "best" else "best",
            "-o", output_template,
            url
        ]
        
        result = subprocess.run(cmd, capture_output=True, text=True, timeout=3600)
        
        if result.returncode == 0:
            task["status"] = "completed"
            print(f"[Worker 1] Задача {task_id} завершена")
        else:
            task["status"] = "failed"
            task["error"] = result.stderr
            print(f"[Worker 1] Ошибка: {result.stderr}")
        
        with open(task_file, "w") as f:
            json.dump(task, f)
    
    except Exception as e:
        print(f"[Worker 1] Ошибка обработки: {e}")
        task["status"] = "failed"
        task["error"] = str(e)
        with open(task_file, "w") as f:
            json.dump(task, f)

def main():
    print("[Worker 1] Запущен")
    
    while True:
        try:
            # Ищем pending задачи
            if os.path.exists(TASKS_DIR):
                for task_file in os.listdir(TASKS_DIR):
                    if task_file.endswith(".json"):
                        full_path = os.path.join(TASKS_DIR, task_file)
                        with open(full_path, "r") as f:
                            task = json.load(f)
                        
                        if task.get("status") == "pending":
                            # Меняем статус на in_progress
                            task["status"] = "in_progress"
                            with open(full_path, "w") as f:
                                json.dump(task, f)
                            
                            # Обрабатываем
                            process_task(full_path)
            
            time.sleep(5)  # Проверяем каждые 5 секунд
        
        except Exception as e:
            print(f"[Worker 1] Ошибка в main loop: {e}")
            time.sleep(5)

if __name__ == "__main__":
    main()