Compare commits
3 Commits
60886e8be2
...
sidetree
| Author | SHA1 | Date | |
|---|---|---|---|
| e5626b7b2d | |||
| 68630eb773 | |||
| dad9cf2104 |
@@ -115,6 +115,7 @@ TRANSLATIONS = {
|
||||
'month': '📅 Весь месяц',
|
||||
'no_events': 'Нет событий',
|
||||
'no_lessons': 'Нет уроков',
|
||||
'time_unknown': 'неизвестно',
|
||||
'free_period': 'Свободно',
|
||||
'unsubscribed': '🔕 Вы отписались от уведомлений.',
|
||||
'diary_title': '📅 <b>Дневник</b> — выберите период:',
|
||||
@@ -186,6 +187,7 @@ TRANSLATIONS = {
|
||||
'month': '📅 Весь місяць',
|
||||
'no_events': 'Немає подій',
|
||||
'no_lessons': 'Немає уроків',
|
||||
'time_unknown': 'невідомо',
|
||||
'free_period': 'Вільно',
|
||||
'unsubscribed': '🔕 Ви відписалися від сповіщень.',
|
||||
'diary_title': '📅 <b>Щоденник</b> — виберіть період:',
|
||||
@@ -257,6 +259,7 @@ TRANSLATIONS = {
|
||||
'month': '📅 Whole month',
|
||||
'no_events': 'No events',
|
||||
'no_lessons': 'No lessons',
|
||||
'time_unknown': 'unknown',
|
||||
'free_period': 'Free',
|
||||
'unsubscribed': '🔕 You have unsubscribed from notifications.',
|
||||
'diary_title': '📅 <b>Diary</b> — choose a period:',
|
||||
@@ -490,7 +493,10 @@ def get_diary_keyboard(user_id: int | None = None):
|
||||
|
||||
|
||||
def _parse_calendar_html(table_html: str) -> tuple:
|
||||
"""Parse calendar HTML table into (month_text, {day_num: {weekday, events}})."""
|
||||
"""Parse calendar HTML table into (month_text, {day_num: {weekday, events}}).
|
||||
|
||||
Each event is a dict: {'title': str, 'id': site event id | None, 'time': 'HH:MM' | None}.
|
||||
"""
|
||||
days = {}
|
||||
weekdays = []
|
||||
rows = re.findall(r'<tr[^>]*>(.*?)</tr>', table_html, re.DOTALL)
|
||||
@@ -522,22 +528,72 @@ def _parse_calendar_html(table_html: str) -> tuple:
|
||||
continue
|
||||
day_num = dm.group(1)
|
||||
|
||||
# Extract events: title attribute (full name) of ALL <a> tags inside the cell
|
||||
# Extract events: title attribute (full name) of ALL <a> tags inside the cell.
|
||||
# Each event keeps its site data-event-id and a 'time' slot (filled later
|
||||
# from the AJAX popup by fetch_diary_data). 'time' is 'HH:MM' or None.
|
||||
events = []
|
||||
for a_match in re.finditer(r'<a[^>]*>(.*?)</a>', cell, re.DOTALL):
|
||||
a_tag = a_match.group(0)
|
||||
# Prefer the title attribute (contains full name, not truncated)
|
||||
title_m = re.search(r'title\s*=\s*"([^"]*)"', a_tag)
|
||||
et = title_m.group(1).strip() if title_m else re.sub(r'<[^>]+>', '', a_match.group(1)).strip()
|
||||
if et:
|
||||
events.append(et)
|
||||
if not et:
|
||||
continue
|
||||
id_m = re.search(r'data-event-id\s*=\s*"?(\d+)"?', a_tag)
|
||||
events.append(
|
||||
{
|
||||
'title': et,
|
||||
'id': id_m.group(1) if id_m else None,
|
||||
'time': None,
|
||||
}
|
||||
)
|
||||
|
||||
weekday = weekdays[col_idx] if col_idx < len(weekdays) else ''
|
||||
days[day_num] = {'weekday': weekday, 'events': events}
|
||||
days[day_num] = {'weekday': weekday, 'weekday_idx': col_idx, 'events': events}
|
||||
|
||||
return month_text, days
|
||||
|
||||
|
||||
async def _collect_event_times(page) -> dict:
|
||||
"""Read event times straight from the rendered calendar DOM.
|
||||
|
||||
The diary page embeds `div.event-full-info[data-event-full-info-id]`
|
||||
containing `span.data` (e.g. "2026-09-02 16:30:00") for every event, so no
|
||||
AJAX popup clicks are needed. Returns {event_id: 'HH:MM'} for events that
|
||||
have a date; events without one are simply skipped.
|
||||
"""
|
||||
times_by_id: dict[str, str] = {}
|
||||
try:
|
||||
raw_times = await page.evaluate(
|
||||
"""() => {
|
||||
const out = {};
|
||||
for (const div of document.querySelectorAll(
|
||||
'div.event-full-info[data-event-full-info-id]'
|
||||
)) {
|
||||
const id = div.getAttribute('data-event-full-info-id');
|
||||
const date_span = div.querySelector('p.date span.data');
|
||||
if (id && date_span) {
|
||||
out[id] = date_span.textContent.trim();
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}"""
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning(f'Failed to read diary event times from DOM: {e}')
|
||||
return times_by_id
|
||||
|
||||
for event_id, time_text in (raw_times or {}).items():
|
||||
if not time_text:
|
||||
continue
|
||||
m = re.search(r'(\d{1,2}:\d{2})', time_text)
|
||||
if m:
|
||||
times_by_id[event_id] = m.group(1)
|
||||
|
||||
logger.info(f'Diary event times collected for {len(times_by_id)} events')
|
||||
return times_by_id
|
||||
|
||||
|
||||
async def fetch_diary_data(phpsessid: str) -> dict | None:
|
||||
logger.info('Fetching diary data via Playwright...')
|
||||
try:
|
||||
@@ -570,6 +626,16 @@ async def fetch_diary_data(phpsessid: str) -> dict | None:
|
||||
f.write(table_html)
|
||||
|
||||
month_text, days = _parse_calendar_html(table_html)
|
||||
|
||||
# Read event times by opening each event's AJAX popup.
|
||||
times_by_id = await _collect_event_times(page)
|
||||
if times_by_id:
|
||||
for day_data in days.values():
|
||||
for ev in day_data.get('events', []):
|
||||
eid = ev.get('id')
|
||||
if eid and eid in times_by_id:
|
||||
ev['time'] = times_by_id[eid]
|
||||
|
||||
logger.info(
|
||||
f'Diary parsed: month={month_text!r}, days_with_events={sum(1 for d in days.values() if d["events"])}/{len(days)}'
|
||||
)
|
||||
@@ -596,6 +662,25 @@ def _parse_diary_month(text: str) -> str:
|
||||
return text.strip()
|
||||
|
||||
|
||||
def _format_event_line(event, lang: str) -> str:
|
||||
"""Render one diary event line: 📌 title, with time in parens when known.
|
||||
|
||||
'08:00' is the site's placeholder for "no time specified" — show a localized
|
||||
'time_unknown' mark instead. Unknown/absent time renders as before, no parens.
|
||||
"""
|
||||
if isinstance(event, dict):
|
||||
title = escape(str(event.get('title') or ''))
|
||||
tme = event.get('time')
|
||||
else:
|
||||
title = escape(str(event))
|
||||
tme = None
|
||||
if tme == '08:00':
|
||||
return f'📌 {title} ({_tr(lang, "time_unknown")})'
|
||||
if tme:
|
||||
return f'📌 {title} ({escape(str(tme))})'
|
||||
return f'📌 {title}'
|
||||
|
||||
|
||||
def format_diary_day(data: dict, day_num: int, lang: str = 'en') -> str:
|
||||
days = data.get('days', {})
|
||||
month_str = _parse_diary_month(data.get('monthFullText', ''))
|
||||
@@ -605,7 +690,7 @@ def format_diary_day(data: dict, day_num: int, lang: str = 'en') -> str:
|
||||
lines.append(_tr(lang, 'no_events'))
|
||||
else:
|
||||
for e in day_data['events']:
|
||||
lines.append(f'📌 {escape(e)}')
|
||||
lines.append(_format_event_line(e, lang))
|
||||
lines.append(f'\n🔗 {DIARY_URL}')
|
||||
return '\n'.join(lines)
|
||||
|
||||
@@ -614,12 +699,12 @@ def format_diary_week(data: dict, today: datetime, lang: str = 'en') -> str:
|
||||
days = data.get('days', {})
|
||||
_parse_diary_month(data.get('monthFullText', ''))
|
||||
monday = today - timedelta(days=today.weekday())
|
||||
sunday = monday + timedelta(days=6)
|
||||
friday = monday + timedelta(days=4)
|
||||
start = f'{monday.day}.{monday.month}'
|
||||
end = f'{sunday.day}.{sunday.month}'
|
||||
end = f'{friday.day}.{friday.month}'
|
||||
short = DIARY_WEEKDAYS_SHORT.get(lang, DIARY_WEEKDAYS_SHORT['en'])
|
||||
lines = [_tr(lang, 'diary_week_title', start=start, end=end) + '\n']
|
||||
for i in range(7):
|
||||
for i in range(5):
|
||||
d = monday + timedelta(days=i)
|
||||
day_data = days.get(str(d.day))
|
||||
lines.append(f'─ <b>{short[i]} {d.day}.{d.month}</b> ─')
|
||||
@@ -627,7 +712,7 @@ def format_diary_week(data: dict, today: datetime, lang: str = 'en') -> str:
|
||||
lines.append(_tr(lang, 'no_events') + '\n')
|
||||
else:
|
||||
for e in day_data['events']:
|
||||
lines.append(f'📌 {escape(e)}')
|
||||
lines.append(_format_event_line(e, lang))
|
||||
lines.append('')
|
||||
lines.append(f'🔗 {DIARY_URL}')
|
||||
return '\n'.join(lines)
|
||||
@@ -639,6 +724,17 @@ def format_diary_month(data: dict, lang: str = 'en') -> str:
|
||||
lines = [f'📅 <b>{month_str}</b>\n']
|
||||
for day_num in sorted(days.keys(), key=int):
|
||||
day_data = days[day_num]
|
||||
if day_data.get('weekday_idx', 0) >= 5:
|
||||
continue
|
||||
if day_data.get('weekday', '').strip().lower() in (
|
||||
'субота',
|
||||
'суббота',
|
||||
'saturday',
|
||||
'неділя',
|
||||
'воскресенье',
|
||||
'sunday',
|
||||
):
|
||||
continue
|
||||
events = day_data.get('events', [])
|
||||
weekday = day_data.get('weekday', '')
|
||||
lines.append(f'─ <b>{weekday} {day_num}</b> ─')
|
||||
@@ -646,7 +742,7 @@ def format_diary_month(data: dict, lang: str = 'en') -> str:
|
||||
lines.append(_tr(lang, 'no_events') + '\n')
|
||||
else:
|
||||
for e in events:
|
||||
lines.append(f'📌 {escape(e)}')
|
||||
lines.append(_format_event_line(e, lang))
|
||||
lines.append('')
|
||||
lines.append(f'🔗 {DIARY_URL}')
|
||||
return '\n'.join(lines)
|
||||
@@ -676,7 +772,7 @@ def get_schedule_day_keyboard(user_id: int | None = None):
|
||||
tomorrow = today + timedelta(days=1)
|
||||
today_label = _tr(lang, 'today')
|
||||
tomorrow_label = _tr(lang, 'tomorrow')
|
||||
short_keys = ['day_mon', 'day_tue', 'day_wed', 'day_thu', 'day_fri', 'day_sat', 'day_sun']
|
||||
short_keys = ['day_mon', 'day_tue', 'day_wed', 'day_thu', 'day_fri']
|
||||
keyboard = [
|
||||
[
|
||||
InlineKeyboardButton(
|
||||
@@ -706,14 +802,6 @@ def get_schedule_day_keyboard(user_id: int | None = None):
|
||||
InlineKeyboardButton(
|
||||
_tr(lang, short_keys[4]), callback_data=f'schedule_day_{SCHEDULE_WEEKDAYS_CANONICAL[4]}'
|
||||
),
|
||||
InlineKeyboardButton(
|
||||
_tr(lang, short_keys[5]), callback_data=f'schedule_day_{SCHEDULE_WEEKDAYS_CANONICAL[5]}'
|
||||
),
|
||||
],
|
||||
[
|
||||
InlineKeyboardButton(
|
||||
_tr(lang, short_keys[6]), callback_data=f'schedule_day_{SCHEDULE_WEEKDAYS_CANONICAL[6]}'
|
||||
),
|
||||
],
|
||||
]
|
||||
return InlineKeyboardMarkup(keyboard)
|
||||
|
||||
Reference in New Issue
Block a user