diff --git a/py/api.py b/py/api.py index 228f567..d90429b 100644 --- a/py/api.py +++ b/py/api.py @@ -4,16 +4,18 @@ import secrets import threading from ai import AI from db import DB +from weather import Weather from voice import VoiceRecognition class API: def __init__(self): - self.crypt_size = 1 + self.crypt_size = 4096 self.app = Flask(__name__) self.ai_response = {} self.ai = AI() self.db = DB() + self.weather = Weather() self.voice = VoiceRecognition() self.db.load_database() self.ai_response_lock = threading.Lock() @@ -23,8 +25,12 @@ class API: @self.app.route('/interstellar_ai/api/ai_create', methods=['GET']) def create_ai(): access_token = secrets.token_urlsafe(self.crypt_size) - self.ai_response[access_token] = "" - return jsonify({'status': 200, 'access_token': access_token}) + + if access_token not in self.ai_response: + self.ai_response[access_token] = "" + return jsonify({'status': 200, 'access_token': access_token}) + + return jsonify({'status': 401, 'error': 'An error occurred, please try again.'}) @self.app.route('/interstellar_ai/api/ai_send', methods=['POST']) def send_ai(): @@ -99,6 +105,12 @@ class API: return jsonify({'status': 401, 'response': "Invalid type"}) + @self.app.route('/interstellar_ai/api/weather', methods=['POST']) + def get_weather(): + unit_type = request.args.get('unit_type') + city = request.args.get('city') + return jsonify({'status': 200, 'response': self.weather.getweather(unit_type, city)}) + self.app.run(debug=True, host='0.0.0.0', port=5000) diff --git a/py/requirements.txt b/py/requirements.txt index 6f01276..3277ca6 100644 --- a/py/requirements.txt +++ b/py/requirements.txt @@ -8,4 +8,5 @@ pyOpenSSL SpeechRecognition PocketSphinx google-cloud-speech -google-generativeai \ No newline at end of file +google-generativeai +python-weather \ No newline at end of file diff --git a/py/weather.py b/py/weather.py new file mode 100644 index 0000000..bb390c5 --- /dev/null +++ b/py/weather.py @@ -0,0 +1,39 @@ +import python_weather + + +class Weather: + @staticmethod + async def getweather(unit_type, city): + + if unit_type == "imperial": + unit_type = python_weather.IMPERIAL + elif unit_type == "metric": + unit_type = python_weather.METRIC + + async with python_weather.Client(unit=unit_type) as client: + weather = await client.get(city) + + data = { + 'temperature': weather.temperature, + 'humidity': weather.humidity, + 'unit': weather.unit, + 'datetime': weather.datetime, + 'coordinates': weather.coordinates, + 'country': weather.country, + 'daily_forecasts': weather.daily_forecasts, + 'description': weather.description, + 'feels_like': weather.feels_like, + 'kind': weather.kind, + 'local_population': weather.local_population, + 'locale': weather.locale, + 'location': weather.location, + 'precipitation': weather.precipitation, + 'pressure': weather.pressure, + 'region': weather.region, + 'ultraviolet': weather.ultraviolet, + 'visibility': weather.visibility, + 'wind_direction': weather.wind_direction, + 'wind_speed': weather.wind_speed, + } + + return data