apibrowser / app.py
Akwbw's picture
Create app.py
5c7290f verified
from flask import Flask, request, jsonify
from playwright.sync_api import sync_playwright
import base64
app = Flask(__name__)
# Global variables to keep the browser session alive
p = None
browser = None
page = None
def initialize_browser():
"""Browser start karta hai agar pehle se nahi chal raha."""
global p, browser, page
if not page:
p = sync_playwright().start()
# Headless mode = True (Browser background me chalega)
browser = p.chromium.launch(headless=True)
# Context create karte hain taake history maintain rahe
context = browser.new_context(viewport={'width': 1280, 'height': 720})
page = context.new_page()
print("Browser Initialized")
def get_page_data():
"""Current page ka Screenshot aur HTML DOM return karta hai."""
global page
# 1. HTML Content
html_content = page.content()
# 2. Screenshot (Base64 string format mein)
screenshot_bytes = page.screenshot(full_page=False)
screenshot_b64 = base64.b64encode(screenshot_bytes).decode('utf-8')
return {
"html": html_content,
"screenshot": screenshot_b64
}
@app.route('/')
def home():
return "Browser Server is Running. Use POST /command to interact."
@app.route('/command', methods=['POST'])
def handle_command():
global page
# Ensure browser is running
initialize_browser()
data = request.json
action = data.get('action')
try:
# --- 1. NAVIGATE (Website kholna) ---
if action == 'navigate':
url = data.get('url')
if not url:
return jsonify({"error": "URL is required"}), 400
page.goto(url, timeout=60000)
page.wait_for_load_state('networkidle') # Wait until page settles
# --- 2. CLICK / TAP ---
elif action == 'click':
selector = data.get('selector')
if selector:
page.click(selector)
else:
# Agar selector nahi to coordinates par click (optional)
x = data.get('x')
y = data.get('y')
if x is not None and y is not None:
page.mouse.click(x, y)
# --- 3. TYPE (Likhna) ---
elif action == 'type':
selector = data.get('selector')
text = data.get('text')
if selector and text:
page.fill(selector, text)
# --- 4. ENTER KEY (Form submit waghera ke liye) ---
elif action == 'enter':
page.keyboard.press('Enter')
# --- 5. SCROLL ---
elif action == 'scroll':
direction = data.get('direction', 'down')
amount = data.get('amount', 500)
if direction == 'down':
page.mouse.wheel(0, amount)
elif direction == 'up':
page.mouse.wheel(0, -amount)
# Thoda wait karein taake scroll render ho jaye
page.wait_for_timeout(500)
# --- 6. HOVER ---
elif action == 'hover':
selector = data.get('selector')
if selector:
page.hover(selector)
# --- 7. WAIT (Agar manual delay chahiye) ---
elif action == 'wait':
duration = data.get('duration', 1000)
page.wait_for_timeout(duration)
else:
return jsonify({"error": "Unknown action"}), 400
# --- RETURN DATA (Screenshot + DOM) ---
response_data = get_page_data()
return jsonify({
"status": "success",
"action_performed": action,
"current_url": page.url,
"dom": response_data['html'],
"screenshot_base64": response_data['screenshot']
})
except Exception as e:
return jsonify({"status": "error", "message": str(e)}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860)