import requests def get_current_weather(api_key, lat, lng, units="IMPERIAL"): # The endpoint for current weather conditions url = "https://weather.googleapis.com/v1/currentConditions:lookup" # Define the query parameters params = { "key": api_key, "location.latitude": lat, "location.longitude": lng, "unitsSystem": units # Can be 'IMPERIAL' or 'METRIC' } try: # Make the HTTP GET request response = requests.get(url, params=params) # Raise an exception for 4XX or 5XX status codes response.raise_for_status() # Parse and return the JSON response return response.json() except requests.exceptions.RequestException as e: #print(f"An error occurred: {e}") return None def get_daily_forecast(api_key, lat, lng, days=2): # The endpoint for daily forecasts url = "https://weather.googleapis.com/v1/forecast/days:lookup" # Define the query parameters params = { "key": api_key, "location.latitude": lat, "location.longitude": lng, "days": days, # Optional: defaults to 10 if not specified "unitsSystem": "IMPERIAL" } try: # Make the HTTP GET request response = requests.get(url, params=params) # Raise an exception if the request was unsuccessful response.raise_for_status() # Parse the JSON response data = response.json() return data except requests.exceptions.RequestException as e: #print(f"An error occurred: {e}") return None def get_google_weather(): # Usage API_KEY = "AIzaSyB42Vu94AoM8C6OHrF_iya5-crL8vPVjzs" LATITUDE, LONGITUDE = 40.576511379423984, -80.07068759089107 theweather = {} current_data = get_current_weather(API_KEY, LATITUDE, LONGITUDE) if current_data: # Extracting specific fields from the response condition = current_data.get("weatherCondition", {}).get("description", {}).get("text") temp = current_data.get("temperature", {}) feels_like = current_data.get("feelsLikeTemperature", {}) humidity = current_data.get("relativeHumidity") theweather["current"] = (f"{condition}") + (f", {temp.get('degrees')}°F")\ + (f", {humidity}%") + (f" hum., Feels like: {int(feels_like.get('degrees'))}°F") else: # current_data not returned theweather["current"] = "Problem retrieving current weather" forecast_data = get_daily_forecast(API_KEY, LATITUDE, LONGITUDE) if forecast_data: day = forecast_data.get("forecastDays", [])[0] max_temp = day.get("maxTemperature", {}).get("degrees") min_temp = day.get("minTemperature", {}).get("degrees") condition = day.get("daytimeForecast", {}).get("weatherCondition", {}).get("description", {}).get("text") theweather["forecast_today"] = (f"{condition}") + (f", {int(min_temp)}°F-{int(max_temp)}°F") day = forecast_data.get("forecastDays", [])[1] max_temp = day.get("maxTemperature", {}).get("degrees") min_temp = day.get("minTemperature", {}).get("degrees") condition = day.get("daytimeForecast", {}).get("weatherCondition", {}).get("description", {}).get("text") theweather["forecast_tomorrow"] = (f"{condition}") + (f", {int(min_temp)}°F-{int(max_temp)}°F") else: theweather["forecast_tomorrow"] = "Problem retrieving weather forecast" return theweather if __name__ == '__main__': print("theweather",get_google_weather())