Add .env configuration support

This commit is contained in:
Meghdad
2026-07-17 03:57:03 +03:30
parent 09cedc9c4b
commit a3c4fb0257
4 changed files with 62 additions and 9 deletions

12
.env.example Normal file
View File

@@ -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

View File

@@ -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.

26
main.py
View File

@@ -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)

View File

@@ -1,3 +1,4 @@
requests
jdatetime
beautifulsoup4
beautifulsoup4
python-dotenv