Introducing a new way to review your PRs.

Read the announcement

nusmodifications / nusmods

🏫 Official course planning platform for National University of Singapore.

View on GitHub

20 documents · Updated Nov 6, 2026

docs / cpex-data-scraper

CPE-X data scraper

The CPE-X data scraper builds the module list used by the Course Planning Exercise (CPE-X) feature. It reads course data from NUS services, keeps modules marked for CPE-X, and publishes a current JSON file plus a time-stamped archive. The data ingestion page gives the wider view of how scraped data reaches NUSMods; the NUS module data scraper covers the main module data pipeline.

Scraper role

The scraper supplies the separate CPE-X module list that the website uses when it builds a student’s choice list. Each output record identifies a module, its title, its credit value, and the semester in which it is available for CPE-X (scrapers/…/scraper.ts• L41-47; website/…/nusmods.js• L80-86).

The list is separate from the general module data flow because CPE-X availability comes from the official course attribute named MPE. The website then filters this list for the active CPE-X semester before it shows modules to a student (website/…/ModuleForm.tsx• L46-56).

Execution entrypoint

The execution entrypoint is scrapers/…/index.ts. It selects academic year 2026/27, loads the scraper service settings from scrapers/…/env.json, and starts one scrape with a safety threshold of 1,500 modules (scrapers/…/index.ts• L1-19). The threshold prevents a small or incomplete result from replacing the current file.

The package exposes the same entrypoint through the scrape script after TypeScript has been built. The dev script builds first, while build, typecheck, lint, and test support local checks (scrapers/…/package.json• L9-17).

Scraping workflow

The scrape turns faculty-scoped NUS course records into one normalized CPE-X list. It first obtains active academic groups, then reads each group’s course pages, selects records with a recognized CPE-X attribute, and writes the result.

The following flow shows how the source records become data for the website.

flowchart TD
    A[Load service settings and academic year] --> B[Fetch academic groups]
    B --> C[Keep active groups]
    C --> D[Fetch course pages for each group]
    D --> E[Select records with MPE attribute]
    E --> F[Normalize and merge module records]
    F --> G{At least 1500 modules?}
    G -->|Yes| H[Write cpexModules.json]
    G -->|No| I[Keep current file unchanged]
    H --> J[Write time-stamped archive]
    I --> J
    J --> K[Website reads CPE-X module list]

Source discovery

The scraper calls the academic-group service with the academic API key and app key. It adds the non-faculty group when NUS does not return that group, then queries the course service for each active group. Course requests carry the selected academic year and use pages of up to 1,000 records (scrapers/…/scraper.ts• L153-229).

A missing group result does not stop the full run when the course service returns 404. Other course request errors are logged for that group and the scraper continues to the next group. A failure in the academic-group request stops the run because the scraper has no source scope to process (scrapers/…/scraper.ts• L166-187; scrapers/…/scraper.ts• L232-244).

References: scrapers/…/scraper.ts• L153-229 scrapers/…/scraper.ts• L232-244

Record selection

The scraper accepts a course only when it has the fields needed for a usable module record and an MPE course attribute. Recognized attribute values mark the module for semester 1, semester 2, or both. Unknown values are logged and excluded so that an unfamiliar source value does not create a false availability claim (scrapers/…/scraper.ts• L246-281).

The output uses a normalized module code and title. It also turns a missing credit value into 0. Records with the same normalized module code are merged so that semester flags accumulate into one module entry. Conflicting titles or credit values are logged for review (scrapers/…/scraper.ts• L105-129; scrapers/…/scraper.ts• L284-313).

References: scrapers/…/scraper.ts• L85-92 scrapers/…/scraper.ts• L246-313

Output publication

The final list is sorted by module code before publication. The scraper always writes a time-stamped copy under the output directory’s old folder. It writes cpexModules.json only when the result reaches the configured threshold, which protects the live data from a partial scrape (scrapers/…/scraper.ts• L324-357).

