This commit is contained in:
2026-05-20 22:26:48 +02:00
parent 8121b70e87
commit 8ddec5184d
2 changed files with 133 additions and 120 deletions
+75 -65
View File
@@ -30,10 +30,11 @@ HOW_MANY_TO_CHECK = 0
COOKIE_FILE = Path("sktorrent_cookies.json")
BASE_URL = (
"https://sktorrent.eu/torrent/torrents.php"
"?active=0&category=23&order=data&by=DESC"
)
CATEGORIES = {
24: "Knihy a časopisy",
32: "Mluvené slovo"
}
MAX_PAGES = 10
SLEEP_BETWEEN_PAGES = 2.0 # pauza mezi stránkami
SLEEP_BEFORE_DOWNLOAD = 1.5 # pauza před stažením každého .torrent souboru
@@ -221,6 +222,8 @@ def main():
else:
mode_desc = f"kontrola posledních {HOW_MANY_TO_CHECK} torrentů"
print(f"Pořadí: nejnovější → nejstarší | {mode_desc}")
print(f"Kategorie: {', '.join(CATEGORIES.values())}")
print(f"Stránek na kategorii: {MAX_PAGES}")
print("=" * 60)
session = build_session()
@@ -230,79 +233,87 @@ def main():
new_count = 0
checked_count = 0
skipped_count = 0
page = 0
stop = False
while not stop:
url = f"{BASE_URL}&page={page}"
r = None
for attempt in range(1, 6):
try:
r = session.get(url, timeout=15)
r.raise_for_status()
break
except Exception as e:
if attempt < 5:
print(f"⚠️ Stránka {page} — pokus {attempt}/5 selhal: {e} — čekám 10s")
time.sleep(10)
else:
print(f"⚠️ Stránka {page} — všech 5 pokusů selhalo: {e}")
if r is None or not r.ok:
for category_id, category_name in CATEGORIES.items():
if stop:
break
if "login.php" in r.url or "Prihlas sa" in r.text:
print("❌ Cookies expiraly — spusť přihlašovací Selenium skript a obnov cookies.")
break
print(f"\n📚 Kategorie: {category_name} (ID: {category_id})")
rows = parse_page(r.text)
for page in range(MAX_PAGES):
if not rows:
print(f" Stránka {page} — žádné záznamy, konec.")
break
print(f"\n📄 Stránka {page} ({len(rows)} torrentů)")
for item in rows:
if HOW_MANY_TO_CHECK > 0 and checked_count >= HOW_MANY_TO_CHECK:
print(f" ⏹ Zkontrolováno {checked_count} torrentů — limit dosažen.")
stop = True
break
checked_count += 1
cursor.execute(
"SELECT 1 FROM torrents WHERE torrent_hash = %s",
(item["torrent_hash"],)
url = (
"https://sktorrent.eu/torrent/torrents.php"
f"?active=0&category={category_id}&order=data&by=DESC&page={page}"
)
exists = cursor.fetchone()
r = None
for attempt in range(1, 6):
try:
r = session.get(url, timeout=15)
r.raise_for_status()
break
except Exception as e:
if attempt < 5:
print(f"⚠️ Stránka {page} — pokus {attempt}/5 selhal: {e} — čekám 10s")
time.sleep(10)
else:
print(f"⚠️ Stránka {page} — všech 5 pokusů selhalo: {e}")
if r is None or not r.ok:
break
if exists:
if HOW_MANY_TO_CHECK == -1:
print(f" ⏹ Již v DB: {item['title_visible']} → zastavuji import.")
if "login.php" in r.url or "Prihlas sa" in r.text:
print("❌ Cookies expiraly — spusť přihlašovací Selenium skript a obnov cookies.")
break
rows = parse_page(r.text)
if not rows:
print(f" Stránka {page} — žádné záznamy, konec.")
break
print(f"\n📄 Stránka {page} ({len(rows)} torrentů)")
for item in rows:
if HOW_MANY_TO_CHECK > 0 and checked_count >= HOW_MANY_TO_CHECK:
print(f" ⏹ Zkontrolováno {checked_count} torrentů — limit dosažen.")
stop = True
break
checked_count += 1
cursor.execute(
"SELECT 1 FROM torrents WHERE torrent_hash = %s",
(item["torrent_hash"],)
)
exists = cursor.fetchone()
if exists:
if HOW_MANY_TO_CHECK == -1:
print(f" ⏹ Již v DB: {item['title_visible']} → zastavuji import.")
stop = True
break
else:
skipped_count += 1
print(f" ⏭ Již v DB: {item['title_visible']} — přeskakuji")
continue
print(f" ⬇️ Nový: {item['title_visible']}")
time.sleep(SLEEP_BEFORE_DOWNLOAD)
content = download_torrent(session, item["download_url"])
if content:
print(f" ✔ Staženo ({len(content):,} B)")
else:
skipped_count += 1
print(f" ⏭ Již v DB: {item['title_visible']} — přeskakuji")
continue
print(f" ✖ Nepodařilo se stáhnout, ukládám bez obsahu")
print(f" ⬇️ Nový: {item['title_visible']}")
time.sleep(SLEEP_BEFORE_DOWNLOAD)
item["torrent_content"] = content
cursor.execute(INSERT_SQL, item)
new_count += 1
content = download_torrent(session, item["download_url"])
if content:
print(f" ✔ Staženo ({len(content):,} B)")
else:
print(f" ✖ Nepodařilo se stáhnout, ukládám bez obsahu")
item["torrent_content"] = content
cursor.execute(INSERT_SQL, item)
new_count += 1
if not stop:
page += 1
if stop:
break
time.sleep(SLEEP_BETWEEN_PAGES)
# ============================================================
@@ -314,7 +325,6 @@ def main():
print(f"Nových torrentů uloženo : {new_count}")
print(f"Zkontrolováno celkem : {checked_count}")
print(f"Přeskočeno (v DB) : {skipped_count}")
print(f"Stránek prošlo : {page}")
print("=" * 60)
db.close()