67 lines
2.0 KiB
Python
67 lines
2.0 KiB
Python
from concurrent.futures import ThreadPoolExecutor
|
|
import threading
|
|
from cachetools import TTLCache
|
|
import threading
|
|
import shutil
|
|
import tempfile
|
|
import os
|
|
import glob
|
|
import time
|
|
from YTProcessing import *
|
|
import redis
|
|
|
|
CHUNK_SIZE = 256 * 1024
|
|
PRE_BUFFER_MB = 5
|
|
QUEUE_SIZE = 64
|
|
HOME_HTML = 'templates/Home.html'
|
|
PLAYER_HTML = 'templates/Player.html'
|
|
|
|
executor = ThreadPoolExecutor(max_workers=8)
|
|
info_cache = TTLCache(maxsize=50, ttl=1800)
|
|
cache_lock = threading.Lock()
|
|
_progress: dict = {}
|
|
|
|
def cleanup_old_tempfiles():
|
|
while True:
|
|
try:
|
|
tmp_base = tempfile.gettempdir()
|
|
cutoff = time.time() - 3600
|
|
for folder in glob.glob(os.path.join(tmp_base, 'tmp*')):
|
|
if os.path.isdir(folder) and os.path.getmtime(folder) < cutoff:
|
|
shutil.rmtree(folder, ignore_errors=True)
|
|
except Exception:
|
|
pass
|
|
time.sleep(600)
|
|
|
|
try:
|
|
redis_client = redis.Redis(host='localhost', port=6379, db=0, decode_responses=True)
|
|
redis_client.ping()
|
|
USE_REDIS = True
|
|
print("Redis connected — using Redis for progress tracking.")
|
|
except Exception:
|
|
USE_REDIS = False
|
|
print("Redis not available — using in-memory progress tracking.")
|
|
|
|
|
|
|
|
|
|
def cleanup_old_tempfiles():
|
|
while True:
|
|
try:
|
|
cutoff = time.time() - 3600
|
|
for folder in glob.glob(os.path.join(tempfile.gettempdir(), 'tmp*')):
|
|
if os.path.isdir(folder) and os.path.getmtime(folder) < cutoff:
|
|
shutil.rmtree(folder, ignore_errors=True)
|
|
except Exception:
|
|
pass
|
|
time.sleep(600)
|
|
|
|
threading.Thread(target=cleanup_old_tempfiles, daemon=True).start()
|
|
def set_progress(url: str, data: dict):
|
|
_progress[url] = data
|
|
|
|
def get_progress(url: str) -> dict:
|
|
return _progress.get(url, {'percent': 0, 'speed': '', 'eta': ''})
|
|
|
|
def clear_progress(url: str):
|
|
_progress.pop(url, None) |