Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
# main.py
|
| 2 |
import os
|
|
|
|
| 3 |
from fastapi import FastAPI
|
| 4 |
from fastapi.responses import JSONResponse
|
| 5 |
from playwright.async_api import async_playwright
|
| 6 |
-
|
| 7 |
from turnstile import solve_turnstile
|
| 8 |
|
| 9 |
app = FastAPI()
|
|
@@ -15,12 +15,12 @@ BROWSER_LIMIT = int(os.getenv("browserLimit", 20))
|
|
| 15 |
_state = {"browser_limit": BROWSER_LIMIT}
|
| 16 |
|
| 17 |
|
| 18 |
-
@app.get("/")
|
| 19 |
-
async def
|
| 20 |
return {
|
| 21 |
"message": "Server is running!",
|
| 22 |
"domain": DOMAIN,
|
| 23 |
-
"endpoint": f"{DOMAIN}/turnstile",
|
| 24 |
"status": {
|
| 25 |
"browserLimit": _state["browser_limit"],
|
| 26 |
"timeOut": TIMEOUT,
|
|
@@ -35,9 +35,7 @@ async def turnstile_route(body: dict):
|
|
| 35 |
proxy = body.get("proxy")
|
| 36 |
|
| 37 |
if not domain or not site_key:
|
| 38 |
-
return JSONResponse(
|
| 39 |
-
{"message": "Missing domain or siteKey"}, status_code=400
|
| 40 |
-
)
|
| 41 |
|
| 42 |
if _state["browser_limit"] <= 0:
|
| 43 |
return JSONResponse({"message": "Too Many Requests"}, status_code=429)
|
|
@@ -47,7 +45,6 @@ async def turnstile_route(body: dict):
|
|
| 47 |
|
| 48 |
try:
|
| 49 |
pw = await async_playwright().start()
|
| 50 |
-
|
| 51 |
launch_args = {
|
| 52 |
"headless": True,
|
| 53 |
"args": ["--no-sandbox", "--disable-dev-shm-usage"],
|
|
@@ -56,7 +53,6 @@ async def turnstile_route(body: dict):
|
|
| 56 |
launch_args["proxy"] = {
|
| 57 |
"server": f"http://{proxy['hostname']}:{proxy['port']}"
|
| 58 |
}
|
| 59 |
-
|
| 60 |
browser = await pw.chromium.launch(**launch_args)
|
| 61 |
context = await browser.new_context()
|
| 62 |
page = await context.new_page()
|
|
@@ -66,6 +62,7 @@ async def turnstile_route(body: dict):
|
|
| 66 |
await route.abort()
|
| 67 |
else:
|
| 68 |
await route.continue_()
|
|
|
|
| 69 |
await page.route("**/*", block)
|
| 70 |
await page.goto("about:blank")
|
| 71 |
|
|
@@ -76,9 +73,20 @@ async def turnstile_route(body: dict):
|
|
| 76 |
return JSONResponse({"code": 500, "message": str(e)}, status_code=500)
|
| 77 |
finally:
|
| 78 |
if browser:
|
| 79 |
-
try:
|
| 80 |
-
|
|
|
|
|
|
|
| 81 |
if pw:
|
| 82 |
-
try:
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# main.py
|
| 2 |
import os
|
| 3 |
+
import gradio as gr
|
| 4 |
from fastapi import FastAPI
|
| 5 |
from fastapi.responses import JSONResponse
|
| 6 |
from playwright.async_api import async_playwright
|
|
|
|
| 7 |
from turnstile import solve_turnstile
|
| 8 |
|
| 9 |
app = FastAPI()
|
|
|
|
| 15 |
_state = {"browser_limit": BROWSER_LIMIT}
|
| 16 |
|
| 17 |
|
| 18 |
+
@app.get("/api")
|
| 19 |
+
async def api_root():
|
| 20 |
return {
|
| 21 |
"message": "Server is running!",
|
| 22 |
"domain": DOMAIN,
|
| 23 |
+
"endpoint": f"{DOMAIN}/gradio/turnstile",
|
| 24 |
"status": {
|
| 25 |
"browserLimit": _state["browser_limit"],
|
| 26 |
"timeOut": TIMEOUT,
|
|
|
|
| 35 |
proxy = body.get("proxy")
|
| 36 |
|
| 37 |
if not domain or not site_key:
|
| 38 |
+
return JSONResponse({"message": "Missing domain or siteKey"}, status_code=400)
|
|
|
|
|
|
|
| 39 |
|
| 40 |
if _state["browser_limit"] <= 0:
|
| 41 |
return JSONResponse({"message": "Too Many Requests"}, status_code=429)
|
|
|
|
| 45 |
|
| 46 |
try:
|
| 47 |
pw = await async_playwright().start()
|
|
|
|
| 48 |
launch_args = {
|
| 49 |
"headless": True,
|
| 50 |
"args": ["--no-sandbox", "--disable-dev-shm-usage"],
|
|
|
|
| 53 |
launch_args["proxy"] = {
|
| 54 |
"server": f"http://{proxy['hostname']}:{proxy['port']}"
|
| 55 |
}
|
|
|
|
| 56 |
browser = await pw.chromium.launch(**launch_args)
|
| 57 |
context = await browser.new_context()
|
| 58 |
page = await context.new_page()
|
|
|
|
| 62 |
await route.abort()
|
| 63 |
else:
|
| 64 |
await route.continue_()
|
| 65 |
+
|
| 66 |
await page.route("**/*", block)
|
| 67 |
await page.goto("about:blank")
|
| 68 |
|
|
|
|
| 73 |
return JSONResponse({"code": 500, "message": str(e)}, status_code=500)
|
| 74 |
finally:
|
| 75 |
if browser:
|
| 76 |
+
try:
|
| 77 |
+
await browser.close()
|
| 78 |
+
except Exception:
|
| 79 |
+
pass
|
| 80 |
if pw:
|
| 81 |
+
try:
|
| 82 |
+
await pw.stop()
|
| 83 |
+
except Exception:
|
| 84 |
+
pass
|
| 85 |
+
_state["browser_limit"] += 1
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
with gr.Blocks() as demo:
|
| 89 |
+
gr.Markdown("Turnstile Service. API endpoint: /gradio/turnstile")
|
| 90 |
+
|
| 91 |
+
demo = gr.mount_gradio_app(app, demo, path="/gradio")
|
| 92 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|