File size: 1,708 Bytes
de14271
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb413f5
de14271
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
import requests
import json
import os
from dotenv import load_dotenv
load_dotenv()


def find_weather(city):
    base_url = 'https://api.openweathermap.org/data/2.5/weather?'
    weather_api_key = os.getenv("OPEN_WEATHER_API_KEY")
    url = base_url + '&appid=' + weather_api_key + '&q=' + city + '&units=metric'
    response = requests.get(url).json()
    weather_data_final = {
        "city_longitude": response['coord']['lon'],
        "city_latitude": response['coord']['lat'],
        "weather_main": response['weather'][0]['main'],
        "weather_description": response['weather'][0]['description'],
        "temperature": response['main']['temp'],
        "feels_like": response['main']['feels_like'],
        "temperature_min": response['main']['temp_min'],
        "temperature_max": response['main']['temp_max'],
        "pressure": response['main']['pressure'],
        "humidity": response['main']['humidity'],
        "visibility": response['visibility'],
        "wind_speed": response['wind']['speed'],
        "clouds": response['clouds']['all'],
        "city_name": response['name'],
        "country_code": response['sys']['country']
    }
    output = print_weather_data(weather_data_final)
    return weather_data_final, output


def print_weather_data(weather_data_final):
    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"  + "\nAverage humidity for today is: " + str(weather_data_final['humidity']) + "\n\n\n"
    return output