Spaces:
Paused
Paused
Update main.py
Browse files
main.py
CHANGED
|
@@ -10,6 +10,7 @@ from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
|
| 10 |
# 全局变量
|
| 11 |
browser = None
|
| 12 |
page = None
|
|
|
|
| 13 |
config = None
|
| 14 |
scheduler = AsyncIOScheduler()
|
| 15 |
|
|
@@ -19,12 +20,54 @@ def load_config():
|
|
| 19 |
return json.load(f)
|
| 20 |
|
| 21 |
def load_cookies():
|
| 22 |
-
"""
|
| 23 |
try:
|
| 24 |
with open("cookies.json", "r", encoding="utf-8") as f:
|
| 25 |
-
|
| 26 |
except FileNotFoundError:
|
| 27 |
return []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
async def activate_task():
|
| 30 |
"""定时激活任务"""
|
|
@@ -38,7 +81,7 @@ async def activate_task():
|
|
| 38 |
|
| 39 |
async def init_browser():
|
| 40 |
"""初始化浏览器"""
|
| 41 |
-
global browser, page, config
|
| 42 |
|
| 43 |
config = load_config()
|
| 44 |
cookies = load_cookies()
|
|
@@ -46,20 +89,47 @@ async def init_browser():
|
|
| 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 |
-
#
|
| 53 |
if cookies:
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
page = await context.new_page()
|
| 58 |
|
| 59 |
# 打开目标页面
|
| 60 |
target_link = config.get("targetLink", "https://www.example.com")
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
async def close_browser():
|
| 65 |
"""关闭浏览器"""
|
|
@@ -121,8 +191,37 @@ async def refresh_page():
|
|
| 121 |
if not page:
|
| 122 |
return {"status": "error", "message": "Browser not initialized"}
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
@app.get("/health")
|
| 128 |
async def health_check():
|
|
|
|
| 10 |
# 全局变量
|
| 11 |
browser = None
|
| 12 |
page = None
|
| 13 |
+
context = None
|
| 14 |
config = None
|
| 15 |
scheduler = AsyncIOScheduler()
|
| 16 |
|
|
|
|
| 20 |
return json.load(f)
|
| 21 |
|
| 22 |
def load_cookies():
|
| 23 |
+
"""加载并转换cookies格式"""
|
| 24 |
try:
|
| 25 |
with open("cookies.json", "r", encoding="utf-8") as f:
|
| 26 |
+
raw_cookies = json.load(f)
|
| 27 |
except FileNotFoundError:
|
| 28 |
return []
|
| 29 |
+
|
| 30 |
+
if not raw_cookies:
|
| 31 |
+
return []
|
| 32 |
+
|
| 33 |
+
converted_cookies = []
|
| 34 |
+
for cookie in raw_cookies:
|
| 35 |
+
# 构建Playwright兼容的cookie格式
|
| 36 |
+
new_cookie = {
|
| 37 |
+
"name": cookie.get("name"),
|
| 38 |
+
"value": cookie.get("value"),
|
| 39 |
+
"domain": cookie.get("domain"),
|
| 40 |
+
"path": cookie.get("path", "/"),
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
# 处理过期时间:expirationDate -> expires (秒转为秒,Playwright接受Unix时间戳)
|
| 44 |
+
if "expirationDate" in cookie:
|
| 45 |
+
new_cookie["expires"] = cookie["expirationDate"]
|
| 46 |
+
elif "expires" in cookie:
|
| 47 |
+
new_cookie["expires"] = cookie["expires"]
|
| 48 |
+
|
| 49 |
+
# 处理sameSite:转换为Playwright格式
|
| 50 |
+
same_site = cookie.get("sameSite", "").lower()
|
| 51 |
+
if same_site in ["strict", "lax", "none"]:
|
| 52 |
+
new_cookie["sameSite"] = same_site.capitalize()
|
| 53 |
+
if same_site == "none":
|
| 54 |
+
new_cookie["sameSite"] = "None"
|
| 55 |
+
elif same_site == "unspecified" or same_site == "no_restriction":
|
| 56 |
+
new_cookie["sameSite"] = "None"
|
| 57 |
+
else:
|
| 58 |
+
new_cookie["sameSite"] = "Lax" # 默认值
|
| 59 |
+
|
| 60 |
+
# 处理secure和httpOnly
|
| 61 |
+
new_cookie["secure"] = cookie.get("secure", False)
|
| 62 |
+
new_cookie["httpOnly"] = cookie.get("httpOnly", False)
|
| 63 |
+
|
| 64 |
+
# sameSite=None时必须secure=True
|
| 65 |
+
if new_cookie["sameSite"] == "None":
|
| 66 |
+
new_cookie["secure"] = True
|
| 67 |
+
|
| 68 |
+
converted_cookies.append(new_cookie)
|
| 69 |
+
|
| 70 |
+
return converted_cookies
|
| 71 |
|
| 72 |
async def activate_task():
|
| 73 |
"""定时激活任务"""
|
|
|
|
| 81 |
|
| 82 |
async def init_browser():
|
| 83 |
"""初始化浏览器"""
|
| 84 |
+
global browser, page, context, config
|
| 85 |
|
| 86 |
config = load_config()
|
| 87 |
cookies = load_cookies()
|
|
|
|
| 89 |
playwright = await async_playwright().start()
|
| 90 |
browser = await playwright.chromium.launch(headless=True)
|
| 91 |
context = await browser.new_context(
|
| 92 |
+
viewport={"width": 1920, "height": 1080},
|
| 93 |
+
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
|
| 94 |
)
|
| 95 |
|
| 96 |
+
# 按域名分组导入cookies
|
| 97 |
if cookies:
|
| 98 |
+
# 过滤有效的cookies
|
| 99 |
+
valid_cookies = []
|
| 100 |
+
for cookie in cookies:
|
| 101 |
+
try:
|
| 102 |
+
# 确保必要字段存在
|
| 103 |
+
if cookie.get("name") and cookie.get("value") and cookie.get("domain"):
|
| 104 |
+
valid_cookies.append(cookie)
|
| 105 |
+
except Exception as e:
|
| 106 |
+
print(f"[Cookie] Skip invalid cookie: {e}")
|
| 107 |
+
|
| 108 |
+
if valid_cookies:
|
| 109 |
+
try:
|
| 110 |
+
await context.add_cookies(valid_cookies)
|
| 111 |
+
print(f"[Browser] Loaded {len(valid_cookies)} cookies")
|
| 112 |
+
except Exception as e:
|
| 113 |
+
print(f"[Browser] Error loading cookies: {e}")
|
| 114 |
+
# 尝试逐个添加
|
| 115 |
+
success_count = 0
|
| 116 |
+
for cookie in valid_cookies:
|
| 117 |
+
try:
|
| 118 |
+
await context.add_cookies([cookie])
|
| 119 |
+
success_count += 1
|
| 120 |
+
except Exception as ce:
|
| 121 |
+
print(f"[Cookie] Failed to add cookie '{cookie.get('name')}' for {cookie.get('domain')}: {ce}")
|
| 122 |
+
print(f"[Browser] Loaded {success_count}/{len(valid_cookies)} cookies individually")
|
| 123 |
|
| 124 |
page = await context.new_page()
|
| 125 |
|
| 126 |
# 打开目标页面
|
| 127 |
target_link = config.get("targetLink", "https://www.example.com")
|
| 128 |
+
try:
|
| 129 |
+
await page.goto(target_link, wait_until="networkidle", timeout=60000)
|
| 130 |
+
print(f"[Browser] Opened: {target_link}")
|
| 131 |
+
except Exception as e:
|
| 132 |
+
print(f"[Browser] Warning: Page load issue - {e}")
|
| 133 |
|
| 134 |
async def close_browser():
|
| 135 |
"""关闭浏览器"""
|
|
|
|
| 191 |
if not page:
|
| 192 |
return {"status": "error", "message": "Browser not initialized"}
|
| 193 |
|
| 194 |
+
try:
|
| 195 |
+
await page.reload(wait_until="networkidle", timeout=60000)
|
| 196 |
+
return {"status": "success", "message": "Page refreshed"}
|
| 197 |
+
except Exception as e:
|
| 198 |
+
return {"status": "warning", "message": f"Refresh completed with issue: {e}"}
|
| 199 |
+
|
| 200 |
+
@app.get("/navigate")
|
| 201 |
+
async def navigate_to(url: str):
|
| 202 |
+
"""导航到指定URL"""
|
| 203 |
+
global page
|
| 204 |
+
if not page:
|
| 205 |
+
return {"status": "error", "message": "Browser not initialized"}
|
| 206 |
+
|
| 207 |
+
try:
|
| 208 |
+
await page.goto(url, wait_until="networkidle", timeout=60000)
|
| 209 |
+
return {"status": "success", "message": f"Navigated to {url}"}
|
| 210 |
+
except Exception as e:
|
| 211 |
+
return {"status": "warning", "message": f"Navigation completed with issue: {e}"}
|
| 212 |
+
|
| 213 |
+
@app.get("/cookies")
|
| 214 |
+
async def get_cookies():
|
| 215 |
+
"""获取当前cookies"""
|
| 216 |
+
global context
|
| 217 |
+
if not context:
|
| 218 |
+
return {"status": "error", "message": "Browser not initialized"}
|
| 219 |
+
|
| 220 |
+
try:
|
| 221 |
+
cookies = await context.cookies()
|
| 222 |
+
return {"status": "success", "count": len(cookies), "cookies": cookies}
|
| 223 |
+
except Exception as e:
|
| 224 |
+
return {"status": "error", "message": str(e)}
|
| 225 |
|
| 226 |
@app.get("/health")
|
| 227 |
async def health_check():
|