MOOC Utils

Utils like auto assignment solver extension and course notice announcment notification for MOOC courses

Notice reminders API

Introduction#

FastAPI backend for discovering Swayam/NPTEL courses and tracking announcements. Cookie-based JWT auth, CLI and API modes, subscriptions, and notification records.

Project structure#

The repository is a monorepo with a shared core and multiple interfaces. The Notice Reminders API resides under the notice-reminders package and exposes:

  • A FastAPI application with routers for authentication, course search, course details, announcements, subscriptions, and notifications.
  • A core module for configuration, database registration, and authentication helpers.
  • A models layer backed by Tortoise ORM for persistence.
  • A services layer implementing business logic for authentication, course discovery, announcements, and notifications.
  • A CLI entry point supporting interactive scraping without requiring a database.

Core components#

  • FastAPI Application Factory: Creates the app, registers CORS, includes routers, and initializes the database.
  • Configuration: Centralized settings for database URL, platform base URLs, cache TTL, JWT, OTP, and optional integrations (Telegram SMTP).
  • Database Registration: Configures Tortoise ORM connections and models, auto-generates SQLite if missing.
  • Authentication Utilities: Extracts tokens from cookies, verifies JWT, and enforces protected routes.
  • Services Layer: Implements OTP request/verify, JWT creation/verification, refresh token lifecycle, and user management.
  • Data Models: Users, Courses, Announcements, Subscriptions, OTP codes, and refresh tokens.

Key responsibilities:

  • API routing: Exposes endpoints for authentication, course search, course retrieval, announcements, subscriptions, and notifications.
  • Persistence: Uses Tortoise ORM models to persist users, courses, announcements, subscriptions, OTP codes, and refresh tokens.
  • Security: Cookie-based JWT with access and refresh tokens, CSRF-safe defaults.

Architecture overview#

The system follows a layered architecture:

  • Presentation: FastAPI routers expose REST endpoints.
  • Application: Services encapsulate business logic.
  • Persistence: Tortoise ORM models and database.
  • Infrastructure: Configuration, cookies, JWT, and optional external integrations.

Detailed component analysis#

Authentication system (OTP-based login with JWT cookies)#

The authentication system uses:

  • OTP request and verification for email-based login.
  • Access token (short-lived) and refresh token (longer-lived) managed via cookies.
  • Protected routes enforced by extracting and validating the access token from cookies.

Key implementation highlights:

  • Access token creation and verification using HS256 with a secret from settings.
  • Refresh token lifecycle: creation, validation, rotation, and revocation.
  • Cookie policies: HttpOnly, SameSite lax, Secure based on debug mode, path “/”.
  • Logout endpoint revokes refresh tokens and clears cookies.

Database models and schema#

The system persists users, courses, announcements, subscriptions, OTP codes, and refresh tokens. Below is the entity-relationship view:

Notes:

  • Unique constraints: email, telegram_id, course code, and refresh token ensure referential integrity.
  • Foreign keys link announcements to courses and subscriptions to users/courses.

Services layer#

  • AuthService: OTP generation and delivery, user upsert, JWT and refresh token management.
  • Course Discovery: Swayam service integration for course search and metadata retrieval.
  • Announcement Tracking: Fetches announcements per course and stores them.
  • Notification Channels: Channel abstraction for future Telegram/email delivery.
  • Subscription Management: CRUD operations for user-course subscriptions.

API endpoints overview#

  • Authentication
  • POST /auth/request-otp: Initiates OTP delivery.
  • POST /auth/verify-otp: Verifies OTP and sets access/refresh cookies.
  • POST /auth/refresh: Rotates tokens using a valid refresh cookie.
  • POST /auth/logout: Revokes refresh token and clears cookies.
  • GET /auth/me: Returns authenticated user profile.
  • Course Discovery
  • POST /search/courses: Searches courses on supported platforms.
  • GET /courses/{code}: Retrieves course details.
  • Announcements
  • GET /courses/{code}/announcements: Lists announcements for a course.
  • Subscriptions
  • GET /subscriptions: Lists user subscriptions.
  • POST /subscriptions: Creates a subscription.
  • DELETE /subscriptions/{id}: Cancels a subscription.
  • Notifications
  • GET /notifications: Lists user notifications.
  • POST /notifications/deliver: Triggers delivery via configured channels.

Note: Endpoint definitions are implemented in routers under app/api/routers/*.py.

CLI tool capabilities#

The CLI runs independently of the API and does not require a database. It provides:

  • Interactive scraping for course discovery and announcements.
  • Command-line entry via the root main.py with the “cli” subcommand.

Usage:

Dependency analysis#

External libraries and their roles:

  • FastAPI/Uvicorn: Web framework and ASGI server.
  • Tortoise ORM + Aerich: Asynchronous ORM and migrations.
  • Pydantic Settings: Typed settings from environment.
  • httpx, beautifulsoup4: HTTP client and HTML parsing for scraping.
  • PyJWT: JWT encoding/decoding.
  • python-multipart: Form parsing.
  • email-validator: Email validation.

Performance considerations#

  • Caching: Platform base URLs and cache TTL are configurable; consider caching announcements and course metadata to reduce scrape frequency.
  • Database: SQLite is default; for production, use a database engine and enable connection pooling.
  • Token Lifetimes: Short-lived access tokens minimize exposure; refresh tokens are long-lived but rotated securely.
  • Scraping Efficiency: Batch requests, respect robots.txt, and throttle to avoid rate limits.

Troubleshooting guide#

Common issues and resolutions:

  • Missing JWT Secret: Ensure jwt_secret is set in environment; otherwise, token operations will fail.
  • Database Initialization: On first run with SQLite, the database file and schema are generated automatically if the path is valid.
  • CORS Errors: Verify cors_origins matches the frontend origin.
  • OTP Delivery: Configure OTP delivery method and credentials; console mode prints codes for local testing.
  • Token Validation Failures: Confirm cookie presence and expiration; use refresh endpoint to obtain new tokens.

Operational checks:

  • Environment variables: Load via .env using pydantic-settings.
  • Database connectivity: Confirm database_url is reachable.
  • Cookie Security: Insecure environments (debug=true) set non-Secure cookies; production should disable debug.

Conclusion#

One backend, two entrypoints. Configure secrets, run migrations, and keep scrapers polite.

Appendices#

Setup instructions#

  • Prerequisites: Python 3.12+.
  • Install dependencies: uv sync.
  • Initialize database: First run creates SQLite file and schema automatically.
  • Run API: uv run python main.py api [–host HOST] [–port PORT] [–reload].
  • Run CLI: uv run python main.py cli.

Configuration options#

Environment variables loaded via pydantic-settings (.env file):

  • app_name, debug, database_url
  • swayam_base_url, nptel_base_url
  • cache_ttl_minutes
  • telegram_bot_token, smtp_host, smtp_port, smtp_user, smtp_password, smtp_from
  • cors_origins
  • jwt_secret, jwt_access_token_expire_minutes, jwt_refresh_token_expire_days
  • otp_expire_minutes, otp_delivery, otp_length

Usage examples#

  • API Mode:
  • Start server: uv run python main.py api --reload
  • Authenticate:
  • POST /auth/request-otp with email
  • POST /auth/verify-otp with email and OTP
  • Use cookies for subsequent requests
  • CLI Mode:
  • uv run python main.py cli
  • Interactively search and view course announcements