update_0.2 #1
+7
-1
@@ -1,7 +1,13 @@
|
|||||||
# cache.py
|
# cache.py
|
||||||
|
import hashlib
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
def cache_key(url: str, quality: str, audio: bool = False) -> str:
|
||||||
|
key_str = f"{url}_{quality}_{audio}"
|
||||||
|
return hashlib.sha256(key_str.encode()).hexdigest()
|
||||||
|
|
||||||
def cache_path(cache_dir: str, key: str, extension: str) -> str:
|
def cache_path(cache_dir: str, key: str, extension: str) -> str:
|
||||||
subdir = key[:2]
|
subdir = key[:2]
|
||||||
full_dir = os.path.join(cache_dir, subdir)
|
full_dir = os.path.join(cache_dir, subdir)
|
||||||
os.makedirs(full_dir, exist_ok=True) # ← ОБЯЗАТЕЛЬНО
|
os.makedirs(full_dir, exist_ok=True)
|
||||||
return os.path.join(full_dir, f"{key}.{extension}")
|
return os.path.join(full_dir, f"{key}.{extension}")
|
||||||
+6
-12
@@ -1,10 +1,9 @@
|
|||||||
# main.py
|
# main.py — ИСПРАВЛЕНО: добавлен import cache_key
|
||||||
import os
|
import os
|
||||||
import asyncio
|
import asyncio
|
||||||
import threading
|
import threading
|
||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
from datetime import datetime
|
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from aiogram import Bot, Dispatcher, F
|
from aiogram import Bot, Dispatcher, F
|
||||||
@@ -33,7 +32,7 @@ from downloader import (
|
|||||||
from middleware import PrivateMiddleware
|
from middleware import PrivateMiddleware
|
||||||
from rate_limit import check_rate_limit
|
from rate_limit import check_rate_limit
|
||||||
from info import extract_info, is_playlist, get_platform_info
|
from info import extract_info, is_playlist, get_platform_info
|
||||||
from cache import cache_path
|
from cache import cache_key, cache_path # ← ИСПРАВЛЕНО: добавлен cache_key
|
||||||
from cleanup import cleanup_tmp
|
from cleanup import cleanup_tmp
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
@@ -56,7 +55,6 @@ USER_URLS: dict[int, str] = {}
|
|||||||
USER_DATA: dict[int, dict] = {}
|
USER_DATA: dict[int, dict] = {}
|
||||||
ACTIVE_DOWNLOADS: dict[int, dict] = {}
|
ACTIVE_DOWNLOADS: dict[int, dict] = {}
|
||||||
|
|
||||||
# Вспомогательные функции
|
|
||||||
def render_bar(percent: float, size: int = 10) -> str:
|
def render_bar(percent: float, size: int = 10) -> str:
|
||||||
filled = int(size * percent / 100)
|
filled = int(size * percent / 100)
|
||||||
return "█" * filled + "░" * (size - filled)
|
return "█" * filled + "░" * (size - filled)
|
||||||
@@ -89,7 +87,7 @@ def make_progress_cb(loop, message):
|
|||||||
asyncio.run_coroutine_threadsafe(update(d), loop)
|
asyncio.run_coroutine_threadsafe(update(d), loop)
|
||||||
return cb
|
return cb
|
||||||
|
|
||||||
# ---------------------- AUTO DOWNLOADS (TikTok/Instagram) ----------------------
|
# ---------------------- AUTO DOWNLOADS (TikTok/Instagram) ------------------
|
||||||
|
|
||||||
async def process_tiktok_auto(message: Message, user_id: int, url: str):
|
async def process_tiktok_auto(message: Message, user_id: int, url: str):
|
||||||
status = await message.answer("🎵 <b>Загружаю TikTok (видео + аудио)...</b>", parse_mode="HTML")
|
status = await message.answer("🎵 <b>Загружаю TikTok (видео + аудио)...</b>", parse_mode="HTML")
|
||||||
@@ -97,7 +95,6 @@ async def process_tiktok_auto(message: Message, user_id: int, url: str):
|
|||||||
key_video = cache_key(url, "tiktok_video", audio=False)
|
key_video = cache_key(url, "tiktok_video", audio=False)
|
||||||
key_audio = cache_key(url, "tiktok_audio", audio=True)
|
key_audio = cache_key(url, "tiktok_audio", audio=True)
|
||||||
|
|
||||||
# ✅ ИСПРАВЛЕНО: создаём подкаталоги в TMP
|
|
||||||
video_cache = cache_path(CACHE_DIR, key_video, "mp4")
|
video_cache = cache_path(CACHE_DIR, key_video, "mp4")
|
||||||
tmp_video = cache_path(TMP_DIR, key_video, "mp4")
|
tmp_video = cache_path(TMP_DIR, key_video, "mp4")
|
||||||
os.makedirs(os.path.dirname(tmp_video), exist_ok=True)
|
os.makedirs(os.path.dirname(tmp_video), exist_ok=True)
|
||||||
@@ -224,7 +221,7 @@ async def process_instagram_auto(message: Message, user_id: int, url: str):
|
|||||||
ACTIVE_DOWNLOADS.pop(user_id, None)
|
ACTIVE_DOWNLOADS.pop(user_id, None)
|
||||||
cleanup_tmp(TMP_DIR)
|
cleanup_tmp(TMP_DIR)
|
||||||
|
|
||||||
# ---------------------- HANDLERS ----------------------
|
# ---------------------- HANDLERS ------------------
|
||||||
|
|
||||||
@dp.message(F.text == "/start")
|
@dp.message(F.text == "/start")
|
||||||
async def start(message: Message):
|
async def start(message: Message):
|
||||||
@@ -275,8 +272,6 @@ async def handle_link(message: Message):
|
|||||||
parse_mode="HTML"
|
parse_mode="HTML"
|
||||||
)
|
)
|
||||||
|
|
||||||
# ---------------------- YOUTUBE/VK HANDLERS ----------------------
|
|
||||||
|
|
||||||
@dp.callback_query(F.data.startswith("q:"))
|
@dp.callback_query(F.data.startswith("q:"))
|
||||||
async def handle_video(callback: CallbackQuery):
|
async def handle_video(callback: CallbackQuery):
|
||||||
await callback.answer()
|
await callback.answer()
|
||||||
@@ -329,7 +324,6 @@ async def handle_video(callback: CallbackQuery):
|
|||||||
await status.edit_text("⛔ Загрузка отменена")
|
await status.edit_text("⛔ Загрузка отменена")
|
||||||
return
|
return
|
||||||
|
|
||||||
# ✅ ИСПРАВЛЕНО: просто перемещаем без оптимизации
|
|
||||||
if os.path.exists(final_path): os.remove(final_path)
|
if os.path.exists(final_path): os.remove(final_path)
|
||||||
os.makedirs(os.path.dirname(final_path), exist_ok=True)
|
os.makedirs(os.path.dirname(final_path), exist_ok=True)
|
||||||
os.rename(tmp_path, final_path)
|
os.rename(tmp_path, final_path)
|
||||||
@@ -439,7 +433,7 @@ async def handle_audio(callback: CallbackQuery):
|
|||||||
logger.error(f"Error sending audio: {e}")
|
logger.error(f"Error sending audio: {e}")
|
||||||
await status.edit_text("❌ Ошибка при отправке аудио")
|
await status.edit_text("❌ Ошибка при отправке аудио")
|
||||||
|
|
||||||
# ---------------------- PLAYLIST HANDLERS ----------------------
|
# ---------------------- PLAYLIST HANDLERS ------------------
|
||||||
@dp.callback_query(F.data == "playlist_all")
|
@dp.callback_query(F.data == "playlist_all")
|
||||||
async def handle_playlist_all(callback: CallbackQuery):
|
async def handle_playlist_all(callback: CallbackQuery):
|
||||||
await callback.answer()
|
await callback.answer()
|
||||||
@@ -602,7 +596,7 @@ async def cancel_download(callback: CallbackQuery):
|
|||||||
else:
|
else:
|
||||||
await callback.answer("❌ Нет активной загрузки", show_alert=True)
|
await callback.answer("❌ Нет активной загрузки", show_alert=True)
|
||||||
|
|
||||||
# ---------------------- ENTRYPOINT ----------------------
|
# ---------------------- ENTRYPOINT ------------------
|
||||||
async def main():
|
async def main():
|
||||||
cleanup_tmp(TMP_DIR)
|
cleanup_tmp(TMP_DIR)
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user