172 lines
5.2 KiB
Python
172 lines
5.2 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
|
||
import json
|
||
from pathlib import Path
|
||
from playwright.sync_api import sync_playwright
|
||
|
||
# =============================================================
|
||
# CONFIGURATION
|
||
# =============================================================
|
||
|
||
COOKIE_FILE = Path("sktorrent_cookies.json")
|
||
URL = "https://sktorrent.eu/torrent/torrents.php?active=0"
|
||
|
||
|
||
def load_cookies(context):
|
||
"""Load saved cookies if available."""
|
||
if COOKIE_FILE.exists():
|
||
with open(COOKIE_FILE, "r") as f:
|
||
cookies = json.load(f)
|
||
context.add_cookies(cookies)
|
||
print("🔄 Loaded login cookies.")
|
||
return True
|
||
print("❌ Cookie file not found. Run manual login first.")
|
||
return False
|
||
|
||
|
||
# =============================================================
|
||
# MAIN CODE
|
||
# =============================================================
|
||
|
||
with sync_playwright() as p:
|
||
|
||
# 1️⃣ Launch browser
|
||
browser = p.chromium.launch(
|
||
headless=False,
|
||
args=[
|
||
"--disable-popup-blocking",
|
||
"--disable-background-networking",
|
||
"--disable-notifications",
|
||
"--no-default-browser-check",
|
||
"--no-first-run",
|
||
"--noerrdialogs",
|
||
"--disable-dev-shm-usage",
|
||
"--disable-features=IsolateOrigins,site-per-process",
|
||
"--no-sandbox",
|
||
]
|
||
)
|
||
|
||
# 2️⃣ Create context before any pages exist
|
||
context = browser.new_context()
|
||
|
||
# 3️⃣ Block ALL third-party requests (ads, JS, popups, tracking)
|
||
def block_third_party(route, request):
|
||
url = request.url.lower()
|
||
if "sktorrent.eu" in url:
|
||
route.continue_()
|
||
else:
|
||
print(f"🚫 Blocked third-party request: {url}")
|
||
route.abort()
|
||
|
||
context.route("**/*", block_third_party)
|
||
|
||
# 4️⃣ Block ANY popup windows except the first page
|
||
pages = []
|
||
|
||
def on_new_page(new_page):
|
||
pages.append(new_page)
|
||
if len(pages) == 1:
|
||
print("➡️ Main page created.")
|
||
else:
|
||
print("⚠️ Popup blocked (auto-closed).")
|
||
new_page.close()
|
||
|
||
context.on("page", on_new_page)
|
||
|
||
# 5️⃣ Disable all popup JS functions (window.open, window.close, opener.close)
|
||
context.add_init_script("""
|
||
window.open = () => { console.log("Blocked window.open"); return null; };
|
||
window.close = () => { console.log("Blocked window.close"); };
|
||
|
||
try {
|
||
if (window.opener) {
|
||
window.opener.close = () => { console.log("Blocked opener.close"); };
|
||
}
|
||
} catch (e) {}
|
||
|
||
// Block <a target="_blank">
|
||
document.addEventListener('click', function(e) {
|
||
const el = e.target.closest('a[target="_blank"]');
|
||
if (el) {
|
||
e.preventDefault();
|
||
console.log("Blocked target=_blank");
|
||
}
|
||
}, true);
|
||
|
||
// Block middle-click opening a new tab
|
||
document.addEventListener('auxclick', function(e) {
|
||
e.preventDefault();
|
||
}, true);
|
||
""")
|
||
|
||
# 6️⃣ Create the FIRST page (main page)
|
||
page = context.new_page()
|
||
pages.append(page)
|
||
|
||
# 7️⃣ Load cookies (login)
|
||
load_cookies(context)
|
||
|
||
# 8️⃣ Navigate
|
||
print("🌍 Opening page...")
|
||
page.goto(URL)
|
||
|
||
# Do NOT use networkidle on ad-heavy sites
|
||
page.wait_for_load_state("domcontentloaded")
|
||
page.wait_for_selector("table tr", timeout=15000)
|
||
# Remove popup/overlay elements created by SKTorrent
|
||
page.evaluate("""
|
||
const selectors = [
|
||
'#lightbox', '.lightbox', '#popup', '.popup',
|
||
'.overlay', '#overlay', '.modal', '#modal',
|
||
'div[style*="fixed"]', 'div[style*="position: fixed"]',
|
||
'table[style*="position: fixed"]',
|
||
'iframe', 'frame'
|
||
];
|
||
|
||
selectors.forEach(sel => {
|
||
document.querySelectorAll(sel).forEach(el => {
|
||
console.log("Removing popup element:", sel);
|
||
el.remove();
|
||
});
|
||
});
|
||
|
||
// Remove onclick handlers that trigger popups
|
||
document.querySelectorAll('*').forEach(el => {
|
||
el.onclick = null;
|
||
el.onauxclick = null;
|
||
el.oncontextmenu = null;
|
||
});
|
||
|
||
// Remove timers that trigger delayed popups
|
||
window.setTimeout = () => {};
|
||
window.setInterval = () => {};
|
||
""")
|
||
|
||
print("✔ Page loaded, extracting table rows...")
|
||
|
||
# 9️⃣ Extract all rows
|
||
rows = page.locator("table tr").all()
|
||
print(f"📄 Total rows found (including header): {len(rows)}")
|
||
|
||
# 🔟 Extract SECOND ROW only (your request)
|
||
if len(rows) > 1:
|
||
row = rows[1] # 0 = header, 1 = first data row
|
||
tds = row.locator("td")
|
||
|
||
name = tds.nth(1).inner_text().strip()
|
||
size = tds.nth(2).inner_text().strip()
|
||
seeders = tds.nth(3).inner_text().strip()
|
||
leechers = tds.nth(4).inner_text().strip()
|
||
|
||
print("\n========= SECOND ROW =========")
|
||
print(f"Name: {name}")
|
||
print(f"Size: {size}")
|
||
print(f"Seeders: {seeders}")
|
||
print(f"Leechers: {leechers}")
|
||
print("==============================\n")
|
||
else:
|
||
print("❌ No data rows found!")
|
||
|
||
page.wait_for_timeout(5000)
|