Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -93,7 +93,7 @@ async def perform_web_browse(action: str, query: str, browser_name: str, search_
|
|
| 93 |
launch_args = ['--no-sandbox'] if browser_key == 'chromium' else []
|
| 94 |
browser = await browser_launcher.launch(headless=True, args=launch_args)
|
| 95 |
|
| 96 |
-
if action == "Scrape
|
| 97 |
url = query if query.startswith(('http://', 'https://')) else f"http://{query}"
|
| 98 |
else: # action == "Search"
|
| 99 |
url_template = SEARCH_ENGINES.get(search_engine)
|
|
@@ -143,7 +143,7 @@ async def perform_web_browse(action: str, query: str, browser_name: str, search_
|
|
| 143 |
|
| 144 |
@app.route('/', methods=['GET'])
|
| 145 |
def index():
|
| 146 |
-
return json.dumps({ "status": "online", "message": "Welcome to the Web Browse API!", "api_endpoint": "/web_browse", "instructions": "Send a POST request to /web_browse with a JSON payload to use the service.", "payload_format": { "action": "string (required: 'Search' or 'Scrape
|
| 147 |
|
| 148 |
@app.route('/web_browse', methods=['POST'])
|
| 149 |
def web_browse():
|
|
@@ -154,7 +154,7 @@ def web_browse():
|
|
| 154 |
browser = data.get('browser', 'firefox')
|
| 155 |
search_engine = data.get('search_engine', 'DuckDuckGo')
|
| 156 |
if not action or not query: return jsonify({"status": "error", "error_message": "Missing required parameters: 'action' and 'query' are mandatory."}), 400
|
| 157 |
-
if action not in ["Search", "Scrape
|
| 158 |
try:
|
| 159 |
result = asyncio.run(perform_web_browse(action, query, browser, search_engine))
|
| 160 |
response_status_code = 200 if result.get("status") == "success" else 500
|
|
|
|
| 93 |
launch_args = ['--no-sandbox'] if browser_key == 'chromium' else []
|
| 94 |
browser = await browser_launcher.launch(headless=True, args=launch_args)
|
| 95 |
|
| 96 |
+
if action == "Scrape":
|
| 97 |
url = query if query.startswith(('http://', 'https://')) else f"http://{query}"
|
| 98 |
else: # action == "Search"
|
| 99 |
url_template = SEARCH_ENGINES.get(search_engine)
|
|
|
|
| 143 |
|
| 144 |
@app.route('/', methods=['GET'])
|
| 145 |
def index():
|
| 146 |
+
return json.dumps({ "status": "online", "message": "Welcome to the Web Browse API!", "api_endpoint": "/web_browse", "instructions": "Send a POST request to /web_browse with a JSON payload to use the service.", "payload_format": { "action": "string (required: 'Search' or 'Scrape')", "query": "string (required: a search term or a full URL)", "browser": "string (optional, default: 'firefox'; options: 'firefox', 'chromium', 'webkit')", "search_engine": "string (optional, default: 'DuckDuckGo'; see code for all options)" }, "example_curl": """curl -X POST YOUR_SPACE_URL/web_browse -H "Content-Type: application/json" -d '{"action": "Search", "query": "latest news on AI", "browser": "webkit"}'""" }, indent=4)
|
| 147 |
|
| 148 |
@app.route('/web_browse', methods=['POST'])
|
| 149 |
def web_browse():
|
|
|
|
| 154 |
browser = data.get('browser', 'firefox')
|
| 155 |
search_engine = data.get('search_engine', 'DuckDuckGo')
|
| 156 |
if not action or not query: return jsonify({"status": "error", "error_message": "Missing required parameters: 'action' and 'query' are mandatory."}), 400
|
| 157 |
+
if action not in ["Search", "Scrape"]: return jsonify({"status": "error", "error_message": "Invalid 'action'. Must be 'Search' or 'Scrape'."}), 400
|
| 158 |
try:
|
| 159 |
result = asyncio.run(perform_web_browse(action, query, browser, search_engine))
|
| 160 |
response_status_code = 200 if result.get("status") == "success" else 500
|