Skip to content

Commit

Permalink
Merge pull request #89 from Kritika75/weather_info
Browse files Browse the repository at this point in the history
Added the required features (New Weather file)
  • Loading branch information
suryanshsk authored Oct 9, 2024
2 parents 9531b45 + 277def6 commit 46e3690
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
100 changes: 100 additions & 0 deletions new_weather_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import requests
import json
import logging

API_KEY = 'YOUR_API_KEY' Replace with your OpenWeatherMap API key
BASE_URL = "http://api.openweathermap.org/data/2.5/weather?"
FORECAST_URL = "http://api.openweathermap.org/data/2.5/forecast?"
ONE_CALL_URL = "http://api.openweathermap.org/data/2.5/onecall?"
FAVOURITE_CITIES_FILE = "favourite_cities.json"

logging.basicConfig(filename="weather_info.log", level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s")

def log_api_call(city_name, response):
if response.status_code == 200:
logging.info(f"Successfully fetched weather for {city_name}")
else:
logging.error(f"Failed to fetch weather for {city_name}: {response.status_code} {response.text}")

def get_weather(city_name, unit="metric"):
try:
unit_param = {"metric": "°C", "imperial": "°F", "kelvin": "K"}
complete_url = BASE_URL + f"q={city_name}&units={unit}&appid=" + API_KEY
response = requests.get(complete_url)
log_api_call(city_name, response)

data = response.json()
if response.status_code != 200:
return f"Error: {data.get('message', 'Unable to retrieve weather data.')}"

if data["cod"] == "404":
return "City not found. Please check the city name."

main = data["main"]
weather_description = data["weather"][0]["description"]
temperature = main["temp"]
weather_info = f"Temperature: {temperature:.2f}{unit_param[unit]}\nDescription: {weather_description}"

return weather_info
except requests.exceptions.RequestException as e:
logging.error(f"Request failed: {str(e)}")
return f"Error: Unable to fetch data. {str(e)}"

def get_weather_forecast(city_name):

complete_url = FORECAST_URL + "q=" + city_name + "&appid=" + API_KEY + "&units=metric"
response = requests.get(complete_url)
data = response.json()

if data["cod"] != "404":
forecast_list = data["list"]
forecast_info = []
for forecast in forecast_list[::8]:
date = forecast["dt_txt"].split(" ")[0]
temperature = forecast["main"]["temp"]
description = forecast["weather"][0]["description"]
forecast_info.append(f"{date} - Temperature: {temperature:.2f}°C, Description: {description}")
return "\n".join(forecast_info)
else:
return "City not found"

def get_weather_alerts(lat, lon):
complete_url = ONE_CALL_URL + f"lat={lat}&lon={lon}&appid=" + API_KEY
response = requests.get(complete_url)
data = response.json()

if "alerts" in data:
alerts = data["alerts"]
alert_info = []
for alert in alerts:
event = alert["event"]
description = alert["description"]
start = alert["start"]
end = alert["end"]
alert_info.append(f"Alert: {event}\nDescription: {description}\nStart: {start}\nEnd: {end}")
return "\n".join(alert_info)
else:
return "No weather alerts"

def save_favourite_city(city_name):
try:
with open(FAVOURITE_CITIES_FILE, 'r+') as file:
cities = json.load(file)
if city_name not in cities:
cities.append(city_name)
file.seek(0)
json.dump(cities, file)
return "City added to favourites"
except FileNotFoundError:
with open(FAVOURITE_CITIES_FILE, 'w') as file:
json.dump([city_name], file)
return "Favourites file created and city added"

def get_favourite_cities():
try:
with open(FAVOURITE_CITIES_FILE, 'r') as file:
cities = json.load(file)
return cities
except FileNotFoundError:
return "No favourite cities saved"
32 changes: 32 additions & 0 deletions test_weather.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from new_weather_info import (
get_weather,
get_weather_forecast,
get_weather_alerts,
save_favourite_city,
get_favourite_cities
)

def test_all_features():
city_name = "Chennai"
unit = "metric"

print(f"Testing Current Weather of {city_name}")
print(get_weather(city_name, unit))


print("\nTesting 5-Day Forecast:")
print(get_weather_forecast(city_name))


print("\nTesting Weather Alerts:")
print(get_weather_alerts(26.4499, 80.3319))

print("\nTesting Saving Favorite City:")
print(save_favourite_city(city_name))


print("\nTesting Retrieving Favorite Cities:")
print(get_favourite_cities())

if __name__ == "__main__":
test_all_features()

0 comments on commit 46e3690

Please sign in to comment.