File size: 2,474 Bytes
f2a243f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
940e723
 
 
 
 
 
 
f2a243f
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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)

@tools.route("/datetime", methods=["GET"])
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})

@tools.route("/calculator", methods=["POST"])
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))

@tools.route("/websearch", methods=["POST"])
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))

@tools.route("/", methods=["GET"])
def home() -> str:
  return "hello"

tools.run(host=config.tools_host, port=config.tools_port)