Skip to content

Created weather_notifier.py #965

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions weather_notifier/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Weather Notifier

This script provides a simple GUI application to display weather information for a specified city and can notify the user at regular intervals.

## Prerequisites

- Python 3.x
- `requests` library

## Installation

1. Clone the repository or download the script.
2. Install the required libraries using pip:
```sh
pip install requests
```

## Setup

1. Obtain an API key from [OpenWeatherMap](https://openweathermap.org/api).
2. Set the API key as an environment variable:
- On Windows:
```sh
setx OPENWEATHERMAP_API_KEY "your_api_key_here"
```
- On macOS/Linux:
```sh
export OPENWEATHERMAP_API_KEY="your_api_key_here"
```

## Usage

1. Navigate to the directory containing the script.
2. Run the script:
```sh
python weather_notifier.py
```
3. Enter the city name and the interval for notifications.
4. Click "Get Weather" to display the current weather.
5. Click "Start Notifier" to start receiving weather notifications at the specified interval.
6. Click "Stop Notifier" to stop the notifications.

## Features

- Display current weather information for a specified city.
- Notify the user at regular intervals with updated weather information.
- Supports intervals in seconds, minutes, hours, and days.

## Acknowledgements

- [OpenWeatherMap](https://openweathermap.org/) for providing the weather API.
85 changes: 85 additions & 0 deletions weather_notifier/weather_notifier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import tkinter as tk
from tkinter import messagebox
import requests
import time
import threading
import os

# Global variable to control the notifier state
notifier_running = False

# Function to get weather data
def get_weather(api_key, city):
base_url = "http://api.openweathermap.org/data/2.5/weather?"
complete_url = base_url + "appid=" + api_key + "&q=" + city
response = requests.get(complete_url)
return response.json()

# Function to display weather
def display_weather():
city = city_entry.get()
api_key = os.getenv("OPENWEATHERMAP_API_KEY") # Get API key from environment variable
if not api_key:
messagebox.showerror("Error", "API key not found in environment variables")
return

weather_data = get_weather(api_key, city)

if weather_data["cod"] != "404":
main = weather_data["main"]
weather_desc = weather_data["weather"][0]["description"]
temp_celsius = main["temp"] - 273.15 # Convert from Kelvin to Celsius
temp_fahrenheit = (temp_celsius * 9/5) + 32 # Convert from Celsius to Fahrenheit
messagebox.showinfo("Weather Info", f"City: {city}\nTemperature: {temp_celsius:.2f}°C / {temp_fahrenheit:.2f}°F\nDescription: {weather_desc}")
else:
messagebox.showerror("Error", "City Not Found")

# Function to run the notifier at fixed intervals
def run_notifier(interval):
global notifier_running
while notifier_running:
display_weather()
time.sleep(interval)

# Function to start the notifier in a separate thread
def start_notifier():
global notifier_running
notifier_running = True
interval = int(interval_entry.get())
time_unit = time_unit_var.get()

if time_unit == "Minute(s)":
interval *= 60
elif time_unit == "Hour(s)":
interval *= 3600
elif time_unit == "Day(s)":
interval *= 86400

threading.Thread(target=run_notifier, args=(interval,)).start()

# Function to stop the notifier
def stop_notifier():
global notifier_running
notifier_running = False

# GUI setup
root = tk.Tk()
root.title("Weather Notifier")

tk.Label(root, text="City:").grid(row=0, column=0)
city_entry = tk.Entry(root)
city_entry.grid(row=0, column=1)

tk.Label(root, text="Interval:").grid(row=1, column=0)
interval_entry = tk.Entry(root)
interval_entry.grid(row=1, column=1)

time_unit_var = tk.StringVar(value="Second(s)")
time_unit_menu = tk.OptionMenu(root, time_unit_var, "Second(s)", "Minute(s)", "Hour(s)", "Day(s)")
time_unit_menu.grid(row=1, column=2)

tk.Button(root, text="Get Weather", command=display_weather).grid(row=2, column=0)
tk.Button(root, text="Start Notifier", command=start_notifier).grid(row=2, column=1)
tk.Button(root, text="Stop Notifier", command=stop_notifier).grid(row=2, column=2)

root.mainloop()
Loading