File size: 1,706 Bytes
3aea0a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
import requests

API_KEY = "8acd7401e3d1478a87596f7e00e76226"
BASE_URL = "https://api.openweathermap.org/data/2.5/weather"

def get_current_weather(city_name):
    """

    Fetch current weather data for a given city using OpenWeatherMap API.

    Returns a dictionary with temperature (Celsius) and rainfall (mm, if available).

    """
    params = {
        'q': city_name,
        'appid': API_KEY,
        'units': 'metric'
    }
    response = requests.get(BASE_URL, params=params)
    data = response.json()
    if response.status_code != 200:
        raise Exception(f"Weather API error: {data.get('message', 'Unknown error')}")
    temp = data['main']['temp']
    # Rainfall may not always be present
    rain = data.get('rain', {}).get('1h', 0.0)
    return {
        'temperature': temp,
        'rainfall': rain
    } 
import requests

API_KEY = "8acd7401e3d1478a87596f7e00e76226"
BASE_URL = "https://api.openweathermap.org/data/2.5/weather"

def get_current_weather(city_name):
    """

    Fetch current weather data for a given city using OpenWeatherMap API.

    Returns a dictionary with temperature (Celsius) and rainfall (mm, if available).

    """
    params = {
        'q': city_name,
        'appid': API_KEY,
        'units': 'metric'
    }
    response = requests.get(BASE_URL, params=params)
    data = response.json()
    if response.status_code != 200:
        raise Exception(f"Weather API error: {data.get('message', 'Unknown error')}")
    temp = data['main']['temp']
    # Rainfall may not always be present
    rain = data.get('rain', {}).get('1h', 0.0)
    return {
        'temperature': temp,
        'rainfall': rain
    }