← ritorno
import subprocess
import os
import sys
from datetime import datetime
import importlib.util
# File con i link
FILE_LIST = "gdown_files.txt"
# File di log
LOG_FILE = "gdown_download.log"
# File cookie
COOKIE_FILE = "cookies.txt"
USE_COOKIES = False # Flag globale
def log(message):
timestamp = datetime.now().strftime("[%Y-%m-%d %H:%M:%S]")
line = f"{timestamp} {message}"
print(line)
with open(LOG_FILE, "a", encoding="utf-8") as logf:
logf.write(line + "\n")
def ensure_gdown_installed():
if importlib.util.find_spec("gdown") is None:
log("📦 'gdown' non trovato. Lo installo con pip...")
try:
subprocess.check_call([sys.executable, "-m", "pip", "install", "gdown"])
log("✅ gdown installato correttamente.")
except subprocess.CalledProcessError:
log("❌ Errore nell'installazione di gdown. Interruzione.")
sys.exit(1)
else:
log("✅ 'gdown' è già installato.")
def ask_for_cookie_usage():
global USE_COOKIES
if os.path.exists(COOKIE_FILE):
answer = input("🔐 È stato trovato 'cookies.txt'. Vuoi usarlo per i download privati? (s/n): ").strip().lower()
if answer == "s":
USE_COOKIES = True
log("✅ I cookie verranno utilizzati per i download.")
else:
log("➡️ Cookie ignorati.")
else:
log("ℹ️ Nessun file 'cookies.txt' trovato. I download saranno pubblici.")
def download_files():
if not os.path.exists(FILE_LIST):
log(f"❌ File '{FILE_LIST}' non trovato.")
return
with open(FILE_LIST, "r", encoding="utf-8") as f:
links = [line.strip() for line in f if line.strip()]
if not links:
log("⚠️ Nessun link trovato nel file.")
return
log(f"🚀 Inizio download di {len(links)} file...")
for i, link in enumerate(links, 1):
log(f"⬇️ [{i}/{len(links)}] Download in corso: {link}")
try:
cmd = ["gdown", "--fuzzy", link]
if USE_COOKIES:
cmd += ["--cookies", COOKIE_FILE]
process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True
)
for line in process.stdout:
print(line.strip())
with open(LOG_FILE, "a", encoding="utf-8") as logf:
logf.write(line)
process.wait()
if process.returncode == 0:
log(f"✅ Download completato.")
else:
log(f"❌ Download fallito con codice {process.returncode}.")
except Exception as e:
log(f"❌ Errore durante il download: {e}")
log("🏁 Tutti i download completati.")
if __name__ == "__main__":
ensure_gdown_installed()
ask_for_cookie_usage()
download_files()
if os.name == "nt":
input("Premi INVIO per chiudere...")