Upload 64 files
Browse files- README.md +8 -0
- app/config.py +2 -0
- app/services/browser.py +45 -9
README.md
CHANGED
|
@@ -233,3 +233,11 @@ This project is open-sourced under the [MIT License](LICENSE).
|
|
| 233 |
**Built with β€οΈ by [mrshibly](https://github.com/mrshibly)**
|
| 234 |
|
| 235 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
**Built with β€οΈ by [mrshibly](https://github.com/mrshibly)**
|
| 234 |
|
| 235 |
</div>
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
Hugging Face Secrets Set Karein: Hugging Face Space ke Settings -> Variables and secrets mein jayein aur ye add karein:
|
| 240 |
+
|
| 241 |
+
CHATGPT_SESSION_TOKEN: Isme apna ChatGPT session token dalein (Chrome DevTools -> Application -> Cookies -> __Secure-next-auth.session-token).
|
| 242 |
+
PROXY_URL: (Optional) Agar IP block ho rahi hai, toh apna proxy URL dalein (e.g., http://username:password@ip:port).
|
| 243 |
+
Logs Check Karein: Push karne ke baad agar fir bhi fail hota hai, toh mujhe naye logs dikhaiye. Ab logs mein humein saaf dikhega ki browser login page par hai ya Cloudflare par.
|
app/config.py
CHANGED
|
@@ -16,6 +16,8 @@ class Settings(BaseSettings):
|
|
| 16 |
# --- Browser Engine ---
|
| 17 |
HEADLESS: bool = True
|
| 18 |
BROWSER_TIMEOUT: int = 120000 # milliseconds
|
|
|
|
|
|
|
| 19 |
|
| 20 |
model_config = {
|
| 21 |
"env_file": ".env",
|
|
|
|
| 16 |
# --- Browser Engine ---
|
| 17 |
HEADLESS: bool = True
|
| 18 |
BROWSER_TIMEOUT: int = 120000 # milliseconds
|
| 19 |
+
CHATGPT_SESSION_TOKEN: str = "" # __Secure-next-auth.session-token
|
| 20 |
+
PROXY_URL: str = "" # e.g. http://user:pass@host:port
|
| 21 |
|
| 22 |
model_config = {
|
| 23 |
"env_file": ".env",
|
app/services/browser.py
CHANGED
|
@@ -40,17 +40,23 @@ class BrowserEngine(threading.Thread):
|
|
| 40 |
from playwright.async_api import async_playwright
|
| 41 |
|
| 42 |
print("[PhantomAPI] π Launching browser...")
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
args=[
|
| 47 |
"--disable-blink-features=AutomationControlled",
|
| 48 |
"--no-sandbox",
|
| 49 |
"--disable-gpu",
|
| 50 |
"--disable-dev-shm-usage",
|
| 51 |
"--disable-setuid-sandbox",
|
| 52 |
-
]
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
# ------------------------------------------------------------------
|
| 56 |
# Public API
|
|
@@ -85,26 +91,56 @@ class BrowserEngine(threading.Thread):
|
|
| 85 |
viewport={"width": 1920, "height": 1080},
|
| 86 |
)
|
| 87 |
|
| 88 |
-
# Hide the webdriver flag
|
| 89 |
await context.add_init_script(
|
| 90 |
"Object.defineProperty(navigator, 'webdriver', {get: () => undefined})"
|
| 91 |
)
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
page = await context.new_page()
|
| 94 |
|
| 95 |
try:
|
| 96 |
page.set_default_timeout(settings.BROWSER_TIMEOUT)
|
| 97 |
|
| 98 |
# Navigate to ChatGPT
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
# Type the prompt
|
| 102 |
-
|
|
|
|
| 103 |
await page.fill("#prompt-textarea", prompt)
|
| 104 |
await asyncio.sleep(0.5)
|
| 105 |
await page.press("#prompt-textarea", "Enter")
|
| 106 |
|
| 107 |
# Wait for the assistant to start responding
|
|
|
|
| 108 |
await page.wait_for_selector(
|
| 109 |
'[data-message-author-role="assistant"]',
|
| 110 |
timeout=settings.BROWSER_TIMEOUT,
|
|
|
|
| 40 |
from playwright.async_api import async_playwright
|
| 41 |
|
| 42 |
print("[PhantomAPI] π Launching browser...")
|
| 43 |
+
launcher_args = {
|
| 44 |
+
"headless": settings.HEADLESS,
|
| 45 |
+
"args": [
|
|
|
|
| 46 |
"--disable-blink-features=AutomationControlled",
|
| 47 |
"--no-sandbox",
|
| 48 |
"--disable-gpu",
|
| 49 |
"--disable-dev-shm-usage",
|
| 50 |
"--disable-setuid-sandbox",
|
| 51 |
+
]
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
if settings.PROXY_URL:
|
| 55 |
+
print(f"[PhantomAPI] π Using proxy: {settings.PROXY_URL}")
|
| 56 |
+
launcher_args["proxy"] = {"server": settings.PROXY_URL}
|
| 57 |
+
|
| 58 |
+
self.playwright = await async_playwright().start()
|
| 59 |
+
self.browser = await self.playwright.chromium.launch(**launcher_args)
|
| 60 |
|
| 61 |
# ------------------------------------------------------------------
|
| 62 |
# Public API
|
|
|
|
| 91 |
viewport={"width": 1920, "height": 1080},
|
| 92 |
)
|
| 93 |
|
| 94 |
+
# Hide the webdriver flag
|
| 95 |
await context.add_init_script(
|
| 96 |
"Object.defineProperty(navigator, 'webdriver', {get: () => undefined})"
|
| 97 |
)
|
| 98 |
|
| 99 |
+
# Inject session token if provided
|
| 100 |
+
if settings.CHATGPT_SESSION_TOKEN:
|
| 101 |
+
print("[PhantomAPI] π Injecting session token...")
|
| 102 |
+
await context.add_cookies([{
|
| 103 |
+
"name": "__Secure-next-auth.session-token",
|
| 104 |
+
"value": settings.CHATGPT_SESSION_TOKEN,
|
| 105 |
+
"domain": ".chatgpt.com",
|
| 106 |
+
"path": "/",
|
| 107 |
+
"httpOnly": True,
|
| 108 |
+
"secure": True,
|
| 109 |
+
"sameSite": "Lax"
|
| 110 |
+
}])
|
| 111 |
+
|
| 112 |
page = await context.new_page()
|
| 113 |
|
| 114 |
try:
|
| 115 |
page.set_default_timeout(settings.BROWSER_TIMEOUT)
|
| 116 |
|
| 117 |
# Navigate to ChatGPT
|
| 118 |
+
print(f"[PhantomAPI] π Navigating to ChatGPT...")
|
| 119 |
+
await page.goto("https://chatgpt.com/", wait_until="networkidle")
|
| 120 |
+
|
| 121 |
+
# --- Diagnostic Logging ---
|
| 122 |
+
title = await page.title()
|
| 123 |
+
current_url = page.url
|
| 124 |
+
print(f"[PhantomAPI] π Page Title: '{title}'")
|
| 125 |
+
print(f"[PhantomAPI] π Current URL: {current_url}")
|
| 126 |
+
|
| 127 |
+
if "auth0" in current_url or "login" in current_url:
|
| 128 |
+
print("[PhantomAPI] β οΈ Detected Login/Auth wall.")
|
| 129 |
+
if not settings.CHATGPT_SESSION_TOKEN:
|
| 130 |
+
print("[PhantomAPI] β ERROR: Not logged in. Please set CHATGPT_SESSION_TOKEN.")
|
| 131 |
+
|
| 132 |
+
if "cloudflare" in title.lower() or "hcaptcha" in await page.content():
|
| 133 |
+
print("[PhantomAPI] β οΈ Detected Cloudflare/CAPTCHA wall.")
|
| 134 |
|
| 135 |
# Type the prompt
|
| 136 |
+
print("[PhantomAPI] β¨οΈ Waiting for input box...")
|
| 137 |
+
await page.wait_for_selector("#prompt-textarea", timeout=45000)
|
| 138 |
await page.fill("#prompt-textarea", prompt)
|
| 139 |
await asyncio.sleep(0.5)
|
| 140 |
await page.press("#prompt-textarea", "Enter")
|
| 141 |
|
| 142 |
# Wait for the assistant to start responding
|
| 143 |
+
print("[PhantomAPI] π€ Waiting for response...")
|
| 144 |
await page.wait_for_selector(
|
| 145 |
'[data-message-author-role="assistant"]',
|
| 146 |
timeout=settings.BROWSER_TIMEOUT,
|