implement using bargheman api

This commit is contained in:
2025-06-01 23:25:32 +03:30
parent 33aaf44eaa
commit 117ac3a45a
4 changed files with 185 additions and 73 deletions

View File

@@ -4,6 +4,9 @@ on:
push: push:
branches: branches:
- main - main
schedule:
- cron: "30 */3 * * *"
workflow_dispatch:
jobs: jobs:
run-script: run-script:
@@ -25,7 +28,9 @@ jobs:
- name: Run Python Script - name: Run Python Script
env: env:
BARGHEMAN_TOKEN: ${{ secrets.BARGHEMAN_TOKEN }}
EITAAYAR_TOKEN: ${{ secrets.EITAAYAR_TOKEN }} EITAAYAR_TOKEN: ${{ secrets.EITAAYAR_TOKEN }}
BILL_IDS: ${{ secrets.BILL_IDS }}
CHAT_ID: ${{ secrets.CHAT_ID }} CHAT_ID: ${{ secrets.CHAT_ID }}
run: | run: |
python main.py python main.py

157
main.py
View File

@@ -1,95 +1,106 @@
import os import os
import re import json
import time
import requests import requests
from bs4 import BeautifulSoup from jdatetime import datetime, timedelta
token = os.getenv("EITAAYAR_TOKEN") BARGHEMAN_TOKEN = os.getenv("BARGHEMAN_TOKEN")
chat_id = os.getenv("CHAT_ID") 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 token: if not BARGHEMAN_TOKEN or not EITAAYAR_TOKEN or not CHAT_ID or len(BILL_IDS) == 0:
raise EnvironmentError("EITAAYAR_TOKEN is not set!") raise EnvironmentError("Environment is not ready!")
if not chat_id: today = datetime.now()
raise EnvironmentError("CHAT_ID is not set!") to_date = today + timedelta(days=5)
def send_message(text: str) -> dict: def get_blackouts(bill_id) -> list:
sent_at: int = int(time.time()) url = "https://uiapi2.saapa.ir/api/ebills/PlannedBlackoutsReport"
url = f'https://eitaayar.ir/api/{token}/sendMessage?chat_id={chat_id}&text={text}&title={sent_at=}' headers = {
return requests.get(url).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)
return response.json().get("data", [])
def get_blockout_page() -> str: def load_blackouts() -> dict:
blockout_link = 'https://qepd.co.ir/fa-IR/DouranPortal/6423' if os.path.exists(BLACKOUTS_FILE):
blockout_link = 'https://re.rodad.net/blockout' with open(BLACKOUTS_FILE, "r", encoding="utf-8") as f:
return requests.get(blockout_link).text return json.load(f)
return {}
def convert_to_english_numbers(text): def save_blackouts(data) -> None:
persian_to_english = str.maketrans("۰۱۲۳۴۵۶۷۸۹", "0123456789") with open(BLACKOUTS_FILE, "w", encoding="utf-8") as f:
return text.translate(persian_to_english) json.dump(data, f, ensure_ascii=False, indent=2)
def find_times(text: str) -> list[str]: def format_message(bill_id, entry):
text = convert_to_english_numbers(text) RTLM = "\u200f"
pattern = r"(\d{1,2}:\d{2}|\d{1,2})" return f"{RTLM}\n{RTLM}".join([
return re.findall(pattern, text) 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 is_time_past(time_str: str) -> bool:
try:
current_time = datetime.now(iran_tz).time()
if ":" in time_str:
time_obj = datetime.strptime(time_str.strip(), "%H:%M").time()
else:
time_obj = datetime.strptime(time_str.strip(), "%H").time()
return time_obj < current_time
except Exception:
return false
def make_message(html: str) -> str|None: def send_message(text):
return None url = f"https://eitaayar.ir/api/{EITAAYAR_TOKEN}/sendMessage"
if len(html) == 0: payload = {
return "خطا در خواندن صفحه!" "chat_id": CHAT_ID,
"text": text,
soup = BeautifulSoup(html, 'html.parser') "parse_mode": "HTML"
# table = soup.find('table', id='ctl01_ctl00_myDataList') }
response = requests.get(url, params=payload)
block_needle = 'بلوک A4' if not response.ok:
try: print(f"❌ Failed to send message: {response.text}")
paragraphs = soup.find_all('p')
block = soup.find('p', string=lambda text: isinstance(text, str) and block_needle in text)
time_index = paragraphs.index(block) - 1
text = paragraphs[time_index].get_text(separator='\n', strip=True)
start, end, *_ = find_times(text)
if is_time_past(start):
return None
return text
except:
return f"اطلاعات {block_needle} یافت نشد!"
def main() -> None: def main() -> None:
try: previous_data = load_blackouts()
page = get_blockout_page() updated_data = previous_data.copy()
message = make_message(page)
except requests.exceptions.Timeout: for bill_id in BILL_IDS:
message = 'درخواست ارسال نشد!' new_blackouts = get_blackouts(bill_id)
old_blackouts = previous_data.get(bill_id, [])
if not message:
return None new_entries = [entry for entry in new_blackouts if entry not in old_blackouts]
response = send_message(message) for entry in new_entries:
if not response.get('ok'): message = format_message(bill_id, entry)
if response.get('description'): send_message(message)
raise Exception(response.get('description'))
else: if new_entries:
raise Exception('something sent wrong!') 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 data.json")
os.system("git commit -m 'Update outage data'")
os.system("git push")
if __name__ == '__main__': if __name__ == '__main__':
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\"}^"
"""

View File

@@ -1,2 +1,3 @@
requests requests
jdatetime
beautifulsoup4 beautifulsoup4

95
site-version.py Normal file
View File

@@ -0,0 +1,95 @@
import os
import re
import time
import requests
from bs4 import BeautifulSoup
token = os.getenv("EITAAYAR_TOKEN")
chat_id = os.getenv("CHAT_ID")
if not token:
raise EnvironmentError("EITAAYAR_TOKEN is not set!")
if not chat_id:
raise EnvironmentError("CHAT_ID is not set!")
def send_message(text: str) -> dict:
sent_at: int = int(time.time())
url = f'https://eitaayar.ir/api/{token}/sendMessage?chat_id={chat_id}&text={text}&title={sent_at=}'
return requests.get(url).json()
def get_blockout_page() -> str:
blockout_link = 'https://qepd.co.ir/fa-IR/DouranPortal/6423'
blockout_link = 'https://re.rodad.net/blockout'
return requests.get(blockout_link).text
def convert_to_english_numbers(text):
persian_to_english = str.maketrans("۰۱۲۳۴۵۶۷۸۹", "0123456789")
return text.translate(persian_to_english)
def find_times(text: str) -> list[str]:
text = convert_to_english_numbers(text)
pattern = r"(\d{1,2}:\d{2}|\d{1,2})"
return re.findall(pattern, text)
def is_time_past(time_str: str) -> bool:
try:
current_time = datetime.now(iran_tz).time()
if ":" in time_str:
time_obj = datetime.strptime(time_str.strip(), "%H:%M").time()
else:
time_obj = datetime.strptime(time_str.strip(), "%H").time()
return time_obj < current_time
except Exception:
return false
def make_message(html: str) -> str|None:
return None
if len(html) == 0:
return "خطا در خواندن صفحه!"
soup = BeautifulSoup(html, 'html.parser')
# table = soup.find('table', id='ctl01_ctl00_myDataList')
block_needle = 'بلوک A4'
try:
paragraphs = soup.find_all('p')
block = soup.find('p', string=lambda text: isinstance(text, str) and block_needle in text)
time_index = paragraphs.index(block) - 1
text = paragraphs[time_index].get_text(separator='\n', strip=True)
start, end, *_ = find_times(text)
if is_time_past(start):
return None
return text
except:
return f"اطلاعات {block_needle} یافت نشد!"
def main() -> None:
try:
page = get_blockout_page()
message = make_message(page)
except requests.exceptions.Timeout:
message = 'درخواست ارسال نشد!'
if not message:
return None
response = send_message(message)
if not response.get('ok'):
if response.get('description'):
raise Exception(response.get('description'))
else:
raise Exception('something sent wrong!')
if __name__ == '__main__':
main()