import requests from datetime import datetime def get_weather_forecast(lat, lon, api_key, days=7): url = "http://api.weatherapi.com/v1/forecast.json" params = { "key": api_key, "q": f"{lat},{lon}", "days": days, "alerts": "no", } response = requests.get(url, params=params) if response.status_code != 200: print( f"Failed to retrieve the forecast data. Status code: {response.status_code}" ) return {} data = response.json() all_weather_data = {} for day in data["forecast"]["forecastday"]: time = int(datetime.strptime(day["date"], "%Y-%m-%d").timestamp()) hourly_data = day["hour"] # Calculate average cloud coverage total_cloud = sum(entry["cloud"] for entry in hourly_data) average_cloud = total_cloud / len(hourly_data) single_weather_data = { "summary": day["day"]["condition"]["text"], "precimax": day["day"].get("totalprecip_mm", "Not Available"), "precimaxtime": "Not Available", # WeatherAPI doesn't provide this "preciprob": day["day"].get("daily_chance_of_rain", "Not Available"), "temphigh": day["day"]["maxtemp_c"], "temphightime": "Not Available", # WeatherAPI doesn't provide this "templow": day["day"]["mintemp_c"], "templowtime": "Not Available", # WeatherAPI doesn't provide this "cloudcover": average_cloud, } all_weather_data[time] = single_weather_data return all_weather_data # Usage api_key = "dd6e11f5d20e4694bed72932241007" lat = 40.7128 # Example latitude (New York City) lon = -74.0060 # Example longitude (New York City) days = 7 data_new = get_weather_forecast(lat, lon, api_key)