moodle-scraper
Mirror the course files of your enrolled Moodle courses on
learn.haw-kiel.de into a local folder tree, with a per-course
manifest.json that makes repeat runs incremental and resumable.
It is a personal, read-only backup tool: it downloads the files you already have access to, and never touches grades, submissions, forums, quizzes, or messages.
Built for HAW Kiel's Moodle instance, but any Moodle base URL works (MOODLE_BASE_URL).
What it does
- Discovers your courses (dashboard where available, or directly by course id/URL).
- Walks each course's sections and activities and resolves the file links behind
mod_resource,mod_folder,mod_url,mod_page,mod_bookandmod_imscp. - Mirrors those files under
data/<shortname>-<id>/<NN>-<section>/<activity>/.... - Records size /
Last-Modified/ ETag / optional SHA-1 inmanifest.json. - On later runs it skips unchanged files, resumes interrupted
.partdownloads, and can prune files that were removed upstream.
What it does not do
Forums, grades, assignment submissions, quizzes, calendar, messages, streaming video/HLS capture, HTML→Markdown conversion, scheduling, or any upload/POST — all out of scope. It also does not automate SSO login.
Legal and ethical use
- Only use this for content you are already enrolled in, as a personal backup. Do not redistribute any downloaded material.
- The tool only issues
GET/HEADrequests. It never posts, submits, or grades anything. - Defaults are deliberately polite: sequential requests, a
0.5sdelay, one download at a time, exponential backoff on429/5xx, and an identifyingUser-Agent. Please keep the delay unless you have a reason not to. - Your
MoodleSessioncookie is a secret. It is read from.env/environment/a cookie file, never logged, and never written into a manifest..envanddata/are git-ignored.
Requirements
- Python 3.11+
- A browser where you are logged in to your Moodle instance.
Install
From PyPI:
pip install moodle-scraper
From source:
git clone https://github.com/RTXC01/moodle_scrape.git moodle_scrape
cd moodle_scrape
python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install -e ".[dev]"
Then copy the example config and fill in your cookie:
Copy-Item .env.example .env
Cookie export
The tool reuses the browser session you already have; it never asks for your password and does not
drive SSO. Export the MoodleSession cookie and put it in .env as MOODLE_COOKIE (or save a
Netscape cookies.txt and point MOODLE_COOKIE_FILE at it).
Chrome / Edge
-
Log in at https://learn.haw-kiel.de.
-
Open DevTools (
F12) → Application → Storage → Cookies →https://learn.haw-kiel.de. -
Copy the Value of the
MoodleSessioncookie. -
Put it in
.env:MOODLE_COOKIE=MoodleSession=<paste-value>
A bare value also works (
MOODLE_COOKIE=<paste-value>), as does a full header (MOODLE_COOKIE=Cookie: MoodleSession=...).
Firefox
- Log in at https://learn.haw-kiel.de.
- DevTools (
F12) → Storage → Cookies →https://learn.haw-kiel.de→ copy theMoodleSessionValue. - Paste it into
.envas above. Alternatively, use a "cookies.txt" export extension, save the Netscape file, and setMOODLE_COOKIE_FILE=./cookies.txt.
The Moodle session times out after ~8 hours (
sessiontimeout: 28800). When it expires, commands exit with code3and tell you to re-export the cookie.
Commands
moodle-scrape doctor
moodle-scrape courses
moodle-scrape files <course>
moodle-scrape sync [--course ID]... [options]
doctor
Validates the cookie, prints the logged-in account and how many courses are visible.
Exit code 0 when the session is good, 3 when it is expired or invalid.
moodle-scrape doctor
courses
Prints a table of enrolled courses (id, shortname, fullname, discovered file count).
moodle-scrape courses
files
Dry discovery for one course: lists the files that would be downloaded, without downloading.
moodle-scrape files 12345
moodle-scrape files https://learn.haw-kiel.de/course/view.php?id=12345
sync
The main command.
--course ID|URL Repeatable. Defaults to all enrolled courses.
--out DIR Output directory (default: data).
--delay SECONDS Delay between requests (default: 0.5).
--timeout SECONDS Per-request timeout (default: 30; raise it for slow servers).
--concurrency N Parallel downloads (default: 1 = sequential).
--dry-run Resolve and report everything, download nothing.
--prune Delete local files that no longer exist upstream.
--checksum Compute SHA-1 while downloading (slower).
--max-file-size N Skip files larger than N bytes (default: unlimited).
--insecure Disable TLS verification (only behind a broken campus proxy).
--verbose Verbose logging; the cookie is always redacted.
Examples:
# Everything, politely
moodle-scrape sync
# One course, preview only
moodle-scrape sync --course 12345 --dry-run
# Two courses, checksums, prune removed files
moodle-scrape sync --course 12345 --course 67890 --checksum --prune
Slow dashboard? Some Moodle instances answer very slowly under load. The tool retries transient login bounces and transport errors automatically; for this site a timeout of
MOODLE_TIMEOUT=180in.envis a good idea. Single-course runs fetch the course page directly and skip the dashboard entirely.
Output layout
data/
Analysis-12345/ # <shortname>-<id>
00-General/
Slides.pdf
Scripts/
chapter1.pdf # nested folder structure preserved
01-Week 1/
Exercises/
Notes.txt
_external/ # recorded links, not downloaded
manifest.json
Physics-67890/
...
manifest.json
Filenames and directories are sanitised for Windows (<>:"/\|?*, control characters, trailing
dots/spaces, reserved device names such as CON/PRN, over-long components, and path-length
limits). If two different URLs would map to the same path, a short deterministic hash
(__<8 hex chars>) is inserted before the extension.
Manifest format
One manifest.json per course, written atomically after the course finishes (and rewritten after
partial failures so a rerun resumes instead of restarting):
{
"schema_version": 1,
"course": { "id": 12345, "shortname": "Analysis", "fullname": "Analysis I" },
"generated_at": "2026-09-15T19:30:00+00:00",
"files": [
{
"rel_path": "Analysis-12345/00-General/Slides.pdf",
"url": "https://learn.haw-kiel.de/pluginfile.php/1/mod_resource/content/0/Slides.pdf",
"section": "General",
"activity": "",
"filename": "Slides.pdf",
"size": 1048576,
"last_modified": "Wed, 03 Sep 2026 10:00:00 GMT",
"etag": "\"abc123\"",
"sha1": null,
"downloaded": true,
"external": false,
"status": "downloaded",
"fetched_at": "2026-09-15T19:31:00+00:00"
}
]
}
status is one of pending, downloaded, skipped, missing, failed, external,
unsupported. external: true entries are links pointing outside the Moodle host; they are
recorded, never fetched.
Exit codes
| Code | Meaning |
|---|---|
0 |
Success |
1 |
Unexpected error |
2 |
Bad usage or configuration (no cookie, bad values, missing cookie file) |
3 |
Session expired or invalid |
Incremental behaviour
- If the manifest entry for a URL matches the local file's size (and, when available,
Last-Modified/ETag via a conditional request), the file is skipped. - Otherwise the file is downloaded to
<name>.part, flushed, then atomically renamed. If the run is interrupted, the.partfile stays and the next run resumes it with aRangerequest when the server supports it. 404is recorded asmissingand the run continues.401/403triggers a session check and aborts the run.
Development
pip install -e ".[dev]"
ruff check .
ruff format --check .
mypy src
pytest -q
Tests are fully offline: HTML fixtures under tests/fixtures/ plus httpx.MockTransport cover
discovery, path sanitisation, manifest diffing/pruning, cookie parsing, session-expiry detection
and retry/backoff behaviour.
Releasing (maintainers)
- Bump
__version__insrc/moodle_scraper/__init__.py. - Commit and tag:
git tag v0.1.0 && git push origin v0.1.0— the release workflow builds the sdist/wheel, runstwine check, and publishes to PyPI via trusted publishing (configure the PyPI project once: ownerRTXC01, repositorymoodle_scrape, workflowrelease.yml, environmentpypi). - Manual alternative:
python -m build && twine upload dist/*.
Troubleshooting
- Exit code 3 / "session expired" — log in again in your browser and re-export the cookie.
- A course page returns no files — Moodle markup varies by version/theme. Run
moodle-scrape files <id> --verboseand check whether the activity selectors indiscovery.pymatch. All selectors live in that one module. - A single activity is skipped — some activities bounce to login even with a valid session
(insufficient rights); they are skipped individually instead of aborting the run. Run with
--verboseto see which. - Files look larger than expected / slow — use
--max-file-sizeto skip big video files, and keep--concurrency 1. - TLS errors behind a campus proxy —
--insecuredisables verification for that run only.
Roadmap
browser_cookie3browser-store extraction as an optional extra.- Optionally record
mod_page/mod_booktext content in the manifest (today only their attachments are collected).
License
Metadata
Release files for moodle-scraper 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| moodle_scraper-0.1.0.tar.gz | 40.5 kB | With provenance | Details for moodle_scraper-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| moodle_scraper-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for moodle_scraper-0.1.0-py3-none-any.whl |
Total release size: 74.7 kB
Release files / moodle_scraper-0.1.0.tar.gz
| Download URL | moodle_scraper-0.1.0.tar.gz |
|---|---|
| Size | 40.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d2cb4ba90056468ccb98bf5c01594f310ff73b73c39c3f1d4ea3c8a78f7c4314
|
|
BLAKE2b-256 checksum How to use checksums |
c664a6db0a77d291678c44061581acbaf0256342d2657aa09b08e000cb4edf03
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 15, 2026.
Transparency logRelease files / moodle_scraper-0.1.0-py3-none-any.whl
| Download URL | moodle_scraper-0.1.0-py3-none-any.whl |
|---|---|
| Size | 34.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4a67a6b29113adab3c08303f89329a66a28263932b5c4b2876e315b70b937897
|
|
BLAKE2b-256 checksum How to use checksums |
36ef67cfad3bef18fc22f0b784cfad5b0bd3aab1ea46efd7d06494a15fbd3e3c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 15, 2026.
Transparency logSecurity for moodle-scraper 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for moodle_scraper-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 15, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.