fix(edu_master): satisfy ruff in schedule scraper
- rename ambiguous loop var, merge nested if (E741, SIM102) - use tempfile.gettempdir() for debug dump (S108) - drop unused total_lessons assignment (F841)
This commit is contained in:
@@ -3,6 +3,7 @@ import json
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
import tempfile
|
||||||
import time
|
import time
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
@@ -545,7 +546,7 @@ def _parse_schedule_cell(cell_html: str) -> list:
|
|||||||
text = text.strip()
|
text = text.strip()
|
||||||
if not text:
|
if not text:
|
||||||
continue
|
continue
|
||||||
lines = [l.strip() for l in text.split('\n') if l.strip()]
|
lines = [ln.strip() for ln in text.split('\n') if ln.strip()]
|
||||||
if not lines:
|
if not lines:
|
||||||
continue
|
continue
|
||||||
subject = lines[0] if lines else ''
|
subject = lines[0] if lines else ''
|
||||||
@@ -599,9 +600,10 @@ def _parse_schedule_html(table_html: str) -> dict:
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
first_cell_text = re.sub(r'<[^>]+>', '', cells[0]).strip()
|
first_cell_text = re.sub(r'<[^>]+>', '', cells[0]).strip()
|
||||||
if not re.match(r'^\d+\.', first_cell_text):
|
if not re.match(r'^\d+\.', first_cell_text) and (
|
||||||
if 'Позакласне' in first_cell_text or re.search(r'colspan\s*=\s*"?(\d+)"?', row):
|
'Позакласне' in first_cell_text or re.search(r'colspan\s*=\s*"?(\d+)"?', row)
|
||||||
continue
|
):
|
||||||
|
continue
|
||||||
|
|
||||||
lesson_num_match = re.match(r'^(\d+)\.', first_cell_text)
|
lesson_num_match = re.match(r'^(\d+)\.', first_cell_text)
|
||||||
lesson_num = lesson_num_match.group(1) if lesson_num_match else ''
|
lesson_num = lesson_num_match.group(1) if lesson_num_match else ''
|
||||||
@@ -647,11 +649,11 @@ async def fetch_schedule_data(phpsessid: str) -> dict | None:
|
|||||||
logger.error('table.schedule-table not found in DOM')
|
logger.error('table.schedule-table not found in DOM')
|
||||||
return None
|
return None
|
||||||
|
|
||||||
with contextlib.suppress(Exception), open('/tmp/schedule_debug.html', 'w', encoding='utf-8') as f:
|
debug_path = os.path.join(tempfile.gettempdir(), 'schedule_debug.html')
|
||||||
|
with contextlib.suppress(Exception), open(debug_path, 'w', encoding='utf-8') as f:
|
||||||
f.write(table_html)
|
f.write(table_html)
|
||||||
|
|
||||||
data = _parse_schedule_html(table_html)
|
data = _parse_schedule_html(table_html)
|
||||||
total_lessons = sum(len(entry['classes'].get(c, [])) for day in data['weekdays'].values() for entry in day for c in data['classes'])
|
|
||||||
logger.info(f'Schedule parsed: {len(data["weekdays"])} days, classes={data["classes"]}')
|
logger.info(f'Schedule parsed: {len(data["weekdays"])} days, classes={data["classes"]}')
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|||||||
Reference in New Issue
Block a user