h5p-auto / parser.py
cnb.cYcEnOSHAGA
feat: 集成 cloudcampus_h5p 核心引擎、云控后台、QR 码服务端生成、英文登录界面修复
4dfeffa
Raw History Blame Contribute Delete
1.48 kB
import re
import logging
from typing import List
from dataclasses import dataclass, field
from bs4 import BeautifulSoup, Tag
from .config import COMPLETED_PATTERN, EMBED_URL_BASE
logger = logging.getLogger(__name__)
ID_RE = re.compile(r"id=(\d+)")
@dataclass
class Activity:
cm_id: int
name: str
completed: bool = False
def parse_grade_report(html: str) -> List[Activity]:
soup = BeautifulSoup(html, "html.parser")
# Try specific selectors first, fall back to generic
rows: List[Tag] = soup.select("table.generaltable tr")
if not rows:
rows = soup.select("table tr")
activities: List[Activity] = []
seen_ids: set = set()
for row in rows:
link = row.select_one('a[href*="mod/h5pactivity"]')
if not link:
continue
href = link.get("href", "")
id_match = ID_RE.search(str(href))
if not id_match:
continue
cm_id = int(id_match.group(1))
if cm_id in seen_ids:
continue
seen_ids.add(cm_id)
name = link.get_text(strip=True)[:80]
# Detect completion: scan all td cells for a numeric score pattern
completed = False
for td in row.select("td"):
txt = td.get_text(strip=True)
if COMPLETED_PATTERN.match(txt):
completed = True
break
activities.append(Activity(cm_id=cm_id, name=name, completed=completed))
return activities