Spaces:
Sleeping
Sleeping
created findWeather.py. It takes the city name as input parameter and uses openweathermap api to fetch the weather details
Browse files- findWeather.py +36 -0
findWeather.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
load_dotenv()
|
6 |
+
|
7 |
+
|
8 |
+
def find_weather(city):
|
9 |
+
base_url = 'https://api.openweathermap.org/data/2.5/weather?'
|
10 |
+
weather_api_key = os.getenv("OPEN_WEATHER_API_KEY")
|
11 |
+
url = base_url + '&appid=' + weather_api_key + '&q=' + city + '&units=metric'
|
12 |
+
response = requests.get(url).json()
|
13 |
+
weather_data_final = {
|
14 |
+
"city_longitude": response['coord']['lon'],
|
15 |
+
"city_latitude": response['coord']['lat'],
|
16 |
+
"weather_main": response['weather'][0]['main'],
|
17 |
+
"weather_description": response['weather'][0]['description'],
|
18 |
+
"temperature": response['main']['temp'],
|
19 |
+
"feels_like": response['main']['feels_like'],
|
20 |
+
"temperature_min": response['main']['temp_min'],
|
21 |
+
"temperature_max": response['main']['temp_max'],
|
22 |
+
"pressure": response['main']['pressure'],
|
23 |
+
"humidity": response['main']['humidity'],
|
24 |
+
"visibility": response['visibility'],
|
25 |
+
"wind_speed": response['wind']['speed'],
|
26 |
+
"clouds": response['clouds']['all'],
|
27 |
+
"city_name": response['name'],
|
28 |
+
"country_code": response['sys']['country']
|
29 |
+
}
|
30 |
+
output = print_weather_data(weather_data_final)
|
31 |
+
return weather_data_final, output
|
32 |
+
|
33 |
+
|
34 |
+
def print_weather_data(weather_data_final):
|
35 |
+
output = "City: " + weather_data_final['city_name'] + " " + weather_data_final['country_code'] + " " + "\nAverage temperature for today is: " + str(weather_data_final['temperature']) + "°C" + "\nDue to other conditions, temperature feels like " + str(weather_data_final['feels_like']) + "°C today" + "\nMinimum temperature for today is: " + str(weather_data_final['temperature_min']) + "°C" + "\nMaximum temperature for today is: " + str(weather_data_final['temperature_max']) + "°C" + "\nAverage humidity for today is: " + str(weather_data_final['humidity']) + "\n\n\n"
|
36 |
+
return output
|