From e55477afcdfcc673e818eef2a269024e9b12ea5b Mon Sep 17 00:00:00 2001 From: Omar G Ghafour Date: Tue, 27 May 2025 17:23:41 -0400 Subject: [PATCH 1/4] Create weather-app.py --- weather-app.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 weather-app.py diff --git a/weather-app.py b/weather-app.py new file mode 100644 index 0000000..ed32d15 --- /dev/null +++ b/weather-app.py @@ -0,0 +1,30 @@ +import requests + +def get_temperature(city_name, api_key): + base_url = "https://api.openweathermap.org/data/2.5/weather" + params = { + 'q': f"{city_name},US", + 'appid': api_key, + 'units': 'imperial' # Fahrenheit + } + response = requests.get(base_url, params=params) + + if response.status_code == 200: + data = response.json() + temp = data['main']['temp'] + return temp + else: + return None + +def main(): + api_key = "YOUR_API_KEY_HERE" # Replace this with your OpenWeatherMap API key + city = input("Enter a major U.S. city: ").strip() + + temp = get_temperature(city, api_key) + if temp is not None: + print(f"The current temperature in {city.title()} is {temp:.1f}°F.") + else: + print("Sorry, couldn't find weather data for that city.") + +if __name__ == "__main__": + main() From b5287dbc358684c1346536ba00838de2b7b96a8f Mon Sep 17 00:00:00 2001 From: Omar G Ghafour Date: Tue, 27 May 2025 17:29:16 -0400 Subject: [PATCH 2/4] Update weather-app.py Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- weather-app.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/weather-app.py b/weather-app.py index ed32d15..16a9e6f 100644 --- a/weather-app.py +++ b/weather-app.py @@ -17,9 +17,17 @@ def get_temperature(city_name, api_key): return None def main(): - api_key = "YOUR_API_KEY_HERE" # Replace this with your OpenWeatherMap API key + import os + api_key = os.getenv('OPENWEATHER_API_KEY') + if not api_key: + print("Error: Please set the OPENWEATHER_API_KEY environment variable") + return + city = input("Enter a major U.S. city: ").strip() - + if not city: + print("Error: City name cannot be empty") + return + temp = get_temperature(city, api_key) if temp is not None: print(f"The current temperature in {city.title()} is {temp:.1f}°F.") From b27abc1ff4d02f3e75cf8ead1866f21a21f3b101 Mon Sep 17 00:00:00 2001 From: Omar G Ghafour Date: Tue, 27 May 2025 17:36:33 -0400 Subject: [PATCH 3/4] Update weather-app.py --- weather-app.py | 47 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/weather-app.py b/weather-app.py index 16a9e6f..5f77bdb 100644 --- a/weather-app.py +++ b/weather-app.py @@ -1,20 +1,51 @@ import requests def get_temperature(city_name, api_key): + """ + Fetches the current temperature in Fahrenheit for a given U.S. city + using the OpenWeatherMap API. + + Parameters: + city_name (str): The name of the U.S. city. + api_key (str): Your OpenWeatherMap API key. + + Returns: + float: Current temperature in Fahrenheit if successful. + None: If an error occurs or the data cannot be retrieved. + """ + if not api_key or not isinstance(api_key, str): + print("Error: Invalid or missing API key.") + return None + base_url = "https://api.openweathermap.org/data/2.5/weather" params = { 'q': f"{city_name},US", 'appid': api_key, - 'units': 'imperial' # Fahrenheit + 'units': 'imperial' } - response = requests.get(base_url, params=params) - - if response.status_code == 200: + + try: + response = requests.get(base_url, params=params, timeout=10) + response.raise_for_status() data = response.json() - temp = data['main']['temp'] - return temp - else: - return None + + if 'main' in data and 'temp' in data['main']: + return data['main']['temp'] + else: + print("Error: Unexpected response structure.") + return None + except requests.exceptions.HTTPError as http_err: + print(f"HTTP error occurred: {http_err} - {response.text}") + except requests.exceptions.ConnectionError: + print("Error: Network connection error.") + except requests.exceptions.Timeout: + print("Error: The request timed out.") + except requests.exceptions.RequestException as err: + print(f"Error: An unexpected error occurred: {err}") + except ValueError: + print("Error: Failed to parse JSON response.") + + return None def main(): import os From 0fc090c163e77a4ca0fe70d7435305d9f2919dce Mon Sep 17 00:00:00 2001 From: Omar G Ghafour Date: Tue, 27 May 2025 17:39:19 -0400 Subject: [PATCH 4/4] Update weather-app.py Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- weather-app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/weather-app.py b/weather-app.py index 5f77bdb..794e51f 100644 --- a/weather-app.py +++ b/weather-app.py @@ -35,7 +35,7 @@ def get_temperature(city_name, api_key): print("Error: Unexpected response structure.") return None except requests.exceptions.HTTPError as http_err: - print(f"HTTP error occurred: {http_err} - {response.text}") + print(f"HTTP error occurred: {http_err}") except requests.exceptions.ConnectionError: print("Error: Network connection error.") except requests.exceptions.Timeout: