Files
daily-blackout-check/main.py
2025-06-02 00:19:50 +03:30

113 lines
3.4 KiB
Python

import os
import json
import requests
from jdatetime import datetime, timedelta
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 = "blackouts.json"
if not BARGHEMAN_TOKEN or not EITAAYAR_TOKEN or not CHAT_ID or len(BILL_IDS) == 0:
raise EnvironmentError("Environment is not ready!")
today = datetime.now()
to_date = today + timedelta(days=5)
def get_blackouts(bill_id) -> list:
url = "https://uiapi2.saapa.ir/api/ebills/PlannedBlackoutsReport"
url = "https://abat-lms.rodad.net/PlannedBlackoutsReport"
headers = {
'accept': 'application/json',
"Authorization": f"Bearer {BARGHEMAN_TOKEN}",
}
payload = {
"bill_id": bill_id,
"from_date": today.strftime("%Y/%m/%d"),
"to_date": to_date.strftime("%Y/%m/%d"),
}
response = requests.post(url, headers=headers, json=payload)
if response.ok:
return response.json().get("data", [])
else:
print(f"❌ Failed to get blackouts: {response.text}")
return []
def load_blackouts() -> dict:
if os.path.exists(BLACKOUTS_FILE):
with open(BLACKOUTS_FILE, "r", encoding="utf-8") as f:
return json.load(f)
return {}
def save_blackouts(data) -> None:
with open(BLACKOUTS_FILE, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
def format_message(bill_id, entry):
RTLM = "\u200f"
return f"{RTLM}\n{RTLM}".join([
f"⚡️ اطلاعیه خاموشی برق",
f"🆔 شناسه قبض: {bill_id}",
f"📅 تاریخ: {entry['outage_date']}",
f"⏰ ساعت: {entry['outage_start_time']} تا {entry['outage_stop_time']}",
f"📍 محل: {entry['outage_address']}",
f"📝 دلیل: {entry['reason_outage']}",
f"📥 ارسال شده در {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}",
f".",
])
def send_message(text):
url = f"https://eitaayar.ir/api/{EITAAYAR_TOKEN}/sendMessage"
payload = {
"chat_id": CHAT_ID,
"text": text,
"parse_mode": "HTML"
}
response = requests.get(url, params=payload)
if not response.ok:
print(f"❌ Failed to send message: {response.text}")
def main() -> None:
previous_data = load_blackouts()
updated_data = previous_data.copy()
for bill_id in BILL_IDS:
new_blackouts = get_blackouts(bill_id)
old_blackouts = previous_data.get(bill_id, [])
new_entries = [entry for entry in new_blackouts if entry not in old_blackouts]
for entry in new_entries:
message = format_message(bill_id, entry)
send_message(message)
if new_entries:
updated_data[bill_id] = old_blackouts + new_entries
if updated_data != previous_data:
save_blackouts(updated_data)
os.system("git config --global user.name 'github-actions'")
os.system("git config --global user.email 'github-actions@github.com'")
os.system("git add " + BLACKOUTS_FILE)
os.system("git commit -m 'Update outage data'")
os.system("git push")
if __name__ == '__main__':
main()
"""
curl ^"https://uiapi2.saapa.ir/api/ebills/PlannedBlackoutsReport^" ^
--compressed ^
-X POST ^
-H ^"Authorization: Bearer ...^" ^
--data-raw ^"{\"bill_id\":\"7619314604429\",\"from_date\":\"1404/03/11\",\"to_date\":\"1404/03/16\"}^"
"""