The website reads cpexModules.json through its CPE-X module-list URL. The semester flags in each record are therefore the contract between the scraper output and the CPE-X form (website/…/nusmods.js• L80-86; website/…/mpe.ts• L17-23).

References: scrapers/…/scraper.ts• L324-370 website/…/nusmods.js• L80-86 website/…/mpe.ts• L17-23

Configuration and scheduling

The scraper needs one base service URL and three credentials: separate academic API and app keys for academic-group discovery, plus a course API key for module retrieval. The entrypoint reads these values as a ScraperEnv object from env.json; the file is a deployment input and must provide valid service access without placing credentials in source control (scrapers/…/scraper.ts• L9-14; scrapers/…/index.ts• L12-18).

The scheduled run is managed by PM2 through scrapers/…/ecosystem.config.js. PM2 starts scripts/run.sh as one process and waits one hour before restarting it. The restart interval is used because the upstream API can fail in an unpredictable way (scrapers/…/ecosystem.config.js• L3-20).

Run script

The run script sets the project directory, removes the old build, compiles the TypeScript source, and runs the built entrypoint. The generated data is already connected to the live data path through a symlink, so the script does not copy the files after the scrape (scrapers/…/run.sh• L1-23).

References: scrapers/…/ecosystem.config.js• L3-20 scrapers/…/run.sh• L1-23 scrapers/…/index.ts• L6-19

Data contract

The published module list is a compact read-only input for the CPE-X form. The website uses the module code, title, and credit value for each choice, then checks the semester-specific flag to decide whether the module is selectable in the current exercise (website/…/mpe.ts• L17-23; website/…/ModuleForm.tsx• L46-56).

The scraper’s result also includes run counts for raw rows, incomplete rows, unknown attribute values, and merged duplicates. These counts support a quick check of source health without changing the published module shape (scrapers/…/scraper.ts• L57-67; scrapers/…/scraper.ts• L328-369).

References: scrapers/…/scraper.ts• L41-47 scrapers/…/scraper.ts• L57-67 website/…/mpe.ts• L17-23 website/…/ModuleForm.tsx• L46-56

Change checklist

When these files or sections are changed, remember to consider these:

  • Align the academic year in src/index.ts with the exercise period and the source records expected by the website.
  • Preserve the cpexModules.json field names and semester flags because the CPE-X form filters directly on those fields.
  • Review the minimum-module threshold when the upstream course count changes so a valid smaller result is not blocked or a partial result is published.
  • Keep the output directory and symlink arrangement aligned with the data path read by the website.
  • Update the recognized MPE values when NUS changes its course attribute format. Treat new values as unavailable until their semester meaning is known.

Important failure modes

TriggerConsequenceGuard
Academic-group service returns a non-success responseThe scrape stops before it can build a module list.Check the service response and credentials before the scheduled run.
Course service returns 404 for a groupThat group contributes no modules, but other groups continue.Review the run summary and archive when a group unexpectedly has no data.
A course has incomplete fieldsThe row is skipped and the final list becomes smaller.Monitor the skipped-row count and source schema changes.
NUS sends an unknown MPE valueThe module is excluded from both semester lists.Review the logged value and map it only after confirming its meaning.
Final module count is below the thresholdThe archive is updated, but the current cpexModules.json remains unchanged.Inspect the archive and source response before lowering the threshold.
Duplicate module records disagree on title or creditOne record is published and the conflict is only logged.Review conflict warnings and confirm which source value should win.

Key files

FileWhy to read it
scrapers/…/index.tsShows the production academic year, safety threshold, settings input, and execution start.
scrapers/…/scraper.tsDefines the source-to-output workflow and the published CPE-X record shape.
scrapers/…/run.shShows the build and run sequence used by scheduled execution.
scrapers/…/ecosystem.config.jsShows the PM2 process and hourly restart policy.
scrapers/…/package.jsonLists the local build, run, validation, and test commands.
website/…/nusmods.jsDefines the website URL for the published CPE-X module list.
website/…/mpe.tsDefines the module fields that the website expects from the scraper output.
website/…/ModuleForm.tsxShows how the website uses semester flags to build the selectable list.