forked from React-Group/interstellar_ai
work
This commit is contained in:
parent
5570e9de7c
commit
281d786c19
2 changed files with 41 additions and 35 deletions
11
py/api.py
11
py/api.py
|
@ -157,11 +157,12 @@ class API:
|
||||||
|
|
||||||
@self.app.route("/interstellar_ai/api/weather", methods=["POST"])
|
@self.app.route("/interstellar_ai/api/weather", methods=["POST"])
|
||||||
def get_weather():
|
def get_weather():
|
||||||
unit_type = request.args.get("unit_type")
|
sent_data = request.get_json()
|
||||||
city = request.args.get("city")
|
unit_type = sent_data.get("unit_type")
|
||||||
return jsonify(
|
city = sent_data.get("city")
|
||||||
{"status": 200, "response": self.weather.getweather(unit_type, city)}
|
weather_data = self.weather.getweather(unit_type, city)
|
||||||
)
|
print(type(weather_data))
|
||||||
|
return jsonify({"status": 200, "response": weather_data})
|
||||||
|
|
||||||
self.app.run(debug=True, host="0.0.0.0", port=5000)
|
self.app.run(debug=True, host="0.0.0.0", port=5000)
|
||||||
|
|
||||||
|
|
|
@ -1,39 +1,44 @@
|
||||||
import python_weather
|
import python_weather
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
|
||||||
class Weather:
|
class Weather:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def getweather(unit_type, city):
|
def getweather(unit_type, city):
|
||||||
|
# Define an inner asynchronous function
|
||||||
|
async def fetch_weather(unit_type):
|
||||||
|
if unit_type == "imperial":
|
||||||
|
unit_type = python_weather.IMPERIAL
|
||||||
|
elif unit_type == "metric":
|
||||||
|
unit_type = python_weather.METRIC
|
||||||
|
|
||||||
if unit_type == "imperial":
|
async with python_weather.Client(unit=unit_type) as client:
|
||||||
unit_type = python_weather.IMPERIAL
|
weather = await client.get(city)
|
||||||
elif unit_type == "metric":
|
|
||||||
unit_type = python_weather.METRIC
|
|
||||||
|
|
||||||
async with python_weather.Client(unit=unit_type) as client:
|
data = {
|
||||||
weather = await client.get(city)
|
"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,
|
||||||
|
}
|
||||||
|
|
||||||
data = {
|
return 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
|
# Run the asynchronous function and return the result
|
||||||
|
return asyncio.run(fetch_weather(unit_type))
|
||||||
|
|
Loading…
Reference in a new issue