133 lines
3.9 KiB
Python
133 lines
3.9 KiB
Python
import pymysql
|
|
import requests
|
|
import json
|
|
import time
|
|
import random
|
|
import os
|
|
import re
|
|
from pathlib import Path
|
|
from selenium import webdriver
|
|
from selenium.webdriver.chrome.options import Options
|
|
|
|
# ============================================================
|
|
# KONFIGURACE
|
|
# ============================================================
|
|
DB_CONFIG = {
|
|
"host": "192.168.1.50",
|
|
"port": 3306,
|
|
"user": "root",
|
|
"password": "Vlado9674+",
|
|
"database": "torrents",
|
|
"charset": "utf8mb4",
|
|
"autocommit": True,
|
|
}
|
|
|
|
COOKIE_FILE = Path("sktorrent_cookies.json")
|
|
BACKUP_DIR = "saved_torrents"
|
|
|
|
|
|
# ============================================================
|
|
# POMOCNÉ FUNKCE
|
|
# ============================================================
|
|
def sanitize_filename(name):
|
|
clean = re.sub(r'[^\w\s\.-]', '', name)
|
|
return clean.strip()[:100]
|
|
|
|
|
|
def get_browser_identity():
|
|
print("🤖 Startuji Selenium (Single Thread Mode)...")
|
|
opts = Options()
|
|
opts.add_argument("--headless=new")
|
|
opts.add_argument("--disable-gpu")
|
|
driver = webdriver.Chrome(options=opts)
|
|
driver.get("https://sktorrent.eu")
|
|
|
|
if COOKIE_FILE.exists():
|
|
with open(COOKIE_FILE, "r", encoding="utf-8") as f:
|
|
cookies_list = json.load(f)
|
|
for c in cookies_list:
|
|
driver.add_cookie(c)
|
|
driver.refresh()
|
|
time.sleep(2)
|
|
|
|
user_agent = driver.execute_script("return navigator.userAgent;")
|
|
browser_cookies = driver.get_cookies()
|
|
driver.quit()
|
|
return user_agent, browser_cookies
|
|
|
|
|
|
# ============================================================
|
|
# MAIN
|
|
# ============================================================
|
|
if __name__ == "__main__":
|
|
if not os.path.exists(BACKUP_DIR):
|
|
os.makedirs(BACKUP_DIR)
|
|
|
|
# 1. Načíst zbývající chyby
|
|
db = pymysql.connect(**DB_CONFIG)
|
|
cursor = db.cursor()
|
|
cursor.execute(
|
|
"SELECT torrent_hash, download_url, title_visible FROM torrents WHERE torrent_content IS NULL AND download_url IS NOT NULL")
|
|
rows = cursor.fetchall()
|
|
|
|
print(f"📋 Zbývá opravit: {len(rows)} položek.")
|
|
if not rows:
|
|
print("🎉 Hotovo! Vše je staženo.")
|
|
exit()
|
|
|
|
# 2. Získat identitu
|
|
ua, cookies = get_browser_identity()
|
|
|
|
session = requests.Session()
|
|
session.headers.update({"User-Agent": ua})
|
|
for c in cookies:
|
|
session.cookies.set(c['name'], c['value'])
|
|
|
|
# 3. Pomalá smyčka (1 vlákno)
|
|
success = 0
|
|
dead_links = 0
|
|
|
|
print("🚀 Spouštím jemné dočištění...")
|
|
|
|
for i, row in enumerate(rows):
|
|
t_hash, url, title = row
|
|
print(f"[{i + 1}/{len(rows)}] {title[:50]}...", end=" ")
|
|
|
|
try:
|
|
# Delší pauza pro stabilitu
|
|
time.sleep(random.uniform(1.5, 3.0))
|
|
|
|
resp = session.get(url, timeout=20) # Delší timeout
|
|
|
|
if resp.status_code == 404:
|
|
print("❌ 404 Nenalezeno (soubor na serveru neexistuje)")
|
|
dead_links += 1
|
|
continue
|
|
|
|
if resp.status_code != 200:
|
|
print(f"❌ Chyba {resp.status_code}")
|
|
continue
|
|
|
|
content = resp.content
|
|
if len(content) > 100:
|
|
# DB
|
|
cursor.execute("UPDATE torrents SET torrent_content = %s WHERE torrent_hash = %s", (content, t_hash))
|
|
|
|
# Disk
|
|
fname = f"{sanitize_filename(title)}_{t_hash[:6]}.torrent"
|
|
with open(os.path.join(BACKUP_DIR, fname), "wb") as f:
|
|
f.write(content)
|
|
|
|
print("✅ OK")
|
|
success += 1
|
|
else:
|
|
print("⚠️ Prázdný soubor")
|
|
|
|
except Exception as e:
|
|
print(f"❌ Selhalo: {e}")
|
|
|
|
db.close()
|
|
print("\n" + "=" * 30)
|
|
print(f"🏁 FINÁLE: Opraveno {success} z {len(rows)}")
|
|
if dead_links > 0:
|
|
print(f"💀 Mrtvé odkazy (404): {dead_links} (ty už opravit nejdou)") |