forked from EurostonemxMarmol/pagina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.py
42 lines (32 loc) · 1.39 KB
/
weather.py
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
import requests
from Jarvis.config import config
def fetch_weather(city):
"""
City to weather
:param city: City
:return: weather
"""
api_key = config.weather_api_key
units_format = "&units=metric"
base_url = "http://api.openweathermap.org/data/2.5/weather?q="
complete_url = base_url + city + "&appid=" + api_key + units_format
response = requests.get(complete_url)
city_weather_data = response.json()
if city_weather_data["cod"] != "404":
main_data = city_weather_data["main"]
weather_description_data = city_weather_data["weather"][0]
weather_description = weather_description_data["description"]
current_temperature = main_data["temp"]
current_pressure = main_data["pressure"]
current_humidity = main_data["humidity"]
wind_data = city_weather_data["wind"]
wind_speed = wind_data["speed"]
final_response = f"""
The weather in {city} is currently {weather_description}
with a temperature of {current_temperature} degree celcius,
atmospheric pressure of {current_pressure} hectoPascals,
humidity of {current_humidity} percent
and wind speed reaching {wind_speed} kilometers per hour"""
return final_response
else:
return "Sorry Sir, I couldn't find the city in my database. Please try again"