Add .env configuration support
This commit is contained in:
12
.env.example
Normal file
12
.env.example
Normal 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
|
||||||
30
README.md
30
README.md
@@ -1,2 +1,30 @@
|
|||||||
# daily-blackout-check
|
# 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
26
main.py
@@ -2,15 +2,27 @@ import os
|
|||||||
import json
|
import json
|
||||||
import requests
|
import requests
|
||||||
from jdatetime import datetime, timedelta
|
from jdatetime import datetime, timedelta
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
BARGHEMAN_TOKEN = os.getenv("BARGHEMAN_TOKEN")
|
# Load local configuration without overriding variables supplied by the shell or CI.
|
||||||
EITAAYAR_TOKEN = os.getenv("EITAAYAR_TOKEN")
|
load_dotenv()
|
||||||
CHAT_ID = os.getenv("CHAT_ID")
|
|
||||||
BILL_IDS = os.getenv("BILL_IDS", "").split(",")
|
|
||||||
BLACKOUTS_FILE = os.getenv("BLACKOUTS_FILE")
|
|
||||||
|
|
||||||
if not BARGHEMAN_TOKEN or not EITAAYAR_TOKEN or not CHAT_ID or len(BILL_IDS) == 0:
|
BARGHEMAN_TOKEN = os.getenv("BARGHEMAN_TOKEN", "").strip()
|
||||||
raise EnvironmentError("Environment is not ready!")
|
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()
|
today = datetime.now()
|
||||||
to_date = today + timedelta(days=5)
|
to_date = today + timedelta(days=5)
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
requests
|
requests
|
||||||
jdatetime
|
jdatetime
|
||||||
beautifulsoup4
|
beautifulsoup4
|
||||||
|
python-dotenv
|
||||||
|
|||||||
Reference in New Issue
Block a user