From a3c4fb0257f40c08bf86baddfa7b0efbbc08c4be Mon Sep 17 00:00:00 2001 From: Meghdad <61308304+MeghdadFadaee@users.noreply.github.com> Date: Fri, 17 Jul 2026 03:57:03 +0330 Subject: [PATCH] Add .env configuration support --- .env.example | 12 ++++++++++++ README.md | 30 +++++++++++++++++++++++++++++- main.py | 26 +++++++++++++++++++------- requirements.txt | 3 ++- 4 files changed, 62 insertions(+), 9 deletions(-) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..8ebaaa0 --- /dev/null +++ b/.env.example @@ -0,0 +1,12 @@ +# API token used to retrieve planned outages +BARGHEMAN_TOKEN=replace-with-bargheman-token + +# Eitaa bot configuration +EITAAYAR_TOKEN=replace-with-eitaayar-token +CHAT_ID=replace-with-chat-id + +# One or more comma-separated bill IDs +BILL_IDS=7356609804429,761/pe9314604429 + +# JSON file used to remember outages that have already been announced +BLACKOUTS_FILE=blackouts.json diff --git a/README.md b/README.md index 60dc57b..2d93b33 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,30 @@ # daily-blackout-check -check every hour for daily power outage + +Checks planned power outages and sends new notices to an Eitaa chat. + +## Setup + +1. Create and activate a Python virtual environment, then install dependencies: + + ```sh + python -m venv .venv + source .venv/bin/activate + pip install -r requirements.txt + ``` + +2. Copy the example configuration and replace its placeholder values: + + ```sh + cp .env.example .env + ``` + +3. Run the checker: + + ```sh + python main.py + ``` + +The `.env` file is ignored by Git so tokens are not committed. Environment +variables supplied by the shell or GitHub Actions take precedence over `.env`. + +`BILL_IDS` accepts one or more comma-separated electricity bill IDs. diff --git a/main.py b/main.py index 07e0ca2..a186eb6 100644 --- a/main.py +++ b/main.py @@ -2,15 +2,27 @@ import os import json import requests from jdatetime import datetime, timedelta +from dotenv import load_dotenv -BARGHEMAN_TOKEN = os.getenv("BARGHEMAN_TOKEN") -EITAAYAR_TOKEN = os.getenv("EITAAYAR_TOKEN") -CHAT_ID = os.getenv("CHAT_ID") -BILL_IDS = os.getenv("BILL_IDS", "").split(",") -BLACKOUTS_FILE = os.getenv("BLACKOUTS_FILE") +# Load local configuration without overriding variables supplied by the shell or CI. +load_dotenv() -if not BARGHEMAN_TOKEN or not EITAAYAR_TOKEN or not CHAT_ID or len(BILL_IDS) == 0: - raise EnvironmentError("Environment is not ready!") +BARGHEMAN_TOKEN = os.getenv("BARGHEMAN_TOKEN", "").strip() +EITAAYAR_TOKEN = os.getenv("EITAAYAR_TOKEN", "").strip() +CHAT_ID = os.getenv("CHAT_ID", "").strip() +BILL_IDS = [bill_id.strip() for bill_id in os.getenv("BILL_IDS", "").split(",") if bill_id.strip()] +BLACKOUTS_FILE = os.getenv("BLACKOUTS_FILE", "").strip() + +required_settings = { + "BARGHEMAN_TOKEN": BARGHEMAN_TOKEN, + "EITAAYAR_TOKEN": EITAAYAR_TOKEN, + "CHAT_ID": CHAT_ID, + "BILL_IDS": BILL_IDS, + "BLACKOUTS_FILE": BLACKOUTS_FILE, +} +missing_settings = [name for name, value in required_settings.items() if not value] +if missing_settings: + raise EnvironmentError(f"Missing required environment variables: {', '.join(missing_settings)}") today = datetime.now() to_date = today + timedelta(days=5) diff --git a/requirements.txt b/requirements.txt index 246d8c2..7a43e0b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ requests jdatetime -beautifulsoup4 \ No newline at end of file +beautifulsoup4 +python-dotenv