THEZYZSTUDIO commited on
Commit
2446f86
ยท
verified ยท
1 Parent(s): 3ebde61

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -5
app.py CHANGED
@@ -705,6 +705,38 @@ async def handle_action(ws: WebSocket, msg: dict, sess: dict):
705
  except Exception:
706
  pass
707
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
708
  async def shot_bg(label: str = "", delay: float = 0.5,
709
  force_mx: int | None = None, force_my: int | None = None):
710
  """auto_shot ููŠ ุงู„ุฎู„ููŠุฉ โ€” ู…ุน rate limit + delta suppression."""
@@ -713,9 +745,7 @@ async def handle_action(ws: WebSocket, msg: dict, sess: dict):
713
  if now - sess.get("last_bg_shot_ts", 0) < 0.3:
714
  await asyncio.sleep(0.3 - (now - sess["last_bg_shot_ts"]))
715
 
716
- result = await asyncio.to_thread(
717
- capture_with_grid, display, 0.65, 72, force_mx, force_my
718
- )
719
  sess["last_bg_shot_ts"] = time.time()
720
  if not result["data"]:
721
  return
@@ -739,7 +769,7 @@ async def handle_action(ws: WebSocket, msg: dict, sess: dict):
739
 
740
  async def shot_explicit(label: str = ""):
741
  """screenshot ุตุฑูŠุญ โ€” ูŠูุฑุณู„ ุฏุงุฆู…ุงู‹ ุจุฏูˆู† delta suppression."""
742
- result = await asyncio.to_thread(capture_with_grid, display, 0.65, 75)
743
  if not result["data"]:
744
  return
745
  sess["last_bg_hash"] = _frame_hash(result["data"])
@@ -767,8 +797,12 @@ async def handle_action(ws: WebSocket, msg: dict, sess: dict):
767
  return
768
 
769
  # โ”€โ”€ screenshot โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
 
 
 
 
770
  if action == "screenshot":
771
- await shot_explicit("explicit screenshot")
772
 
773
  # โ”€โ”€ terminal โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
774
  elif action == "terminal":
 
705
  except Exception:
706
  pass
707
 
708
+ # โ”€โ”€ ุฅุตู„ุงุญ ุฌุฐุฑูŠ: ู‚ูู„ ุฎุงุต ุจูƒู„ session ู„ู…ู†ุน ุชุฑุงูƒู…/ุชุถุงุฑุจ ุนู…ู„ูŠุงุช capture ุงู„ู…ุชุฒุงู…ู†ุฉ โ”€โ”€
709
+ # ุงู„ู…ุดูƒู„ุฉ ุงู„ุฃุตู„ูŠุฉ: action == "screenshot" ูƒุงู† ูŠูู†ููŽู‘ุฐ ุจู€ await ู…ุจุงุดุฑ ุถู…ู† ุญู„ู‚ุฉ
710
+ # while True ุงู„ุฑุฆูŠุณูŠุฉ ููŠ websocket_endpointุŒ ูุฅุฐุง ุชุฃุฎุฑุช capture_with_grid (ู‚ุฏ ุชุตู„
711
+ # ู„ุฏู‚ูŠู‚ุฉ ูˆู†ุตู ููŠ ุฃุณูˆุฃ ุณูŠู†ุงุฑูŠูˆ ูุดู„/ุชุฃุฎุฑ scrot+import+ffmpeg)ุŒ ุชุจู‚ู‰ ุงู„ุญู„ู‚ุฉ ูƒู„ู‡ุง
712
+ # ู…ุญุฌูˆุจุฉ ูˆู„ุง ุชุณุชู‚ุจู„ ุฃูŠ ุฑุณุงู„ุฉ ุนู…ูŠู„ ุฌุฏูŠุฏุฉ (ู†ู‚ุฑุฉุŒ ุทู„ุจ screenshot ุขุฎุฑุŒ ุฅู„ุฎ) ุญุชู‰ ุชู†ุชู‡ูŠ.
713
+ # ุงู„ุญู„: ูƒู„ ุทู„ุจ screenshot ูŠูุดุบูŽู‘ู„ ููˆุฑุงู‹ ุนุจุฑ create_task (ู„ุง ูŠุญุฌุจ ุงู„ุญู„ู‚ุฉ ุฃุจุฏุงู‹)ุŒ
714
+ # ูˆู†ุณุชุฎุฏู… ู‚ูู„ุงู‹ ู„ู…ู†ุน ุชุฑุงูƒู… ุฃูƒุซุฑ ู…ู† ุนู…ู„ูŠุฉ capture ูุนู„ูŠุฉ ูˆุงุญุฏุฉ ุจู†ูุณ ุงู„ู„ุญุธุฉ ุนู„ู‰ ู†ูุณ
715
+ # ุงู„ู€ display (ู„ุชูุงุฏูŠ ุชุถุงุฑุจ ู…ู„ูุงุช /tmp ุงู„ู…ุคู‚ุชุฉ ูˆุงุณุชู‡ู„ุงูƒ CPU ู…ุถุงุนู ุจู„ุง ูุงุฆุฏุฉ) โ€”
716
+ # ููŠูุตุจุญ ุจุฅู…ูƒุงู† ุงู„ุฐูƒุงุก ุงู„ุงุตุทู†ุงุนูŠ ุทู„ุจ ู„ู‚ุทุฉ ุดุงุดุฉ ููŠ ุฃูŠ ูˆู‚ุช ูˆู…ู† ุฃูŠ ู…ูƒุงู† ุฏูˆู† ุฃู† ูŠุนู„ู‚
717
+ # ุฎู„ู ุทุงุจูˆุฑ ุฃูŠ ุฅุฌุฑุงุก ุณุงุจู‚ ุจุทูŠุก.
718
+ if "_shot_lock" not in sess:
719
+ sess["_shot_lock"] = asyncio.Lock()
720
+ _shot_lock = sess["_shot_lock"]
721
+
722
+ async def _safe_capture(scale, quality, force_mx=None, force_my=None):
723
+ """
724
+ ูŠุญุตู„ ุนู„ู‰ ุงู„ู‚ูู„ ุซู… ูŠู„ุชู‚ุท ุงู„ุดุงุดุฉุŒ ู…ุน ุณู‚ู ุฒู…ู†ูŠ ุฅุฌู…ุงู„ูŠ ุตุงุฑู… (25 ุซุงู†ูŠุฉ).
725
+ ู‡ุฐุง ูŠุถู…ู† ุฃู†ู‡ ุญุชู‰ ู„ูˆ ุทุงู„ ุงู†ุชุธุงุฑ ุงู„ู‚ูู„ (ุทู„ุจุงุช ู…ุชุฑุงูƒู…ุฉ) ุฃูˆ ุนู„ู‚ุช capture_with_grid
726
+ ู„ุณุจุจ ุบูŠุฑ ู…ุชูˆู‚ุนุŒ ู„ุง ูŠุจู‚ู‰ ุฃูŠ ุทู„ุจ ู„ู‚ุทุฉ ุดุงุดุฉ ู…ุนู„ู‘ู‚ุงู‹ ู„ู„ุฃุจุฏ โ€” ูŠูุดู„ ุจู‡ุฏูˆุก ุจุฏู„ ุฐู„ูƒ
727
+ ูˆูŠูุนุงุฏ ุงู„ู…ุญุงูˆู„ุฉ ููŠ ุงู„ุทู„ุจ ุงู„ุชุงู„ูŠ (ูƒู„ ุงู„ุงุณุชุฏุนุงุกุงุช ุชุชุนุงู…ู„ ุจุงู„ูุนู„ ู…ุน data="" ูƒูุดู„ ุตุงู…ุช).
728
+ """
729
+ async def _do_capture():
730
+ async with _shot_lock:
731
+ return await asyncio.to_thread(
732
+ capture_with_grid, display, scale, quality, force_mx, force_my
733
+ )
734
+ try:
735
+ return await asyncio.wait_for(_do_capture(), timeout=25)
736
+ except asyncio.TimeoutError:
737
+ print(f"[shot:{display}] โš ๏ธ capture timed out after 25s โ€” skipping this frame")
738
+ return {"data": "", "width": 1920, "height": 1080, "mouse_x": 0, "mouse_y": 0}
739
+
740
  async def shot_bg(label: str = "", delay: float = 0.5,
741
  force_mx: int | None = None, force_my: int | None = None):
