moodle-downloader
CLAUDE.md3 starsChanged 4 months agoMITView on GitHub
A Moodle course-material downloader for UNSW (moodle.telt.unsw.edu.au), targeting students downloading materials for courses they are enrolled in. Python 3.11+ (uses tomllib), Rich terminal UI (bilingual zh/en, Chinese default), Selenium-driven Okta SSO login with cookie persistence. See also AGENTS.md (Chinese) for the same conventions. Running the app: python main.py (interactive), python main.py <course_id>, --browser chrome|edge|firefox, --workers N, --lang zh|en, and an undocumented --discover START-END that probes a course-ID range. Post-download study flags (these short-circuit in main_impl() before authentication, since they only…
- Installs packages
In this repository2 other files it gives its agents.
- AGENTS.mdAGENTS.md
- moodle-revisionSkill
The file
Files
CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
What this is
A Moodle course-material downloader for UNSW (moodle.telt.unsw.edu.au), targeting students downloading materials for courses they are enrolled in. Python 3.11+ (uses tomllib), Rich terminal UI (bilingual zh/en, Chinese default), Selenium-driven Okta SSO login with cookie persistence. See also AGENTS.md (Chinese) for the same conventions.
Commands
pip install -r requirements.txt # deps: requests, beautifulsoup4, selenium, rich
python -m pytest tests/ -q # run all tests
python -m pytest tests/test_scanner.py -q # one file
python -m pytest tests/ -k "keyword" -q # one test by name
python -m compileall moodle_scraper/ main.py # syntax checkRunning the app: python main.py (interactive), python main.py <course_id>, --browser chrome|edge|firefox, --workers N, --lang zh|en, and an undocumented --discover START-END that probes a course-ID range.
Post-download study flags (these short-circuit in main_impl() before authentication, since they only read local files): --index [DIR], --obsidian VAULT (+ --overwrite, --copy-files), --notebook PATH (+ --days N). The MCP server runs separately: python -m moodle_scraper.mcp_server --root DIR. PDF text extraction is an optional extra (pip install pypdf) — lazily imported, degrades to empty text when absent, never a hard dependency.
Do not run python main.py from automation. Both main.py and moodle_scraper.__main__.main() block on input() prompts (crash-guard "press Enter to exit", Y/n confirmations) and require a real browser for login. For programmatic checks, call main_impl() or the individual modules directly.
Architecture
Flow: main.py → moodle_scraper.__main__.main_impl() → AuthManager.get_authenticated_client() → Scanner(client) → Downloader(client) → summary.
main.py— thin launcher: crash guard (never lets the console window close silently), manual argv parsing (no argparse), Windows UTF-8 stdout fix. Delegates to the package.moodle_scraper/__main__.py— orchestration only.main_impl(config, ...)returns an exit code (0 ok, 1 error, 2 partial download failures);main()wraps it with the blocking exit prompt.moodle_scraper/http.py— the key seam.HttpClientis a Protocol (get/get_stream);RequestsHttpClientis production,FakeHttpClientis the test double (pre-registered responses with URL prefix matching, plus arequest_log). Scanner and Downloader depend only on this Protocol, never onrequests.moodle_scraper/auth.py—AuthManager: tries cached cookies frommoodle_cookies.jsonfirst, validates them with a probe request, otherwise launches a Selenium browser (auto-detect order chrome → edge → firefox) and polls the URL until the user completes Okta login, then saves cookies and returns an authenticatedRequestsHttpClient.moodle_scraper/scanner.py—Scanner:list_courses()parses the/my/dashboard;fetch_course_name();discover_courses()probes an ID range in parallel;scan_course()is two-stage — directmod/resourcelinks from the course page, then drills intomod/pagesub-pages for nestedpluginfile.phplinks, filtered byfile_keywords/file_extensions(empty filters = accept everything). RaisesScanErrorwhen a response is the login page.moodle_scraper/downloader.py—Downloader.download_all(): skips files that already exist on disk, downloads the rest with aThreadPoolExecutor, writes to.partthenos.replace()for atomicity, returns aDownloadResult.moodle_scraper/config.py—AppConfigdataclass loaded fromconfig.toml(missing file/fields fall back to defaults). A module-level singletonconfig = AppConfig.load()is created at import time; tests should build their ownAppConfiginstead of mutating it.moodle_scraper/i18n.py— zh/en message catalogs and thet(key, **kwargs)lookup;set_language()mutates module-level state, resolved once inmain_impl()from--lang/config.language(CLI wins, unknown values warn and fall back to zh).moodle_scraper/ui.py—RichUI; all user-facing output goes throughui.status(emoji, msg), panels, and progress callbacks — no bareprintin library code.moodle_scraper/utils.py— pure functions:sanitize_filename,extract_extension,is_login_page.moodle_scraper/study/— the post-download layer, operating on files already on disk (never the network, never login).index.pyis its contract:build_index()walks a root treating each subdir as a course, producingCourseIndex/Materialwith heuristickind/weekand optional extracted text, persisted to.moodle-index.json.obsidian.pyexports a vault (per-course MOC + week-bucketed notes with YAML frontmatter and wikilinks);notebook.pyemits a revision.ipynbas raw JSON. Both consumeCourseIndexand, like Scanner/Downloader, return dataclasses rather than printing.planner.pyis the single source of truth for revision ordering and scheduling (KIND_ORDER,revision_sort_key,build_plan→DayPlanwith d-1/d-3/d-7 review slots);notebook.pyandmcp_server.pyboth delegate to it and only render — never re-implement sorting or day-splitting locally (tests/test_mcp_server.pyasserts no local copies exist).moodle_scraper/mcp_server.py— MCP server over stdio (hand-rolled JSON-RPC 2.0, nomcpdependency) exposing 5 tools to AI agents.handle_request(request, index)is a pure function so the protocol is testable without stdio;serve()takes injectablestdin/stdout.skills/moodle-revision/SKILL.md— agent-facing Skill describing the revision workflow over the MCP tools / vault.
The Moodle base URL is hardcoded in scanner.py and auth.py.
Conventions
- Full type annotations on all parameters/returns; every module starts with
from __future__ import annotations. - Custom exceptions per domain (
ScanError,AuthError); worker-level failures are swallowed per-item so one bad page/file never aborts the batch. - Imports grouped stdlib → third-party → local, blank line between groups.
- Tests use
FakeHttpClientthrough theHttpClientProtocol — never hit the network. Follow this seam pattern when adding dependencies. - Never hardcode user-visible text: add a key to both the
_ZHand_ENcatalogs ini18n.pyand callt()at the point of use (emoji prefixes stay in the code, not the catalog).tests/test_i18n.pyenforces key and placeholder parity. Exception:main.pykeeps bilingual static text because it must work before the package imports. Comments stay Chinese.
Repo gotchas
ARTS1630-Japanese_1_(T2_26)/,PHYS1231_Materials/,UNSW_Moodle/are downloaded course PDFs (output data), not source — leave them alone.moodle_cookies.jsoncontains live session cookies. Never commit it or print its contents.启动下载器.batis a Windows double-click launcher menu for end users.assets/demo-*.png(README demos) andassets/social-preview.png(GitHub social card) are real UI renders, generated bypython scripts/gen_assets.py(Richexport_html→ headless-browser screenshot; needs a WebDriver). Regenerate after changing user-facing UI text or layout. The social card template isscripts/social_preview.html.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

