Added ability to process more than one EO

This commit is contained in:
Anry Das
2026-04-04 11:53:13 +03:00
parent 108ba70991
commit 94e683ba24
2 changed files with 87 additions and 43 deletions

124
get_eo.py
View File

@@ -3,8 +3,11 @@
import os import os
import sys import sys
from dataclasses import dataclass
from io import StringIO from io import StringIO
from types import SimpleNamespace from types import SimpleNamespace
from typing import List
import requests import requests
import json import json
import urllib3 import urllib3
@@ -59,6 +62,25 @@ def read_config():
return tg_token, tg_chat, oe_account_number, is_debug, settlement, street, house, building_part_number, apartment return tg_token, tg_chat, oe_account_number, is_debug, settlement, street, house, building_part_number, apartment
@dataclass
class POData:
city: str
street: str
house: int
po_type: str
reason: str
placement_date: str
start_date: str
stop_date: str
start_time: str
stop_time: str
_notified_at: dt = None
need_to_send: bool = True
PARSED_PO_DATAS: List[POData]
def send_message_to_tg(msg, token, chat): def send_message_to_tg(msg, token, chat):
url = f"https://api.telegram.org/bot{token}/sendMessage?chat_id={chat}&parse_mode=html&text={msg}" url = f"https://api.telegram.org/bot{token}/sendMessage?chat_id={chat}&parse_mode=html&text={msg}"
return requests.get(url) return requests.get(url)
@@ -191,12 +213,18 @@ def save_last_datas_GPV(datas):
f.write(str(datas)) f.write(str(datas))
def save_last_datas_PO(placed='', star_dt='', start_tm='', stop_dt='', stop_tm=''): # def save_last_datas_PO(placed='', star_dt='', start_tm='', stop_dt='', stop_tm=''):
# date = dt.today().strftime('%Y-%m-%d')
# with open(LAST_DATAS_FILE_NAME_PO, 'w') as f:
# f.write(LAST_DATAS_FORMAT_PO.format(placed=str(placed), star_dt=str(star_dt), start_tm=str(start_tm),
# stop_dt=str(stop_dt), stop_tm=str(stop_tm),
# current_date=date))
def save_last_datas_PO(po_data: List[POData]):
date = dt.today().strftime('%Y-%m-%d') date = dt.today().strftime('%Y-%m-%d')
with open(LAST_DATAS_FILE_NAME_PO, 'w') as f: with open(LAST_DATAS_FILE_NAME_PO, 'w') as f:
f.write(LAST_DATAS_FORMAT_PO.format(placed=str(placed), star_dt=str(star_dt), start_tm=str(start_tm), f.write(str(po_data) + ' ' + date)
stop_dt=str(stop_dt), stop_tm=str(stop_tm),
current_date=date))
def load_last_datas(file_name): def load_last_datas(file_name):
@@ -223,6 +251,11 @@ def is_old_and_new_datas_equals_PO(placed='', star_dt='', start_tm='', stop_dt='
stop_dt=str(stop_dt), stop_dt=str(stop_dt),
stop_tm=str(stop_tm), current_date=date) == loaded stop_tm=str(stop_tm), current_date=date) == loaded
def is_old_and_new_datas_equals_PO(po_datas: List[POData]):
date = dt.today().strftime('%Y-%m-%d')
loaded = load_last_datas(LAST_DATAS_FILE_NAME_PO)
return str(po_datas)+ ' ' + date == loaded
def is_need_toSend(approved_from, event_date, hours_off, hours_on): def is_need_toSend(approved_from, event_date, hours_off, hours_on):
need_to_send = not is_old_and_new_datas_equals_GPV(hours_off, hours_on, approved_from, event_date) need_to_send = not is_old_and_new_datas_equals_GPV(hours_off, hours_on, approved_from, event_date)
@@ -257,50 +290,61 @@ def is_outage_date_before_or_same(stop_date):
return current_date <= parsed_date return current_date <= parsed_date
def get_message_with_PO(root, def parse_po_data(root) -> List[POData]:
html): # city, street, house, placement_date, PO_type, reason, start_date, stop_date, start_time, stop_time res: List[POData] = []
if "<div class='shutdowns'>" in html: for i in range(len(root.findall(".//div[@class='table-row flex']"))):
city = root.xpath("//div[@class='table-row flex']/div[@class='city']/text()")[0] po_data = POData(city=root.xpath("//div[@class='table-row flex']/div[@class='city']/text()")[i],
street = root.xpath("//div[@class='table-row flex']/div[@class='street']/text()")[0] street=root.xpath("//div[@class='table-row flex']/div[@class='street']/text()")[i],
house = root.xpath("//div[@class='table-row flex']/div[@class='house_number']/text()")[0] house=root.xpath("//div[@class='table-row flex']/div[@class='house_number']/text()")[i],
PO_type = root.xpath("//div[@class='table-row flex']/div[@class='type-shutdown']/text()")[0] po_type=root.xpath("//div[@class='table-row flex']/div[@class='type-shutdown']/text()")[i],
reason = root.xpath( reason=root.xpath("//div[@class='table-row flex']/div[@class='reason-shutdown reason-text-container']/div[@class='reason-text-hide']/text()")[i],
"//div[@class='table-row flex']/div[@class='reason-shutdown reason-text-container']/div[@class='reason-text-hide']/text()")[ placement_date=root.xpath("//div[@class='table-row flex']/div[@class='placement_date']/div[@class='date']/text()")[i], start_date=
0] root.xpath("//div[@class='table-row flex']/div[@class='shutdown_date']/div[@class='date']/text()")[i],
placement_date = \ stop_date=root.xpath("//div[@class='table-row flex']/div[@class='turn_on_date']/div[@class='date']/text()")[i],
root.xpath("//div[@class='table-row flex']/div[@class='placement_date']/div[@class='date']/text()")[0] start_time=root.xpath("//div[@class='table-row flex']/div[@class='shutdown_date']/div[@class='time']/text()")[i],
start_date = root.xpath("//div[@class='table-row flex']/div[@class='shutdown_date']/div[@class='date']/text()")[ stop_time=root.xpath("//div[@class='table-row flex']/div[@class='turn_on_date']/div[@class='time']/text()")[i]
0] )
stop_date = root.xpath("//div[@class='table-row flex']/div[@class='turn_on_date']/div[@class='date']/text()")[0] # po_data.need_to_send = not is_old_and_new_datas_equals_PO(placed=po_data.placement_date, star_dt=po_data.start_date,
start_time = root.xpath("//div[@class='table-row flex']/div[@class='shutdown_date']/div[@class='time']/text()")[ # start_tm=po_data.start_time, stop_dt=po_data.stop_date,
0] # stop_tm=po_data.stop_time)
stop_time = root.xpath("//div[@class='table-row flex']/div[@class='turn_on_date']/div[@class='time']/text()")[0] if is_outage_date_before_or_same(po_data.stop_date):
address = get_address(city, street, house) po_data.need_to_send = True
message = ( if po_data not in res:
f'\U000026A0 <b>Увага!</b>\nНа {start_date} за адресою {address} заплановано <b>{PO_type}</b> відключення (заявка від {placement_date}).\n' res.append(po_data)
f'Причина відключення: {reason}\n'
f'Початок вимкнення <b>{start_date} об {start_time}</b>\n' res.sort(key=lambda pd: pd.start_date)
f'Кінець вимкнення <b>{stop_date} об {stop_time}</b>\n' return res
def get_message_with_PO(root, html): # city, street, house, placement_date, PO_type, reason, start_date, stop_date, start_time, stop_time
message = ''
need_to_send = False
global PARSED_PO_DATAS
PARSED_PO_DATAS = parse_po_data(root)
if "<div class='shutdowns'>" in html:
for el in PARSED_PO_DATAS:
if message:
message += '\n'
address = get_address(el.city, el.street, el.house)
message = message + (
f'\U000026A0 <b>Увага!</b>\nНа {el.start_date} за адресою {address} заплановано <b>{el.po_type}</b> відключення (заявка від {el.placement_date}).\n'
f'Причина відключення: {el.reason}\n'
f'Початок вимкнення <b>{el.start_date} об {el.start_time}</b>\n'
f'Кінець вимкнення <b>{el.stop_date} об {el.stop_time}</b>\n'
f'{MENTIONS}') f'{MENTIONS}')
need_to_send = (not is_old_and_new_datas_equals_PO(placed=placement_date, star_dt=start_date, # need_to_send = el.need_to_send or False
start_tm=start_time, stop_dt=stop_date, stop_tm=stop_time))
if is_outage_date_before_or_same(stop_date): need_to_send = not is_old_and_new_datas_equals_PO(PARSED_PO_DATAS)
need_to_send = need_to_send and True
else:
message = '\nПланових вимкнень немає \U0001F44C \U0001F483'
need_to_send = not is_old_and_new_datas_equals_PO(placed=placement_date, star_dt=start_date,
start_tm=start_time, stop_dt=stop_date, stop_tm=stop_time)
if need_to_send:
save_last_datas_PO(placed=placement_date, star_dt=start_date, start_tm=start_time, stop_dt=stop_date,
stop_tm=stop_time)
else: else:
message = '\nПланових вимкнень немає \U0001F44C \U0001F483' message = '\nПланових вимкнень немає \U0001F44C \U0001F483'
need_to_send = not is_old_and_new_datas_equals_PO() need_to_send = not is_old_and_new_datas_equals_PO()
if need_to_send: if need_to_send:
save_last_datas_PO() save_last_datas_PO(PARSED_PO_DATAS)
return message, need_to_send return message, need_to_send

View File

@@ -4,9 +4,9 @@ account: !secret account
debug: "false" debug: "false"
settlement: !secret settlement settlement: !secret settlement
street: !secret street street: !secret street
house: "9" house: !secret house
building_part_number: "" building_part_number: ""
apartment: "" apartment: !secret apartment
mentions: !secret mentions mentions: !secret mentions
use_matrix: "true" use_matrix: "true"
mx_server: !secret mx_server mx_server: !secret mx_server