742
  """auto_shot ููŠ ุงู„ุฎู„ููŠุฉ โ€” ู…ุน rate limit + delta suppression."""
 
745
  if now - sess.get("last_bg_shot_ts", 0) < 0.3:
746
  await asyncio.sleep(0.3 - (now - sess["last_bg_shot_ts"]))
747
 
748
+ result = await _safe_capture(0.65, 72, force_mx, force_my)
 
 
749
  sess["last_bg_shot_ts"] = time.time()
750
  if not result["data"]:
751
  return
 
769
 
770
  async def shot_explicit(label: str = ""):
771
  """screenshot ุตุฑูŠุญ โ€” ูŠูุฑุณู„ ุฏุงุฆู…ุงู‹ ุจุฏูˆู† delta suppression."""
772
+ result = await _safe_capture(0.65, 75)
773
  if not result["data"]:
774
  return
775
  sess["last_bg_hash"] = _frame_hash(result["data"])
 
797
  return
798
 
799
  # โ”€โ”€ screenshot โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
800
+ # ุฅุตู„ุงุญ: create_task ุจุฏู„ await ู…ุจุงุดุฑ โ€” ู„ุง ูŠุญุฌุจ ุญู„ู‚ุฉ ุงุณุชู‚ุจุงู„ ุงู„ุฑุณุงุฆู„ ุฃุจุฏุงู‹ุŒ
801
+ # ุญุชู‰ ู„ูˆ ุชุฃุฎุฑุช capture_with_grid ู„ุฃูŠ ุณุจุจ (CPU ุถุนูŠูุŒ X11 ุจุทูŠุกุŒ ุฅู„ุฎ).
802
+ # ู‡ุฐุง ูŠุถู…ู† ุฃู† ุฃูŠ ุฅุฌุฑุงุก ู„ุงุญู‚ (ู†ู‚ุฑุฉุŒ ุทุจุงุนุฉุŒ ุทู„ุจ screenshot ุขุฎุฑ) ูŠุตู„ ูˆูŠูุนุงู„ูŽุฌ
803
+ # ููˆุฑุงู‹ ุฏูˆู† ุงู†ุชุธุงุฑ ุงูƒุชู…ุงู„ ู‡ุฐู‡ ุงู„ู„ู‚ุทุฉ ุฃูˆู„ุงู‹.
804
  if action == "screenshot":
805
+ asyncio.create_task(shot_explicit("explicit screenshot"))
806
 
807
  # โ”€โ”€ terminal โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
808
  elif action == "terminal":