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 / data-ingestion

Data ingestion

Data ingestion is the set of scraper systems that turns official NUS academic records into stable data for NUSMods. The systems collect module and teaching data for the main academic experience and a separate course list for the CPE-X feature. They clean and join source records before publishing read-only data for the website, search, and other downstream consumers.

System purpose

Source normalization

The ingestion system provides a boundary between changing NUS services and NUSMods consumers. It converts source records into stable product data so clients can work with module identity, academic details, semester activity, and CPE-X availability without knowing the source formats.

The system treats academic data as a published snapshot for an academic year. This supports repeatable reads by the website while allowing a new scrape to absorb changes in the source systems.

References: scrapers/README.md• L1-13 scrapers/…/README.md• L1-5 scrapers/…/config.ts• L7-20

Scraper boundaries

Two scraper systems serve different product needs. The NUS module data scraper builds the main module, timetable, exam, venue, and prerequisite data set. The CPE-X data scraper builds the smaller list of modules marked for the Course Planning Exercise.

The systems publish separate contracts because general module activity and CPE-X participation come from different source views and serve different website flows. The main scraper publishes year and semester resources. The CPE-X scraper publishes one current module list and keeps dated copies for review.

References: scrapers/…/DataPipeline.ts• L39-134 scrapers/…/scraper.ts• L57-67 scrapers/…/scraper.ts• L342-357

Ingestion flow

Main academic data flow

The main flow builds a complete academic-year view from organization records, module catalog records, semester teaching records, and exam records. It first establishes the faculty and department scope. It then combines stable module details with semester-specific activity before producing module and venue views.

The diagram shows how the main source groups become data that downstream consumers can read.

flowchart TD
    A[Faculty and department records] --> B[Academic scope]
    C[Module catalog records] --> D[Module and semester records]
    B --> D
    E[Timetable records] --> D
    F[Exam records] --> D
    D --> G[Year-wide module data]
    D --> H[Venue data]
    G --> I[Static API and search]
    H --> I

The flow handles the four supported semester values: the two main semesters and the two Special Terms. During an academic-year change, Special Term data can use the prior academic year so that the published year keeps a complete and coherent view.

References: scrapers/…/DataPipeline.ts• L39-122 scrapers/…/modules.ts• L87-92 scrapers/…/GetSemesterData.ts• L317-410

CPE-X data flow

The CPE-X flow reads active academic groups and their course records. It keeps only records with a recognized MPE attribute, where MPE is the NUS course field that marks participation in the module planning exercise. The result records the module code, title, credit value, and applicable CPE-X semester.

The flow is independent from the main publication flow, but both systems use official NUS course information. See the CPE-X data scraper page for its source rules and output contract.

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

Published data

Main resource views

The main scraper publishes several views of the same academic snapshot. Small views support search and lists. Full module views support module pages and planning. Venue views reverse the timetable relationship so a consumer can ask which classes use a location at a given time.

The main resource groups are:

Resource viewDownstream purpose
Module listFind module codes, titles, and offered semesters
Module informationSearch and display summary module details
Full module recordDisplay module details, requisites, timetables, and exams
Venue dataShow classes and occupied times by venue
Faculty and department mapGroup modules in the academic structure

The file data also feeds the public read-only API. In production, module search data is sent to Elasticsearch so search can use a dedicated index without changing the public module resource shape.

References: scrapers/…/fs.ts• L64-116 scrapers/…/fs.ts• L152-186 scrapers/…/index.ts• L21-30 scrapers/…/elastic.ts• L22-23

CPE-X resource view

The CPE-X scraper publishes cpexModules.json as a compact input for the website form. The website uses the module identity and credit fields for each choice. It uses the semester flags to show only modules available in the active exercise semester.

The scraper also writes a time-stamped archive. It replaces the current file only when the result reaches its safety threshold, so a small result from an incomplete source run does not silently remove the last usable list.

