Spaces:
Paused
Paused
Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import asyncio
|
| 3 |
+
from contextlib import asynccontextmanager
|
| 4 |
+
from fastapi import FastAPI
|
| 5 |
+
from fastapi.responses import Response
|
| 6 |
+
from playwright.async_api import async_playwright
|
| 7 |
+
import httpx
|
| 8 |
+
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
| 9 |
+
|
| 10 |
+
# 全局变量
|
| 11 |
+
browser = None
|
| 12 |
+
page = None
|
| 13 |
+
config = None
|
| 14 |
+
scheduler = AsyncIOScheduler()
|
| 15 |
+
|
| 16 |
+
def load_config():
|
| 17 |
+
"""加载配置文件"""
|
| 18 |
+
with open("config.json", "r", encoding="utf-8") as f:
|
| 19 |
+
return json.load(f)
|
| 20 |
+
|
| 21 |
+
def load_cookies():
|
| 22 |
+
"""加载cookies"""
|
| 23 |
+
try:
|
| 24 |
+
with open("cookies.json", "r", encoding="utf-8") as f:
|
| 25 |
+
return json.load(f)
|
| 26 |
+
except FileNotFoundError:
|
| 27 |
+
return []
|
| 28 |
+
|
| 29 |
+
async def activate_task():
|
| 30 |
+
"""定时激活任务"""
|
| 31 |
+
if config and config.get("activateLink"):
|
| 32 |
+
try:
|
| 33 |
+
async with httpx.AsyncClient() as client:
|
| 34 |
+
response = await client.get(config["activateLink"], timeout=30)
|
| 35 |
+
print(f"[Activate] GET {config['activateLink']} - Status: {response.status_code}")
|
| 36 |
+
except Exception as e:
|
| 37 |
+
print(f"[Activate] Error: {e}")
|
| 38 |
+
|
| 39 |
+
async def init_browser():
|
| 40 |
+
"""初始化浏览器"""
|
| 41 |
+
global browser, page, config
|
| 42 |
+
|
| 43 |
+
config = load_config()
|
| 44 |
+
cookies = load_cookies()
|
| 45 |
+
|
| 46 |
+
playwright = await async_playwright().start()
|
| 47 |
+
browser = await playwright.chromium.launch(headless=True)
|
| 48 |
+
context = await browser.new_context(
|
| 49 |
+
viewport={"width": 1920, "height": 1080}
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
# 导入cookies
|
| 53 |
+
if cookies:
|
| 54 |
+
await context.add_cookies(cookies)
|
| 55 |
+
print(f"[Browser] Loaded {len(cookies)} cookies")
|
| 56 |
+
|
| 57 |
+
page = await context.new_page()
|
| 58 |
+
|
| 59 |
+
# 打开目标页面
|
| 60 |
+
target_link = config.get("targetLink", "https://www.example.com")
|
| 61 |
+
await page.goto(target_link, wait_until="networkidle")
|
| 62 |
+
print(f"[Browser] Opened: {target_link}")
|
| 63 |
+
|
| 64 |
+
async def close_browser():
|
| 65 |
+
"""关闭浏览器"""
|
| 66 |
+
global browser
|
| 67 |
+
if browser:
|
| 68 |
+
await browser.close()
|
| 69 |
+
|
| 70 |
+
@asynccontextmanager
|
| 71 |
+
async def lifespan(app: FastAPI):
|
| 72 |
+
"""应用生命周期管理"""
|
| 73 |
+
# 启动时
|
| 74 |
+
await init_browser()
|
| 75 |
+
|
| 76 |
+
# 启动定时任务
|
| 77 |
+
if config and config.get("activateLink"):
|
| 78 |
+
interval = config.get("activateInterval", 60)
|
| 79 |
+
scheduler.add_job(activate_task, "interval", seconds=interval)
|
| 80 |
+
scheduler.start()
|
| 81 |
+
print(f"[Scheduler] Started with interval: {interval}s")
|
| 82 |
+
|
| 83 |
+
yield
|
| 84 |
+
|
| 85 |
+
# 关闭时
|
| 86 |
+
scheduler.shutdown(wait=False)
|
| 87 |
+
await close_browser()
|
| 88 |
+
|
| 89 |
+
app = FastAPI(lifespan=lifespan)
|
| 90 |
+
|
| 91 |
+
@app.get("/screenshot")
|
| 92 |
+
async def get_screenshot():
|
| 93 |
+
"""获取页面截图"""
|
| 94 |
+
global page
|
| 95 |
+
if not page:
|
| 96 |
+
return Response(content="Browser not initialized", status_code=500)
|
| 97 |
+
|
| 98 |
+
try:
|
| 99 |
+
screenshot = await page.screenshot(type="png", full_page=False)
|
| 100 |
+
return Response(content=screenshot, media_type="image/png")
|
| 101 |
+
except Exception as e:
|
| 102 |
+
return Response(content=str(e), status_code=500)
|
| 103 |
+
|
| 104 |
+
@app.get("/screenshot/full")
|
| 105 |
+
async def get_full_screenshot():
|
| 106 |
+
"""获取完整页面截图"""
|
| 107 |
+
global page
|
| 108 |
+
if not page:
|
| 109 |
+
return Response(content="Browser not initialized", status_code=500)
|
| 110 |
+
|
| 111 |
+
try:
|
| 112 |
+
screenshot = await page.screenshot(type="png", full_page=True)
|
| 113 |
+
return Response(content=screenshot, media_type="image/png")
|
| 114 |
+
except Exception as e:
|
| 115 |
+
return Response(content=str(e), status_code=500)
|
| 116 |
+
|
| 117 |
+
@app.get("/refresh")
|
| 118 |
+
async def refresh_page():
|
| 119 |
+
"""刷新页面"""
|
| 120 |
+
global page
|
| 121 |
+
if not page:
|
| 122 |
+
return {"status": "error", "message": "Browser not initialized"}
|
| 123 |
+
|
| 124 |
+
await page.reload(wait_until="networkidle")
|
| 125 |
+
return {"status": "success", "message": "Page refreshed"}
|
| 126 |
+
|
| 127 |
+
@app.get("/health")
|
| 128 |
+
async def health_check():
|
| 129 |
+
"""健康检查"""
|
| 130 |
+
return {"status": "ok"}
|
| 131 |
+
|
| 132 |
+
if __name__ == "__main__":
|
| 133 |
+
import uvicorn
|
| 134 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|