refactor: restructure package and rename CLI to ravin

This commit is contained in:
Meghdad committed 2026-08-04 17:09:02 +03:30
1 parent af78a66366
commit 2580d86984
26 files changed
+2923 -2118

No files matched your search

+66 -33
View File
@@ -1,6 +1,6 @@
# Ravin Moodle Downloader
A dependency-free command-line client that lists your enrolled Moodle courses and downloads accessible course files. It defaults to Ravin Academy, but accepts another Moodle base URL through `--site`.
A lightweight command-line client that lists your enrolled Moodle courses, downloads accessible course files, and builds a private learning library. It defaults to Ravin Academy, but accepts another Moodle base URL through `--site`.
The client first tries Moodle's official mobile web-service API and falls back to a normal web login plus Moodle's authenticated AJAX interface. For sites protected by Cloudflare, it can establish and refresh an authenticated browser session automatically.
@@ -12,50 +12,50 @@ The client first tries Moodle's official mobile web-service API and falls back t
- Python 3.10 or newer
- An active enrollment on the target Moodle site
The downloader itself uses only Python's standard library. Automatic browser login uses the optional Selenium package with an installed Zen, Firefox, Chrome, Chromium, or Brave browser.
The downloader core uses Python's standard library. Selenium is installed automatically because browser authentication is the normal path for Cloudflare-protected LMS sites. You also need an installed Zen, Firefox, Chrome, Chromium, or Brave browser.
## Installation
After cloning or downloading the repository, install the command from its root directory:
```bash
python3 -m pip install '.[browser]'
python3 -m pip install .
```
You can then use `ravin-downloader` from any shell. Running `python3 ravin_downloader.py` directly from the repository remains supported.
You can then use `ravin` from any shell. The equivalent module entry point is `python3 -m ravin`.
## Quick start
```bash
ravin-downloader login
ravin-downloader courses
ravin-downloader files COURSE_ID
ravin-downloader download COURSE_ID
ravin-downloader library
ravin-downloader serve-library --open
ravin login
ravin courses
ravin files COURSE_ID
ravin download COURSE_ID
ravin library
ravin serve-library --open
```
Files are saved under `downloads/<course-id>/...`. Existing completed files are skipped; interrupted downloads use a temporary `.part` suffix and resume automatically with HTTP byte-range requests. Use `--overwrite` if you want to replace existing files, or `--retries` to change the default five retries per file.
Files are saved directly in the private static library under `library/courses/<course-id>/content/`. Every activity uses the sortable `SECTION_NUMBER--ACTIVITY_POSITION--ACTIVITY_ID` directory format, while its real LMS title and metadata live in `item.json`. Original filenames are preserved inside each activity's `files/` directory. Existing completed files are skipped; interrupted downloads use a temporary `.part` suffix and resume automatically with HTTP byte-range requests.
For scripting, use JSON output:
```bash
ravin-downloader --json courses
ravin-downloader --json files COURSE_ID
ravin --json courses
ravin --json files COURSE_ID
```
## Configuration
On first use, missing values are requested interactively and saved in `.env` in the current directory. Copy [`.env.example`](.env.example) if you prefer to configure it manually. The file is Git-ignored and written with owner-only permissions.
`ravin-downloader login` opens an installed browser using the Git-ignored `.ravin-browser-profile/` directory. For Ravin Academy it starts at `https://lms.ravinacademy.com/`, signs in there, follows the available Moodle course-launch link, and captures the resulting session on `training.ravinacademy.com`. If `.env` already contains `RAVIN_USERNAME` and `RAVIN_PASSWORD`, a compatible login form is filled automatically. Once login succeeds, the browser closes and `.env` is updated with the exact User-Agent and cookies. Use `--browser-executable /path/to/browser` to override automatic detection.
`ravin login` opens an installed browser using the Git-ignored `.ravin-browser-profile/` directory. For Ravin Academy it starts at `https://lms.ravinacademy.com/`, signs in there, follows the available Moodle course-launch link, and captures the resulting session on `training.ravinacademy.com`. If `.env` already contains `RAVIN_USERNAME` and `RAVIN_PASSWORD`, a compatible login form is filled automatically. Once login succeeds, the browser closes and `.env` is updated with the exact User-Agent and cookies. Use `--browser-executable /path/to/browser` to override automatic detection.
Never commit or share `.env`. It may contain both account credentials and a temporary authenticated browser session.
If the academy disables its mobile token service, the fallback is automatic. You can force that path for troubleshooting:
```bash
ravin-downloader --web-only --username YOUR_USERNAME courses
ravin --web-only --username YOUR_USERNAME courses
```
## Browser login and Cloudflare
@@ -63,8 +63,8 @@ ravin-downloader --web-only --username YOUR_USERNAME courses
Ravin Academy protects its account portal and Moodle with a browser check. Set up the browser helper once:
```bash
python3 -m pip install '.[browser]'
ravin-downloader login
python3 -m pip install .
ravin login
```
After authentication, `.env` can contain these values:
@@ -80,8 +80,8 @@ RAVIN_COOKIE="the complete browser Cookie header"
Later commands reuse `.env`, so you can simply run:
```bash
ravin-downloader files 44
ravin-downloader download 44
ravin files 44
ravin download 44
```
## Static course library
@@ -89,25 +89,25 @@ ravin-downloader download 44
Build a clean local website from your enrolled courses and downloaded files:
```bash
ravin-downloader library
ravin-downloader serve-library --open
ravin library
ravin serve-library --open
```
The first command refreshes `library/courses.json` from the LMS and prepares `library/` as a self-contained web root. The catalog preserves the LMS chapter order and includes section IDs and summaries, every activity and its type, lesson descriptions, LMS completion state, original filenames, MIME types, sizes, extensions, local download status, and safe LMS activity links. Large course files are not copied: `library/media` is a relative symlink that points only to `downloads/`.
The first command refreshes `library/courses.json` from the LMS and prepares `library/` as a self-contained web root. The catalog preserves the LMS chapter order and includes section IDs and summaries, every activity and its type, lesson descriptions, LMS completion state, original filenames, MIME types, sizes, extensions, local availability, learning artifacts, and safe LMS activity links. Course files already live inside this web root, so no project-level directories or symlinks are exposed.
The second command serves only the prepared web root at `http://localhost:8765/`. The library includes course search, true chapter grouping, offline progress, resource filters, efficient seekable video playback, document links, online LMS activities, dark mode, and completion checkboxes saved in your browser. Run `ravin-downloader library` again after downloading new files to refresh the catalog.
The second command serves only the prepared web root at `http://localhost:8765/`. The library includes course search, true chapter grouping, offline progress, resource filters, efficient seekable video playback, document and exam links, transcript and summary readers, Markdown exam questions, online LMS activities, dark mode, and completion checkboxes saved in your browser.
To build only selected courses or choose other directories:
```bash
ravin-downloader library 44 --output library --downloads downloads
ravin-downloader serve-library --site-dir library --downloads downloads --port 8765
ravin library 44 --output library
ravin serve-library --site-dir library --port 8765
```
If the catalog data is already current and you only need to rebuild the pages, symlink, or Nginx configuration, no LMS connection is required:
If the catalog data is already current and you only need to rescan local artifacts or rebuild the pages and Nginx configuration, no LMS connection is required:
```bash
ravin-downloader library --reuse-catalog
ravin library --reuse-catalog
```
The generated directory contains:
@@ -120,7 +120,14 @@ library/
├── styles.css
├── courses.json
├── nginx-server.conf
└── media -> ../downloads
└── courses/
└── COURSE_ID/
├── course.json
└── content/
└── SECTION--POSITION--ACTIVITY/
├── item.json
├── files/
└── artifacts/
```
For Nginx, include the generated server block inside the `http {}` section of your main configuration:
@@ -136,31 +143,57 @@ nginx -t
brew services restart nginx
```
Open `http://localhost:8765/`. Nginx now uses `library/` as its complete document root. The repository, `.env`, browser profile, source code, and Git metadata are outside that root and cannot be requested through the site. Only the explicit `media` symlink exposes `downloads/`.
Open `http://localhost:8765/`. Nginx uses `library/` as its complete document root. The repository, `.env`, browser profile, source code, and Git metadata remain outside that root and cannot be requested through the site.
The generated `library/` directory is Git-ignored because its JSON contains details about your enrollments. No passwords, cookies, session tokens, or remote protected-file URLs are written to the catalog.
### Migrating the legacy download layout
Repositories created before version 0.4 can migrate their existing files without downloading them again:
```bash
ravin migrate-library --source downloads --library library
```
The migration uses the existing LMS catalog to place resources and exams in course order, moves transcripts and summaries into `artifacts/`, converts machine-specific metadata paths to relative paths, and writes a per-course `migration.json` journal.
The saved session is checked on every run. When it expires or Cloudflare rejects it, the authentication browser opens automatically and replaces the saved values. You can force that refresh yourself with:
```bash
ravin-downloader --refresh-session courses
ravin --refresh-session courses
```
If browser automation is unavailable, manual header entry remains available as a fallback:
```bash
ravin-downloader --manual-session --refresh-session courses
ravin --manual-session --refresh-session courses
```
Never send `.env`, `.ravin-browser-profile/`, your password, or its Cookie value to another person; they grant access to your LMS account or signed-in session.
## Development
The project uses a standard `src/` package layout:
```text
src/ravin/
├── cli.py # command parsing and orchestration
├── auth.py # environment and browser authentication
├── client.py # Moodle API/web client and downloads
├── parsers.py # Moodle HTML parsers
├── paths.py # portable content paths and metadata
├── catalog.py # static catalog generation
├── migration.py # legacy layout migration
├── server.py # local range-aware HTTP server
├── models.py # shared data objects and errors
└── assets/ # packaged static library UI
```
Run the test suite without installing additional packages:
```bash
python3 -m unittest -v
python3 -m py_compile ravin_downloader.py
python3 -m unittest discover -s tests -v
python3 -m compileall -q src tests
```
See [CONTRIBUTING.md](CONTRIBUTING.md) for contribution guidance and [SECURITY.md](SECURITY.md) for reporting security issues.