Spaces:
BG5
/
Running

BG5 commited on
Commit
5c40a50
·
verified ·
1 Parent(s): 1e17234

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +10 -10
api.py CHANGED
@@ -192,10 +192,10 @@ async def system_usage():
192
  """
193
  try:
194
  usage_info = get_system_usage()
195
- return {"code": 0, "msg": "success", "data": usage_info}
196
  except Exception as e:
197
  logger.error(f"获取系统资源使用情况失败: {e}")
198
- return {"code": 1, "msg": "failed", "error": str(e)}
199
 
200
 
201
  # BitBrowser风格API
@@ -206,7 +206,7 @@ async def open_browser(request: Request):
206
  if browser_id in BROWSERS:
207
  logger.info(f"Browser ID {browser_id}: {BROWSERS[browser_id]}")
208
  if BROWSERS[browser_id]["status"] == "open":
209
- return {"code": 0, "msg": "already opened", "data": {"id": browser_id, "ws": BROWSERS['ws']}, "info": BROWSERS["info"]}
210
  else:
211
  port = get_free_port()
212
  BROWSERS[browser_id]["port"] = port
@@ -238,7 +238,7 @@ async def open_browser(request: Request):
238
  proc = subprocess.Popen(args, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
239
  if not wait_port("127.0.0.1", port, timeout=60):
240
  proc.terminate()
241
- return {"code": 2, "msg": f"chrome端口{port}未就绪"}
242
  async with httpx.AsyncClient() as client:
243
  resp = await client.get(f"http://127.0.0.1:{port}/json/version")
244
  if resp.status_code == 200:
@@ -246,7 +246,7 @@ async def open_browser(request: Request):
246
  ws_url = browser_info.get("webSocketDebuggerUrl").replace("ws://127.0.0.1", "wss://0.0.0.0")
247
  logger.info(f"Browser opened with ID: {browser_id}, Info: {browser_info}")
248
  BROWSERS[browser_id] = {"process": proc, "ws": ws_url, "port": port, "status": "open", "info": browser_info}
249
- return {"code": 0, "msg": "success", "data": {"id": browser_id, "ws": ws_url}, "info": browser_info}
250
 
251
 
252
  @app.post("/browser/close")
@@ -255,10 +255,10 @@ async def close_browser(request: Request):
255
  browser_id = data.get("id")
256
  b = BROWSERS.get(browser_id)
257
  if not b:
258
- return {"code": 1, "msg": "not found"}
259
  b["process"].terminate()
260
  b["status"] = "closed"
261
- return {"code": 0, "msg": "closed", "data": {"id": browser_id}}
262
 
263
 
264
  @app.post("/browser/delete")
@@ -267,7 +267,7 @@ async def delete_browser(request: Request):
267
  browser_id = data.get("id")
268
  b = BROWSERS.pop(browser_id, None)
269
  if not b:
270
- return {"code": 1, "msg": "not found"}
271
  try:
272
  b["process"].terminate()
273
  except Exception:
@@ -277,7 +277,7 @@ async def delete_browser(request: Request):
277
  if os.path.exists(profile_dir):
278
  import shutil
279
  shutil.rmtree(profile_dir, ignore_errors=True)
280
- return {"code": 0, "msg": "deleted", "data": {"id": browser_id}}
281
 
282
 
283
  @app.post("/browser/update")
@@ -304,7 +304,7 @@ async def browser_ports():
304
 
305
  @app.post("/health")
306
  async def health():
307
- return {"code": 0, "msg": "ok"}
308
 
309
 
310
  CDP_PATHS = [
 
192
  """
193
  try:
194
  usage_info = get_system_usage()
195
+ return {"success": True, "msg": "success", "data": usage_info}
196
  except Exception as e:
197
  logger.error(f"获取系统资源使用情况失败: {e}")
198
+ return {"success": False, "msg": "failed", "error": str(e)}
199
 
200
 
201
  # BitBrowser风格API
 
206
  if browser_id in BROWSERS:
207
  logger.info(f"Browser ID {browser_id}: {BROWSERS[browser_id]}")
208
  if BROWSERS[browser_id]["status"] == "open":
209
+ return {"success": True, "msg": "already opened", "data": {"id": browser_id, "ws": BROWSERS['ws']}, "info": BROWSERS["info"]}
210
  else:
211
  port = get_free_port()
212
  BROWSERS[browser_id]["port"] = port
 
238
  proc = subprocess.Popen(args, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
239
  if not wait_port("127.0.0.1", port, timeout=60):
240
  proc.terminate()
241
+ return {"success": False, "msg": f"chrome端口{port}未就绪", "data": {}}
242
  async with httpx.AsyncClient() as client:
243
  resp = await client.get(f"http://127.0.0.1:{port}/json/version")
244
  if resp.status_code == 200:
 
246
  ws_url = browser_info.get("webSocketDebuggerUrl").replace("ws://127.0.0.1", "wss://0.0.0.0")
247
  logger.info(f"Browser opened with ID: {browser_id}, Info: {browser_info}")
248
  BROWSERS[browser_id] = {"process": proc, "ws": ws_url, "port": port, "status": "open", "info": browser_info}
249
+ return {"success": True, "msg": "success", "data": {"id": browser_id, "ws": ws_url}, "info": browser_info}
250
 
251
 
252
  @app.post("/browser/close")
 
255
  browser_id = data.get("id")
256
  b = BROWSERS.get(browser_id)
257
  if not b:
258
+ return {"success": False, "msg": "not found"}
259
  b["process"].terminate()
260
  b["status"] = "closed"
261
+ return {"success": True, "msg": "closed", "data": {"id": browser_id}}
262
 
263
 
264
  @app.post("/browser/delete")
 
267
  browser_id = data.get("id")
268
  b = BROWSERS.pop(browser_id, None)
269
  if not b:
270
+ return {"success": False, "msg": "not found"}
271
  try:
272
  b["process"].terminate()
273
  except Exception:
 
277
  if os.path.exists(profile_dir):
278
  import shutil
279
  shutil.rmtree(profile_dir, ignore_errors=True)
280
+ return {"success": True, "msg": "deleted", "data": {"id": browser_id}}
281
 
282
 
283
  @app.post("/browser/update")
 
304
 
305
  @app.post("/health")
306
  async def health():
307
+ return {"success": True, "msg": "ok"}
308
 
309
 
310
  CDP_PATHS = [