Skip to content

Commit f7d0033

Browse files
committed
weather checker
weather checker
1 parent fee6c3f commit f7d0033

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

weather_checker/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
3+
# Weather Checker
4+
5+
A Python script to fetch weather data for Santa Barbara (ZIP code 93001) using the Open-Meteo API.
6+
7+
## Requirements
8+
9+
- No API key is needed for this script.
10+
11+
## Usage
12+
13+
1. Install the required dependency:
14+
```sh
15+
pip install requests
16+
```
17+
18+
2. Run the script:
19+
```sh
20+
python weather_checker.py
21+
```
22+
23+
3. The script will output weather details like temperature, conditions, humidity, and wind speed for Santa Barbara.
24+
25+
## Example Output
26+
27+
```
28+
Weather in Santa Barbara:
29+
Temperature: 25°C (Feels like 28°C)
30+
Conditions: 0 (Clear)
31+
Humidity: 64%
32+
Wind Speed: 5 km/h
33+
```
34+
35+
## Notes
36+
37+
- The script is hardcoded to fetch weather for Santa Barbara (`34.42, -119.70`). If you want to check a different location, modify the `latitude` and `longitude` values in the `get_weather` function.
38+
- Open-Meteo provides data in metric units by default (Celsius). No need for additional parameters unless you prefer imperial units.
39+
- Ensure your internet connection is stable when running the script, as it relies on an external API.

weather_checker/weather_checker.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Import required libraries
2+
import requests
3+
4+
def get_weather():
5+
"""
6+
Fetches weather data for Santa Barbara (hardcoded coordinates) using Open-Meteo API.
7+
8+
Returns:
9+
dict: Weather data if successful, None otherwise.
10+
"""
11+
base_url = "https://api.open-meteo.com/v1/weather"
12+
params = {
13+
'latitude': 34.42,
14+
'longitude': -119.70,
15+
'current_weather': True,
16+
'hourly': {'temperature_2m': {'time': ['2023-08-25T00']}} # Example date
17+
}
18+
19+
try:
20+
response = requests.get(base_url, params=params)
21+
if response.status_code == 200:
22+
return response.json()
23+
else:
24+
print(f"Error fetching weather data: {response.status_code}")
25+
return None
26+
except Exception as e:
27+
print(f"An error occurred: {e}")
28+
return None
29+
30+
def main():
31+
# Example usage (no ZIP code input, hardcoded for Santa Barbara)
32+
weather_data = get_weather()
33+
34+
if weather_data:
35+
print(f"Weather in {weather_data['current']['location']['name']}:")
36+
print(f"Temperature: {weather_data['current']['temperature']}°C (Feels like {weather_data['current']['apparent_temperature']}°C)")
37+
print(f"Conditions: {weather_data['current']['weathercode']}") # Weather code
38+
print(f"Humidity: {weather_data['hourly']['relativehumidity_2m'][0]}%")
39+
print(f"Wind Speed: {weather_data['current']['windspeed']} km/h")
40+
else:
41+
print("Failed to fetch weather data.")
42+
43+
if __name__ == "__main__":
44+
main()

0 commit comments

Comments
 (0)