From 97cbdfc02f46e7abd475b1f07ddaf5c3cd4acf4b Mon Sep 17 00:00:00 2001 From: tillandert Date: Thu, 3 Oct 2024 19:59:42 -0400 Subject: [PATCH] Add weather_notifier.py and README.md --- weather_notifier/README.md | 51 +++++++++++++++++ weather_notifier/weather_notifier.py | 85 ++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 weather_notifier/README.md create mode 100644 weather_notifier/weather_notifier.py diff --git a/weather_notifier/README.md b/weather_notifier/README.md new file mode 100644 index 000000000..edfecd331 --- /dev/null +++ b/weather_notifier/README.md @@ -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. diff --git a/weather_notifier/weather_notifier.py b/weather_notifier/weather_notifier.py new file mode 100644 index 000000000..fb8d30f45 --- /dev/null +++ b/weather_notifier/weather_notifier.py @@ -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() \ No newline at end of file