Update app.py
Browse files
app.py
CHANGED
|
@@ -1,125 +1,100 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
import os
|
| 3 |
import time
|
| 4 |
import base64
|
| 5 |
-
|
| 6 |
-
from
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
self.browser = self.playwright.chromium.launch(
|
| 31 |
-
headless=True,
|
| 32 |
-
args=['--no-sandbox', '--disable-blink-features=AutomationControlled']
|
| 33 |
-
)
|
| 34 |
-
self.context = self.browser.new_context(
|
| 35 |
-
viewport={'width': 375, 'height': 812}, # Mobile View
|
| 36 |
-
user_agent='Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1',
|
| 37 |
-
is_mobile=True, has_touch=True
|
| 38 |
-
)
|
| 39 |
-
self.page = self.context.new_page()
|
| 40 |
-
self.page.goto("https://www.coinpayu.com/login", timeout=60000)
|
| 41 |
-
self.add_log("Browser Started. On Login Page.")
|
| 42 |
-
except Exception as e:
|
| 43 |
-
self.add_log(f"Start Error: {e}")
|
| 44 |
-
|
| 45 |
-
def get_screenshot(self):
|
| 46 |
-
if not self.page: return None
|
| 47 |
-
try:
|
| 48 |
-
return self.page.screenshot(type='jpeg', quality=50)
|
| 49 |
-
except: return None
|
| 50 |
-
|
| 51 |
-
def interact(self, action, text=None):
|
| 52 |
-
if not self.page: return
|
| 53 |
-
try:
|
| 54 |
-
if action == "type":
|
| 55 |
-
self.page.keyboard.type(text)
|
| 56 |
-
self.add_log(f"Typed: {text}")
|
| 57 |
-
elif action == "enter":
|
| 58 |
-
self.page.keyboard.press("Enter")
|
| 59 |
-
self.add_log("Pressed Enter")
|
| 60 |
-
elif action == "scroll":
|
| 61 |
-
self.page.mouse.wheel(0, 300)
|
| 62 |
-
self.add_log("Scrolled Down")
|
| 63 |
-
elif action == "refresh":
|
| 64 |
-
self.page.reload()
|
| 65 |
-
self.add_log("Page Refreshed")
|
| 66 |
-
except Exception as e:
|
| 67 |
-
self.add_log(f"Error: {e}")
|
| 68 |
-
|
| 69 |
-
# --- INITIALIZE ---
|
| 70 |
-
bot = BrowserBot()
|
| 71 |
-
|
| 72 |
-
# --- UI LAYOUT ---
|
| 73 |
-
st.set_page_config(layout="centered", page_title="CoinPayU Bot")
|
| 74 |
-
|
| 75 |
-
st.title("📱 CoinPayU Manager")
|
| 76 |
-
|
| 77 |
-
# 1. LIVE VIEW
|
| 78 |
-
st.subheader("Live Screen")
|
| 79 |
-
screenshot = bot.get_screenshot()
|
| 80 |
-
|
| 81 |
-
if screenshot:
|
| 82 |
-
st.image(screenshot, caption="Live Browser Feed", use_column_width=True)
|
| 83 |
-
else:
|
| 84 |
-
st.info("Browser is OFF. Click 'Start Browser' below.")
|
| 85 |
-
|
| 86 |
-
# 2. CONTROLS
|
| 87 |
-
col1, col2 = st.columns(2)
|
| 88 |
-
|
| 89 |
-
with col1:
|
| 90 |
-
if st.button("🚀 Start Browser", use_container_width=True):
|
| 91 |
-
bot.start()
|
| 92 |
-
st.rerun()
|
| 93 |
-
|
| 94 |
-
if st.button("🔄 Refresh Page", use_container_width=True):
|
| 95 |
-
bot.interact("refresh")
|
| 96 |
-
st.rerun()
|
| 97 |
-
|
| 98 |
-
with col2:
|
| 99 |
-
if st.button("⬇ Scroll Down", use_container_width=True):
|
| 100 |
-
bot.interact("scroll")
|
| 101 |
-
st.rerun()
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
import base64
|
| 4 |
+
import threading
|
| 5 |
+
from flask import Flask, request, jsonify, send_file
|
| 6 |
+
from flask_cors import CORS
|
| 7 |
+
import pyautogui
|
| 8 |
+
import mss
|
| 9 |
+
from PIL import Image
|
| 10 |
+
import io
|
| 11 |
+
|
| 12 |
+
app = Flask(__name__)
|
| 13 |
+
CORS(app)
|
| 14 |
+
|
| 15 |
+
# --- DISPLAY SETUP ---
|
| 16 |
+
os.environ["DISPLAY"] = ":99"
|
| 17 |
+
SCREEN_WIDTH = 1024
|
| 18 |
+
SCREEN_HEIGHT = 768
|
| 19 |
+
|
| 20 |
+
# Optimization: PyAutoGUI speed up
|
| 21 |
+
pyautogui.FAILSAFE = False
|
| 22 |
+
pyautogui.PAUSE = 0.05
|
| 23 |
+
|
| 24 |
+
# Screenshot Manager (MSS is faster than Scrot)
|
| 25 |
+
sct = mss.mss()
|
| 26 |
+
monitor = {"top": 0, "left": 0, "width": SCREEN_WIDTH, "height": SCREEN_HEIGHT}
|
| 27 |
+
|
| 28 |
+
# --- ROUTES ---
|
| 29 |
+
|
| 30 |
+
@app.route('/')
|
| 31 |
+
def home():
|
| 32 |
+
return "Cloud PC Running. Use HTML Client."
|
| 33 |
+
|
| 34 |
+
@app.route('/snapshot', methods=['GET'])
|
| 35 |
+
def get_snapshot():
|
| 36 |
+
try:
|
| 37 |
+
# Capture full desktop
|
| 38 |
+
sct_img = sct.grab(monitor)
|
| 39 |
+
img = Image.frombytes("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX")
|
| 40 |
|
| 41 |
+
# Resize for Mobile Speed (Width 400px is enough for control)
|
| 42 |
+
img.thumbnail((500, 500))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
# Compress to JPEG
|
| 45 |
+
buffer = io.BytesIO()
|
| 46 |
+
img.save(buffer, format="JPEG", quality=40) # Low quality for speed
|
| 47 |
+
b64 = base64.b64encode(buffer.getvalue()).decode('utf-8')
|
| 48 |
+
|
| 49 |
+
return jsonify({"screenshot": b64})
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return jsonify({"error": str(e)}), 500
|
| 52 |
+
|
| 53 |
+
@app.route('/interact', methods=['POST'])
|
| 54 |
+
def interact():
|
| 55 |
+
data = request.json
|
| 56 |
+
action = data.get('action')
|
| 57 |
|
| 58 |
+
try:
|
| 59 |
+
if action == 'tap':
|
| 60 |
+
# Map mobile coordinates to PC resolution
|
| 61 |
+
x = int(data.get('x') * SCREEN_WIDTH)
|
| 62 |
+
y = int(data.get('y') * SCREEN_HEIGHT)
|
| 63 |
+
pyautogui.click(x, y)
|
| 64 |
+
|
| 65 |
+
elif action == 'type':
|
| 66 |
+
text = data.get('text')
|
| 67 |
+
pyautogui.write(text)
|
| 68 |
+
|
| 69 |
+
elif action == 'key':
|
| 70 |
+
key = data.get('key')
|
| 71 |
+
pyautogui.press(key)
|
| 72 |
+
|
| 73 |
+
elif action == 'scroll':
|
| 74 |
+
pyautogui.scroll(-5) # Linux scroll down
|
| 75 |
+
|
| 76 |
+
elif action == 'open_terminal':
|
| 77 |
+
# Shortcut to open terminal in XFCE
|
| 78 |
+
pyautogui.hotkey('ctrl', 'alt', 't')
|
| 79 |
+
|
| 80 |
+
elif action == 'open_browser':
|
| 81 |
+
# Command to run chromium
|
| 82 |
+
os.system("chromium-browser --no-sandbox &")
|
| 83 |
+
|
| 84 |
+
return jsonify({"status": "ok"})
|
| 85 |
+
except Exception as e:
|
| 86 |
+
return jsonify({"error": str(e)}), 500
|
| 87 |
+
|
| 88 |
+
# --- FILE TRANSFER ---
|
| 89 |
+
# Agar aap Antigravity se App banate hain, to wo '/app/output.apk' mein save karein
|
| 90 |
+
@app.route('/download', methods=['GET'])
|
| 91 |
+
def download_app():
|
| 92 |
+
# Example path (Change logic later based on where Antigravity saves files)
|
| 93 |
+
file_path = "/app/output.apk"
|
| 94 |
+
if os.path.exists(file_path):
|
| 95 |
+
return send_file(file_path, as_attachment=True)
|
| 96 |
+
else:
|
| 97 |
+
return "File not found yet. Build the app first!", 404
|
| 98 |
+
|
| 99 |
+
if __name__ == '__main__':
|
| 100 |
+
app.run(host='0.0.0.0', port=7860, threaded=True)
|