|
| 1 | +<h1 align='center'>Get Live Weather Desktop Notifications Project</h1> |
| 2 | + |
| 3 | +## Source Code Explanation |
| 4 | + ### **Imports:** |
| 5 | + - `import aiohttp`: Enables making asynchronous HTTP requests using the `aiohttp` library. |
| 6 | + - `import asyncio`: Provides functionalities for asynchronous programming. |
| 7 | + - `import logging`: Facilitates logging messages for debugging and monitoring purposes. |
| 8 | + - `import argparse`: Allows for parsing command-line arguments when running the script. |
| 9 | + - `from win10toast import ToastNotifier`: Imports the `ToastNotifier` class from the `win10toast` library for displaying toast notifications (Windows-specific). |
| 10 | + |
| 11 | + ### **Logging Configuration:** |
| 12 | + |
| 13 | + - `logging.basicConfig(level=logging.INFO)`: Sets up the basic logging configuration to print messages with a severity level of `INFO` (and higher) to the console. |
| 14 | + |
| 15 | + ### **Toast Notifier Initialization:** |
| 16 | + |
| 17 | + - `notifier = ToastNotifier()`: Creates an instance of the `ToastNotifier` class, which will be used to display toast notifications. |
| 18 | + |
| 19 | + ### **`fetch_data` Function:** |
| 20 | + |
| 21 | + - `async def fetch_data(url):`: Defines an asynchronous function named `fetch_data` that takes a URL as input. |
| 22 | + - `API_KEY = "apikey=YOUR_API_KEY"`: Stores your Tomorrow.io API key (replace `YOUR_API_KEY` with your actual key). This key is crucial for accessing weather data from the Tomorrow.io API. |
| 23 | + - **Important:** Never store your API key directly in the code. Consider environment variables or a configuration file for a more secure approach. |
| 24 | + |
| 25 | + - `headers = {"accept": "application/json"}`: Sets the HTTP header to specify that the function expects JSON data in response. |
| 26 | + - `async with aiohttp.ClientSession() as session:`: Establishes an asynchronous context manager using `aiohttp.ClientSession()`. This context manager will handle creating and closing the HTTP session efficiently. |
| 27 | + - **Note:** `aiohttp.ClientSession()` is specifically designed for asynchronous HTTP requests. |
| 28 | + |
| 29 | + - `try:`: Initiates a `try` block to handle potential exceptions during the HTTP request. |
| 30 | + - `async with session.get(url + API_KEY, headers=headers) as response:`: Makes an asynchronous GET request using the `session` object. It appends the API key to the URL and sets the specified headers. |
| 31 | + - `response.raise_for_status()`: Raises an exception if the response status code indicates an error. |
| 32 | + - `return await response.json()`: Awaits the asynchronous response and returns the parsed JSON data (if successful). |
| 33 | + |
| 34 | + - `except aiohttp.ClientError as e:`: Catches exceptions of type `aiohttp.ClientError` that might occur during the HTTP request. |
| 35 | + - `logging.error(f'Failed to fetch data {e}')`: Logs an error message with details about the exception. |
| 36 | + - `return`: Returns `None` to indicate that no data could be fetched. |
| 37 | + |
| 38 | + ### **`formate_data` Function:** |
| 39 | + |
| 40 | + - `async def formate_data(data):`: Defines an asynchronous function named `formate_data` that takes weather data as input (assumed to be in JSON format). |
| 41 | + - `if "timelines" in data and 'minutely' in data['timelines']:`: Checks if the JSON data has the expected structure, containing "timelines" and "minutely" keys. |
| 42 | + - `first_entry = data['timelines']['minutely'][0]`: Extracts the first entry (representing the current weather) from the "minutely" timeline. |
| 43 | + - `time = first_entry['time']`: Retrieves the time from the entry. |
| 44 | + - `values = first_entry['values']`: Extracts the weather values from the entry. |
| 45 | + |
| 46 | + - `data_Summary = ...`: Constructs a multi-line string containing formatted weather data using f-strings. |
| 47 | + - `else:`: If the expected structure is not found, the message "No weather data available at this movement." is returned. |
| 48 | + - `return data_Summary`: Returns the formatted weather summary or the "No data" message. |
| 49 | + |
| 50 | + ### **`main` Function:** |
| 51 | + |
| 52 | + - `async def main(location, update_interval, notification_duration):`: Defines an asynchronous function named `main` that takes three arguments: |
| 53 | + - `location`: The location for which to fetch weather data (e.g., "Pakistan, PK"). |
| 54 | + - `update_interval`: The interval between weather updates in seconds (default: 36 |
| 55 | + |
| 56 | + ## Usage of weather_notifier.py |
| 57 | + |
| 58 | +This Python script provides live weather notifications on your Windows system using the Tomorrow.io API. Here's how to use it: |
| 59 | + |
| 60 | +### **Requirements:** |
| 61 | + |
| 62 | + - Python 3.x |
| 63 | + - `aiohttp` library: Install with `pip install aiohttp` |
| 64 | + - `win10toast` library (Windows-specific): Install with `pip install win10toast` |
| 65 | + - A Tomorrow.io API key: Create a free account at [https://app.tomorrow.io/signup](https://app.tomorrow.io/signup) and obtain an API key. |
| 66 | + |
| 67 | + **Instructions:** |
| 68 | + |
| 69 | + 1. **Obtain Your API Key:** |
| 70 | + |
| 71 | + - Sign up for a free Tomorrow.io account ([https://app.tomorrow.io/signup](https://app.tomorrow.io/signup)). |
| 72 | + - Go to your account settings and navigate to the API section. |
| 73 | + - Generate a new API key or use an existing one. |
| 74 | + 2. **Replace the Placeholder API Key:** |
| 75 | + |
| 76 | + - Open the `weather_notifier.py` script in a text editor. |
| 77 | + - Find the line `API_KEY = "apikey=YOUR_API_KEY"` and replace `YOUR_API_KEY` with your actual API key. |
| 78 | + - **Important:** Never store your API key directly in the code. Consider using environment variables or a separate configuration file for better security. |
| 79 | + 3. **Run the Script:** |
| 80 | + |
| 81 | + - Open a terminal or command prompt and navigate to the directory where you saved the script. |
| 82 | + - Run the script with the following command, replacing `LOCATION` with your desired location code (e.g., `Pakistan, PK`): |
| 83 | + |
| 84 | + **Bash** |
| 85 | + ``` |
| 86 | + python weather_notifier.py LOCATION |
| 87 | + ``` |
| 88 | + |
| 89 | + 4. **Optional Arguments:** |
| 90 | + |
| 91 | + - ```-u```, ```--update-interval``` : Specify the update interval in seconds between weather checks (default: 3600 seconds, or 1 hour). |
| 92 | + |
| 93 | + - ```-d```, ```--notification-duration``` : Set the duration for which the toast notification should stay visible in seconds (default: 3 seconds). |
| 94 | + |
| 95 | + ``` |
| 96 | + python weather_notifier.py LOCATION -u 1800 -d 5 # Update every 30 minutes (1800 seconds) with a 5-second notification duration |
| 97 | + ``` |
| 98 | + |
0 commit comments