-
Notifications
You must be signed in to change notification settings - Fork 0
Add weather-app.py script to fetch current temperature using OpenWeatherMap API #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
omar-coderabbitai
wants to merge
4
commits into
main
Choose a base branch
from
og-test-feature
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
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' | ||
} | ||
|
||
try: | ||
response = requests.get(base_url, params=params, timeout=10) | ||
response.raise_for_status() | ||
data = response.json() | ||
|
||
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 | ||
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.") | ||
else: | ||
print("Sorry, couldn't find weather data for that city.") | ||
|
||
if __name__ == "__main__": | ||
main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.