Update scraper4.py
Browse files- scraper4.py +97 -15
scraper4.py
CHANGED
|
@@ -18,17 +18,19 @@ class TorBrowserManager:
|
|
| 18 |
|
| 19 |
@classmethod
|
| 20 |
def get_instance(cls):
|
| 21 |
-
if cls._instance is None:
|
|
|
|
| 22 |
return cls._instance
|
| 23 |
|
| 24 |
async def start(self):
|
| 25 |
async with self._lock:
|
| 26 |
-
if self._setup_done:
|
|
|
|
| 27 |
|
| 28 |
print("[TOR] Inicializujem prehliadač cez Tor SOCKS5...")
|
| 29 |
self.playwright = await async_playwright().start()
|
| 30 |
|
| 31 |
-
#
|
| 32 |
self.context = await self.playwright.chromium.launch_persistent_context(
|
| 33 |
"/tmp/playwright_tor",
|
| 34 |
headless=True,
|
|
@@ -36,29 +38,109 @@ class TorBrowserManager:
|
|
| 36 |
args=["--no-sandbox", "--disable-setuid-sandbox"]
|
| 37 |
)
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
print("[TOR] Čakám na stabilizáciu siete...")
|
| 41 |
await asyncio.sleep(10)
|
| 42 |
self._setup_done = True
|
| 43 |
|
| 44 |
async def get_page(self):
|
| 45 |
-
if not self._setup_done:
|
|
|
|
| 46 |
return await self.context.new_page()
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
manager = TorBrowserManager.get_instance()
|
| 49 |
|
| 50 |
async def search_movies(query):
|
| 51 |
-
print(f"[
|
| 52 |
page = await manager.get_page()
|
| 53 |
try:
|
| 54 |
url = f"{DOMENA}/se/j/json?q={urllib.parse.quote(query)}"
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
except Exception as e:
|
| 60 |
-
print(f"[
|
| 61 |
-
|
| 62 |
-
|
|
|
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
@classmethod
|
| 20 |
def get_instance(cls):
|
| 21 |
+
if cls._instance is None:
|
| 22 |
+
cls._instance = cls()
|
| 23 |
return cls._instance
|
| 24 |
|
| 25 |
async def start(self):
|
| 26 |
async with self._lock:
|
| 27 |
+
if self._setup_done:
|
| 28 |
+
return
|
| 29 |
|
| 30 |
print("[TOR] Inicializujem prehliadač cez Tor SOCKS5...")
|
| 31 |
self.playwright = await async_playwright().start()
|
| 32 |
|
| 33 |
+
# Pripojenie na lokálny Tor proces (port 9050)
|
| 34 |
self.context = await self.playwright.chromium.launch_persistent_context(
|
| 35 |
"/tmp/playwright_tor",
|
| 36 |
headless=True,
|
|
|
|
| 38 |
args=["--no-sandbox", "--disable-setuid-sandbox"]
|
| 39 |
)
|
| 40 |
|
| 41 |
+
# Čakáme, kým Tor nadviaže spojenie
|
|
|
|
| 42 |
await asyncio.sleep(10)
|
| 43 |
self._setup_done = True
|
| 44 |
|
| 45 |
async def get_page(self):
|
| 46 |
+
if not self._setup_done:
|
| 47 |
+
await self.start()
|
| 48 |
return await self.context.new_page()
|
| 49 |
|
| 50 |
+
# Tvoja funkcia pre odolnejšie načítanie cez Tor
|
| 51 |
+
async def goto_with_fallback(self, page, url, timeout=60000):
|
| 52 |
+
for pokus in range(3):
|
| 53 |
+
try:
|
| 54 |
+
print(f"[TOR] Načítavam (Pokus {pokus+1}): {url}")
|
| 55 |
+
await page.goto(url, wait_until="domcontentloaded", timeout=timeout)
|
| 56 |
+
return True
|
| 57 |
+
except Exception as e:
|
| 58 |
+
print(f"[TOR] Pokus {pokus+1} zlyhal: {e}")
|
| 59 |
+
await asyncio.sleep(2)
|
| 60 |
+
return False
|
| 61 |
+
|
| 62 |
manager = TorBrowserManager.get_instance()
|
| 63 |
|
| 64 |
async def search_movies(query):
|
| 65 |
+
print(f"[SEARCH] Hľadám cez Tor: {query}")
|
| 66 |
page = await manager.get_page()
|
| 67 |
try:
|
| 68 |
url = f"{DOMENA}/se/j/json?q={urllib.parse.quote(query)}"
|
| 69 |
+
success = await manager.goto_with_fallback(page, url)
|
| 70 |
+
if success:
|
| 71 |
+
content = await page.inner_text("body")
|
| 72 |
+
return json.loads(content)
|
| 73 |
+
except Exception as e:
|
| 74 |
+
print(f"[SEARCH ERROR] {e}")
|
| 75 |
+
finally:
|
| 76 |
+
await page.close()
|
| 77 |
+
return []
|
| 78 |
+
|
| 79 |
+
async def get_details(slug, media_type):
|
| 80 |
+
print(f"[DETAILS] Info cez Tor: {slug}")
|
| 81 |
+
page = await manager.get_page()
|
| 82 |
+
url = f"{DOMENA}/p/{slug}" if media_type in ["movie", "film"] else f"{DOMENA}/tv/{slug}/S01E01"
|
| 83 |
+
res = {"languages": [], "seasons": []}
|
| 84 |
+
try:
|
| 85 |
+
success = await manager.goto_with_fallback(page, url)
|
| 86 |
+
if not success: return res
|
| 87 |
+
|
| 88 |
+
await asyncio.sleep(2)
|
| 89 |
+
soup = BeautifulSoup(await page.content(), "html.parser")
|
| 90 |
+
|
| 91 |
+
kw_div = soup.find("div", class_="movie-keyword")
|
| 92 |
+
if kw_div:
|
| 93 |
+
res["languages"] = [p.get_text(strip=True) for p in kw_div.find_all(recursive=False)]
|
| 94 |
+
|
| 95 |
+
matches = re.findall(r'Séria\s+(\d+)', soup.get_text())
|
| 96 |
+
for seria in sorted(set(matches), key=int):
|
| 97 |
+
res["seasons"].append({"season": int(seria), "episodes": 0})
|
| 98 |
except Exception as e:
|
| 99 |
+
print(f"[DETAILS ERROR] {e}")
|
| 100 |
+
finally:
|
| 101 |
+
await page.close()
|
| 102 |
+
return res
|
| 103 |
|
| 104 |
+
async def get_stream_url(slug, media_type, season=None, episode=None, lang=None, source_idx=0):
|
| 105 |
+
print(f"[STREAM] Skenujem stream cez Tor: {slug}")
|
| 106 |
+
page = await manager.get_page()
|
| 107 |
+
|
| 108 |
+
url = f"{DOMENA}/p/{slug}" if media_type in ["movie", "film"] else f"{DOMENA}/tv/{slug}/S{int(season):02d}E{int(episode):02d}"
|
| 109 |
+
|
| 110 |
+
params = [f"source={source_idx}"]
|
| 111 |
+
if lang: params.append(f"lng={urllib.parse.quote(lang)}")
|
| 112 |
+
url += "?" + "&".join(params)
|
| 113 |
+
|
| 114 |
+
found = {"stream": None, "vtt": None}
|
| 115 |
+
|
| 116 |
+
async def zachyt_request(request):
|
| 117 |
+
if ".m3u8" in request.url and not found["stream"]:
|
| 118 |
+
found["stream"] = request.url
|
| 119 |
+
if request.url.endswith(".vtt") and not found["vtt"]:
|
| 120 |
+
found["vtt"] = request.url
|
| 121 |
+
|
| 122 |
+
page.on("request", zachyt_request)
|
| 123 |
+
|
| 124 |
+
try:
|
| 125 |
+
# Pomalšie načítavanie cez Tor vyžaduje dlhý timeout (90s)
|
| 126 |
+
success = await manager.goto_with_fallback(page, url, timeout=90000)
|
| 127 |
+
if not success: return found
|
| 128 |
+
|
| 129 |
+
for _ in range(25):
|
| 130 |
+
if found["stream"]: break
|
| 131 |
+
await asyncio.sleep(1)
|
| 132 |
+
|
| 133 |
+
if not found["stream"]:
|
| 134 |
+
# Kliknutie na iframe, ak sa stream nenačítal automaticky
|
| 135 |
+
iframe = page.locator("iframe#frm, video").first
|
| 136 |
+
if await iframe.count() > 0:
|
| 137 |
+
await iframe.click(force=True)
|
| 138 |
+
for _ in range(15):
|
| 139 |
+
if found["stream"]: break
|
| 140 |
+
await asyncio.sleep(1)
|
| 141 |
+
except Exception as e:
|
| 142 |
+
print(f"[STREAM ERROR] {e}")
|
| 143 |
+
finally:
|
| 144 |
+
await page.close()
|
| 145 |
+
|
| 146 |
+
return found
|