Download xapi.py from Rclemon/h5p-auto: direct link, hf CLI and curl.
- Browser
- Download file 3.53 kB
-
https://huggingface.co/spaces/Rclemon/h5p-auto/resolve/main/xapi.py
- Command line
-
hf download hf://spaces/Rclemon/h5p-auto/xapi.py
-
curl -L -o xapi.py https://huggingface.co/spaces/Rclemon/h5p-auto/resolve/main/xapi.py
3.53 kB
| import json | |
| import re | |
| import time | |
| import logging | |
| from typing import List, Dict, Optional | |
| from dataclasses import dataclass | |
| import requests | |
| from .config import ( | |
| BASE_URL, | |
| XAPI_SERVICE_URL, | |
| XAPI_METHOD, | |
| XAPI_COMPONENT, | |
| EMBED_URL_BASE, | |
| REQUEST_TIMEOUT, | |
| MAX_RETRIES, | |
| ) | |
| logger = logging.getLogger(__name__) | |
| H5P_INTEGRATION_RE = re.compile(r"H5PIntegration\s*=\s*(\{[\s\S]*?\});") | |
| class XAPIResult: | |
| ok: bool | |
| count: int = 0 | |
| error: str = "" | |
| def build_actor(user_id: str = "", user_name: str = "") -> dict: | |
| return { | |
| "objectType": "Agent", | |
| "account": { | |
| "name": str(user_id) if user_id else "0", | |
| "homePage": BASE_URL, | |
| }, | |
| } | |
| def build_statement(content: dict, integration: dict) -> dict: | |
| integ_user = integration.get("user", {}) | |
| actor = build_actor( | |
| user_id=integ_user.get("id", ""), | |
| user_name=integ_user.get("name", ""), | |
| ) | |
| return { | |
| "actor": actor, | |
| "verb": { | |
| "id": "http://adlnet.gov/expapi/verbs/answered", | |
| "display": {"en-US": "answered"}, | |
| }, | |
| "object": { | |
| "id": content.get("url", ""), | |
| "objectType": "Activity", | |
| "definition": { | |
| "name": {"en-US": content.get("name", "")}, | |
| "type": "http://adlnet.gov/expapi/activities/cmi.interaction", | |
| "interactionType": "compound", | |
| }, | |
| }, | |
| "result": { | |
| "score": {"min": 0, "max": 1, "raw": 1, "scaled": 1}, | |
| "completion": True, | |
| "success": True, | |
| }, | |
| } | |
| class XAPIClient: | |
| def __init__(self, session: requests.Session, sesskey: str): | |
| self._session = session | |
| self._sesskey = sesskey | |
| def sesskey(self) -> str: | |
| return self._sesskey | |
| def post_statements(self, statements: List[dict]) -> XAPIResult: | |
| if not self._sesskey: | |
| return XAPIResult(ok=False, error="No sesskey") | |
| url = f"{XAPI_SERVICE_URL}?sesskey={self._sesskey}&info={XAPI_METHOD}" | |
| payload = [ | |
| { | |
| "methodname": XAPI_METHOD, | |
| "args": { | |
| "component": XAPI_COMPONENT, | |
| "requestjson": json.dumps(statements), | |
| }, | |
| } | |
| ] | |
| last_err = "" | |
| for attempt in range(1, MAX_RETRIES + 1): | |
| try: | |
| resp = self._session.post( | |
| url, | |
| json=payload, | |
| timeout=REQUEST_TIMEOUT, | |
| ) | |
| result = resp.json() | |
| if resp.ok and result and not result[0].get("error"): | |
| data = result[0].get("data", []) | |
| all_ok = isinstance(data, list) and all(v is True for v in data) | |
| return XAPIResult(ok=all_ok, count=len(data)) | |
| err = ( | |
| result[0].get("exception", {}).get("message", "API error") | |
| if result | |
| else "empty response" | |
| ) | |
| last_err = err | |
| logger.warning("xAPI POST attempt %d/%d: %s", attempt, MAX_RETRIES, err) | |
| except requests.RequestException as e: | |
| last_err = str(e) | |
| logger.warning("xAPI POST attempt %d/%d: %s", attempt, MAX_RETRIES, e) | |
| if attempt < MAX_RETRIES: | |
| time.sleep(2**attempt) | |
| return XAPIResult(ok=False, error=last_err) | |