Hana Celeste commited on
Update app/byps_logic.py
Browse files- app/byps_logic.py +91 -54
app/byps_logic.py
CHANGED
|
@@ -1,55 +1,92 @@
|
|
| 1 |
-
import aiohttp
|
| 2 |
import asyncio
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
async def
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
"
|
| 39 |
-
"
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
"
|
| 48 |
-
"
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import asyncio
|
| 2 |
+
import random
|
| 3 |
+
from playwright.async_api import async_playwright
|
| 4 |
+
from playwright_stealth import stealth_async
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class BypsApp:
|
| 8 |
+
def __init__(self):
|
| 9 |
+
self.playwright = None
|
| 10 |
+
self.proxy_config = {
|
| 11 |
+
"server": "http://p.webshare.io:80",
|
| 12 |
+
"username": "szusjfnw-rotate",
|
| 13 |
+
"password": "mftzj9sljp5t"
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
async def start(self):
|
| 17 |
+
if not self.playwright:
|
| 18 |
+
self.playwright = await async_playwright().start()
|
| 19 |
+
|
| 20 |
+
async def stop(self):
|
| 21 |
+
if self.playwright:
|
| 22 |
+
await self.playwright.stop()
|
| 23 |
+
self.playwright = None
|
| 24 |
+
|
| 25 |
+
async def fetch(self, url: str):
|
| 26 |
+
if not self.playwright:
|
| 27 |
+
await self.start()
|
| 28 |
+
|
| 29 |
+
browser = None
|
| 30 |
+
try:
|
| 31 |
+
browser = await self.playwright.chromium.launch(
|
| 32 |
+
headless=False,
|
| 33 |
+
proxy=self.proxy_config,
|
| 34 |
+
args=[
|
| 35 |
+
"--no-sandbox",
|
| 36 |
+
"--disable-setuid-sandbox",
|
| 37 |
+
"--disable-dev-shm-usage",
|
| 38 |
+
"--disable-blink-features=AutomationControlled",
|
| 39 |
+
]
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
context = await browser.new_context(
|
| 43 |
+
viewport={"width": 1280, "height": 720},
|
| 44 |
+
user_agent=(
|
| 45 |
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
|
| 46 |
+
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
| 47 |
+
"Chrome/122.0.0.0 Safari/537.36"
|
| 48 |
+
)
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
page = await context.new_page()
|
| 52 |
+
|
| 53 |
+
await stealth_async(page)
|
| 54 |
+
|
| 55 |
+
await page.goto(url, wait_until="domcontentloaded", timeout=60000)
|
| 56 |
+
|
| 57 |
+
for i in range(15):
|
| 58 |
+
title = await page.title()
|
| 59 |
+
html = await page.content()
|
| 60 |
+
|
| 61 |
+
if "just a moment" not in title.lower() and "cloudflare" not in html.lower():
|
| 62 |
+
break
|
| 63 |
+
|
| 64 |
+
try:
|
| 65 |
+
for frame in page.frames:
|
| 66 |
+
if "challenge" in frame.url or "turnstile" in frame.url:
|
| 67 |
+
box = await page.locator(
|
| 68 |
+
'iframe[src*="challenge"], iframe[src*="turnstile"]'
|
| 69 |
+
).bounding_box()
|
| 70 |
+
if box:
|
| 71 |
+
await page.mouse.click(
|
| 72 |
+
box["x"] + box["width"] / 2,
|
| 73 |
+
box["y"] + box["height"] / 2
|
| 74 |
+
)
|
| 75 |
+
except:
|
| 76 |
+
pass
|
| 77 |
+
|
| 78 |
+
await page.mouse.move(
|
| 79 |
+
random.randint(100, 600),
|
| 80 |
+
random.randint(100, 600)
|
| 81 |
+
)
|
| 82 |
+
await asyncio.sleep(3)
|
| 83 |
+
|
| 84 |
+
content = await page.content()
|
| 85 |
+
return {"ok": True, "data": content}
|
| 86 |
+
|
| 87 |
+
except Exception as e:
|
| 88 |
+
return {"ok": False, "error": str(e)}
|
| 89 |
+
|
| 90 |
+
finally:
|
| 91 |
+
if browser:
|
| 92 |
+
await browser.close()
|