References: scrapers/…/scraper.ts• L41-47 scrapers/…/scraper.ts• L324-357 website/…/nusmods.js• L80-86 website/…/ModuleForm.tsx• L46-56

Data quality

Record joining

The main scraper joins records by module code. It keeps module catalog details separate from semester activity because a module can exist in the catalog without being offered in a semester. It then combines semester records into one year-wide module record and retains the timetable and exam details for offered semesters.

The timetable stage validates source lesson fields before mapping them to published lessons. It groups repeated lesson rows by their activity, class, time, and venue, then turns lesson dates into normal week lists or date ranges. A module remains marked as offered when the source shows a lesson even if that lesson cannot be published because its details are invalid.

The join also supports dual-coded modules. When matching lessons share a title, the system records aliases so a consumer can treat equivalent module codes as related without changing their published identities.

References: scrapers/…/GetSemesterData.ts• L335-375 scrapers/…/GetSemesterTimetable.ts• L194-264 scrapers/…/validation.ts• L13-79 scrapers/…/CollateModules.ts• L98-165 scrapers/…/CollateVenues.ts• L109-149

Timetable compatibility

Timetable data preserves the meaning of NUS day, activity, class, and teaching-week fields while converting them to the stable shape used by downstream readers. It accepts valid Sunday lessons and lessons without a room. It reports duplicate dates and irregular week spacing instead of silently treating them as a normal weekly pattern.

The main scraper can also restore the legacy lesson order for the current academic year. This compatibility layer keeps existing user timetables stable while the data format changes. Lessons without a legacy match remain available at the end of the list.

References: scrapers/…/validation.ts• L13-35 scrapers/…/GetSemesterTimetable.ts• L66-108 scrapers/…/GetSemesterTimetable.ts• L123-146 scrapers/…/GetSemesterTimetable.ts• L242-264 scrapers/…/data.ts• L257-323 scrapers/…/GetSemesterTimetable.test.ts• L52-86 scrapers/…/GetSemesterTimetable.test.ts• L1726-1740

Source safeguards

The scrapers prefer a usable previous result over an unsafe replacement. The main scraper uses request retries and expiring caches for selected source data. It removes modules that no longer appear in the current catalog. The CPE-X scraper skips incomplete or unknown records and protects the current file with a minimum result count.

These safeguards have different effects. A main-pipeline failure can block a run or leave a partial stage unavailable. A CPE-X source problem can leave its current list unchanged while still preserving an archive of the new result for inspection.

References: scrapers/…/api.ts• L93-127 scrapers/…/DataPipeline.ts• L124-132 scrapers/…/scraper.ts• L246-281 scrapers/…/scraper.ts• L342-357

Operational model

Scheduled execution

Production runs build the scraper and then start the data job. The main scraper runs its complete academic pipeline. The CPE-X scraper runs as a separate process with its own settings and output. Process restarts provide the repeat cycle because the upstream services can fail without a predictable schedule.

The two jobs therefore have separate release and failure boundaries. A problem in CPE-X publication does not change the main module data set. A problem in the main pipeline does not change the last valid CPE-X list.

The main scraper package exposes distinct build and scrape commands. The dev command builds the package before starting the generated entrypoint, while the scrape command starts the existing build output. The package also provides separate lint, type, and test checks plus one check command that runs them together.

References: scrapers/…/run.sh• L1-29 scrapers/…/ecosystem.config.js• L3-21 scrapers/…/package.json• L8-20 scrapers/…/run.sh• L1-23 scrapers/…/ecosystem.config.js• L3-20

Downstream consumers

The website reads the published resources according to the product flow. Module search uses condensed data. Module pages and timetable planning use full module and semester data. Venue views use location-grouped timetable data. The CPE-X form reads its separate module list and applies the semester flags.

The serverless and external APIs page describes the wider delivery boundary around these published resources. The key relationship is that the scrapers own data freshness and shape while consumers own presentation and user actions. A change to a published field can therefore affect more than one feature even when the source change occurs in only one scraper.

