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
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,9 +25,13 @@ class API:
@self.app.route('/interstellar_ai/api/ai_create', methods=['GET'])
def create_ai():
access_token = secrets.token_urlsafe(self.crypt_size)
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():
data = request.get_json()
@ -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)

View file

@ -9,3 +9,4 @@ SpeechRecognition
PocketSphinx
google-cloud-speech
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