File size: 954 Bytes
7465c1e
 
8d45176
7465c1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d45176
7465c1e
8d45176
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
import requests

API_KEY = "YOUR_API_KEY"  # ๐Ÿ” Replace this with your OpenWeatherMap key

def get_emoji(condition):
    condition = condition.lower()
    if "rain" in condition:
        return "๐ŸŒง๏ธ"
    elif "cloud" in condition:
        return "โ˜๏ธ"
    elif "clear" in condition or "sun" in condition:
        return "โ˜€๏ธ"
    else:
        return "๐ŸŒˆ"

def get_weather_by_coordinates(lat, lon):
    url = f"https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={API_KEY}&units=metric"
    response = requests.get(url)
    if response.status_code != 200:
        return "โŒ Could not fetch weather data"
    
    data = response.json()
    temp = data["main"]["temp"]
    condition = data["weather"][0]["description"]
    emoji = get_emoji(condition)
    location = data.get("name", "Selected Location")

    return f"๐Ÿ“ **{location}**\n๐ŸŒก๏ธ **Temperature:** {temp}ยฐC\n๐Ÿ“Œ **Condition:** {condition} {emoji}"