References: scrapers/…/fs.ts• L97-120 website/…/nusmods.js• L80-86 website/…/ModuleForm.tsx• L46-56 website/…/ModulePageContent.tsx• L220-287

Change checklist

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

  • Check the main scraper's full module view and its condensed search view when a module field changes.
  • Check both module timetable data and venue data when lesson fields or semester rules change.
  • Keep the academic year and Special Term source year aligned with the output year used by downstream readers.
  • Preserve the lesson validation rules and the rule that an invalid lesson can still show that its module is offered.
  • Check timetable week mapping and legacy ordering when changing lesson identity, day, class, venue, or date fields.
  • Preserve the CPE-X field names and semester flags when changing its source mapping or output.
  • Review the CPE-X safety threshold and archive behavior when the expected course count changes.
  • Check file output and Elasticsearch indexing together when changing the main scraper's published search data.
  • Check the website reader when changing either scraper's output path or resource name.
  • Keep the main scraper's build and scrape commands aligned when changing its production entrypoint or compiled output.

Important failure modes

TriggerConsequenceGuard
Main source APIs return incomplete or stale organization dataModule scope or faculty and department grouping can be wrongUse the organization cache and review the run logs before accepting the snapshot
A module catalog revision is not resolved before semester data is joinedOlder module details can replace current detailsSelect the newest catalog revision before the semester join
Special Term data uses the wrong academic yearThe published year can contain missing or mismatched Special Term activityApply the academic-calendar year rule during the full pipeline
A timetable or exam source has no matching module recordSome activity cannot appear in the normal module viewLog unmatched records and inspect source changes before changing the join rule
A source lesson has an invalid day, date, week count, or time rangeThe lesson is excluded from the timetable while the module may still appear as offeredValidate lessons and inspect invalid-lesson counts and errors
Duplicate or irregular lesson dates are treated as a normal weekly patternTimetable week display can be wrong or unstableDeduplicate dates and retain date ranges when the academic calendar does not fit normal weeks
A legacy timetable key no longer matches a published lessonExisting user timetables can change order during migrationMatch on the full lesson identity and append unmatched lessons without dropping them
The CPE-X source sends an unknown MPE valueA participating module is left out of the CPE-X listLog the value and map it only after its semester meaning is confirmed
A CPE-X run returns fewer modules than the safety thresholdThe current CPE-X list remains unchangedInspect the archived result and source health before lowering the threshold
Main file output and search indexing use different dataSearch and module pages can show different resultsTreat file publication and Elasticsearch publication as one contract
The main scraper runs before its production build completesThe job can use missing or stale generated outputBuild the package before starting the scrape command

Key files

FileWhy to read it
scrapers/README.mdStates the purpose and scope of the scraper systems
scrapers/…/package.jsonDefines the main scraper build, scrape, and validation lifecycle
scrapers/…/DataPipeline.tsShows the main academic-year flow and Special Term handling
scrapers/…/GetSemesterData.tsShows how module, timetable, and exam records form semester data
scrapers/…/GetSemesterTimetable.tsShows how source lessons become stable timetable records
scrapers/…/validation.tsDefines the input checks that protect published lessons and exams
scrapers/…/CollateModules.tsShows how semester data becomes year-wide module resources
scrapers/…/CollateVenues.tsShows how timetable data becomes venue resources and aliases
scrapers/…/data.tsDefines shared data cleanup, alias handling, and timetable compatibility rules
scrapers/…/fs.tsDefines the main published data views and their paths
scrapers/…/elastic.tsDefines the separate search publication boundary
scrapers/…/scraper.tsDefines the CPE-X source selection and publication contract
scrapers/…/index.tsShows the CPE-X academic year and safety threshold
scrapers/…/ecosystem.config.jsShows the CPE-X production restart cycle
website/…/nusmods.jsShows how the website locates published scraper data
website/…/ModuleForm.tsxShows how the website consumes CPE-X semester flags