Weather API

This commit is contained in:
Patrick_Pluto 2024-09-24 14:18:57 +02:00
parent 3970c6a4f3
commit 6d4ac29d83
3 changed files with 56 additions and 4 deletions

View file

@ -4,16 +4,18 @@ import secrets
import threading import threading
from ai import AI from ai import AI
from db import DB from db import DB
from weather import Weather
from voice import VoiceRecognition from voice import VoiceRecognition
class API: class API:
def __init__(self): def __init__(self):
self.crypt_size = 1 self.crypt_size = 4096
self.app = Flask(__name__) self.app = Flask(__name__)
self.ai_response = {} self.ai_response = {}
self.ai = AI() self.ai = AI()
self.db = DB() self.db = DB()
self.weather = Weather()
self.voice = VoiceRecognition() self.voice = VoiceRecognition()
self.db.load_database() self.db.load_database()
self.ai_response_lock = threading.Lock() self.ai_response_lock = threading.Lock()
@ -23,8 +25,12 @@ class API:
@self.app.route('/interstellar_ai/api/ai_create', methods=['GET']) @self.app.route('/interstellar_ai/api/ai_create', methods=['GET'])
def create_ai(): def create_ai():
access_token = secrets.token_urlsafe(self.crypt_size) 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']) @self.app.route('/interstellar_ai/api/ai_send', methods=['POST'])
def send_ai(): def send_ai():
@ -99,6 +105,12 @@ class API:
return jsonify({'status': 401, 'response': "Invalid type"}) 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) self.app.run(debug=True, host='0.0.0.0', port=5000)

View file

@ -9,3 +9,4 @@ SpeechRecognition
PocketSphinx PocketSphinx
google-cloud-speech google-cloud-speech
google-generativeai google-generativeai
python-weather

39
py/weather.py Normal file
View file

@ -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