Spaces:
Sleeping
Sleeping
import logging | |
import os | |
import time | |
from datetime import datetime, timezone | |
from flask import Flask, Response, abort, json, jsonify, request | |
from selenium import webdriver | |
from selenium.webdriver.chrome.options import Options | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.common.keys import Keys | |
import config | |
# selenium options | |
chrome_options = Options() | |
chrome_options.add_argument('--no-sandbox') | |
chrome_options.add_argument('--disable-dev-shm-usage') | |
chrome_options.add_argument('--disable-gpu') | |
chrome_options.add_argument('--headless') | |
tools = Flask(__name__) | |
formatter = logging.Formatter(f'%(asctime)s - TOOLS - %(levelname)s - %(message)s') | |
tools_logger = tools.logger | |
handler = tools_logger.handlers[0].setFormatter(formatter) | |
def get_datetime() -> Response: | |
curr_date = datetime.now(timezone.utc).date().strftime("%Y-%m-%d") | |
curr_time = datetime.now(timezone.utc).strftime("%H:%M:%S") | |
return jsonify({"date": curr_date, "time": curr_time}) | |
def calculate() -> Response: | |
try: | |
data = json.loads(request.data) | |
if data["operator"] == "add": | |
return jsonify(result=data["num1"] + data["num2"]) | |
elif data["operator"] == "subtract": | |
return jsonify(result=data["num1"] - data["num2"]) | |
elif data["operator"] == "multiply": | |
return jsonify(result=data["num1"] * data["num2"]) | |
elif data["operator"] == "divide": | |
return jsonify(result=data["num1"] / data["num2"]) | |
else: | |
abort(400, description="Invalid operator: " + data["operator"]) | |
except KeyError as e: | |
abort(400, description="Missing value in request body: " + str(e)) | |
except Exception as e: | |
abort(400, description="Error: " + str(e)) | |
def google_search() -> Response: | |
try: | |
data = json.loads(request.data) | |
import requests | |
# Call the Playwright service | |
resp = requests.post("http://localhost:5000/search", json={"query": data["query"]}, timeout=30) | |
resp.raise_for_status() | |
result = resp.json().get("result", "No result returned") | |
return jsonify(result=result) | |
except KeyError as e: | |
abort(400, description="Missing value in request body: " + str(e)) | |
except Exception as e: | |
abort(400, description="Error: " + str(e)) | |
def home() -> str: | |
return "hello" | |
tools.run(host=config.tools_host, port=config.tools_port) |