NUS module data scraper
The NUS module data scraper turns official NUS academic data into the read-only JSON data used by NUSMods. It gathers module, timetable, exam, faculty, department, and venue data for an academic year. It then cleans and joins that data before publishing module and venue resources for the website and other API users. See the parent page Data ingestion for the wider ingestion system and the sibling page CPE-X data scraper for the other scraper.
System purpose
Academic data service
The scraper provides one stable data service between changing NUS source systems and downstream NUSMods clients. Its main goal is to make academic data useful for search, module pages, timetable planning, venue views, and prerequisite checks without making clients understand the source API formats.
The service publishes data as static JSON. The public API is read-only and is designed for daily use rather than live queries. This keeps client access simple while allowing the scraper to absorb source changes during each data run.
References: scrapers/…/README.md• L1-5 scrapers/…/swagger.yaml• L1-19
Entry point and execution
Command entrypoint
The scrapers/…/index.ts entrypoint exposes the scraper as a set of commands. Each command runs one part of the data job, while all runs the complete flow for an academic year.
The commands support focused work as well as a full run:
testchecks access to the source APIs.departmentsrefreshes faculty and department data.semestercollects data for one semester.venuebuilds venue data from collected semester data.combinejoins all semester data into year-wide module data.allruns the complete pipeline.migrateconverts older module data to the v2 shape when needed.
The package scripts provide the normal ways to build, run, serve generated data, and publish the API description. The scrape script runs the built entrypoint. The dev script builds it before running it. The check script runs linting, type checks, and tests as one local quality check. The scraper requires Node.js 22 or later.
References: scrapers/…/index.ts• L4-19 scrapers/…/index.ts• L53-155 scrapers/…/package.json• L7-20 scrapers/…/package.json• L69-71
Scheduled run
The production process starts scrapers/…/run.sh. The script builds the scraper, runs the full all pipeline, and copies the API description into the generated data area. PM2 restarts this process after a one-hour delay. This restart pattern acts as the schedule because source API responses can be unreliable and a run can stop early.
The normal production sequence is:
- Build the scraper.
- Fetch and transform the academic data.
- Write the generated data and API description.
- Wait for the process manager to start the next run.
References: scrapers/…/run.sh• L1-29 scrapers/…/ecosystem.config.js• L3-21
Configuration
Source access settings
The scraper reads its runtime settings from scrapers/…/config.ts, with the expected secret and URL fields shown in scrapers/…/env.example.json. The settings identify the source API, provide the credentials for the course, timetable, and academic-organisation services, and limit the number of concurrent requests.
The academic year selects the main data set. A separate special-term year can be supplied when Special Term data still belongs to the previous academic year. The scraper keeps this choice explicit so that timetable data is not mixed with the wrong year during an academic-year change.
References: scrapers/…/config.ts• L5-32 scrapers/…/config.ts• L34-62 scrapers/…/env.example.json• L1-10
Data destinations
The scraper always creates the file data that serves the static API. In production it also sends module search data to Elasticsearch. In local work, file output is enough, so the same pipeline can run without a search service.
Temporary source responses use expiring caches. A successful source request refreshes its cache. If a source request fails, the scraper can use an unexpired cached response for selected data such as exams and organisation lists. This reduces the chance that a short source outage removes useful data from a run.
References: scrapers/…/BaseTask.ts• L9-39 scrapers/…/fs.ts• L26-60 scrapers/…/api.ts• L93-127
Data acquisition services
NUS source APIs
The NUS API service is the shared gateway for all source requests. It adds the required source credentials, limits global request concurrency, converts source errors into scraper errors, and hides the different response styles used by the course, timetable, and academic-organisation services.
Module data is requested by faculty for the academic year when the source supports it. The scraper can fall back to requests for all four semesters and removes duplicate catalog revisions. Timetable data is read for a whole semester, while exam data is read for all modules in that semester.
References: scrapers/…/nus-api.ts• L27-87 scrapers/…/nus-api.ts• L117-149 scrapers/…/nus-api.ts• L236-337
Supporting data services
Timetable enrichment uses shared venue locations and campus zone data. These sources let the scraper attach a zone to each lesson so clients can use location-aware timetable data. The source data is fetched once for a run and reused across lessons.
The logging service records progress and data faults. Production logs are kept per run, and error events are sent to Sentry. This gives operators a way to distinguish a bad source record from a failed full run.
References: scrapers/…/GetSemesterTimetable.ts• L6-7 scrapers/…/getVenueLocations.ts• L7-14 scrapers/…/getCovidZones.ts• L6-19 scrapers/…/index.ts• L24-49
Data pipeline
Pipeline stages
The full pipeline first creates the organisation maps and the year-wide module catalog. It then collects timetable and exam data for each semester. Venue data is built from those lessons, and the final step combines all semester records into year-wide module resources.
The diagram shows how the source records move through the pipeline and become public resources.
flowchart TD
A[Faculty and department data] --> C[Semester module data]
B[Year-wide module catalog] --> C
T[Semester timetables] --> C
E[Semester exams] --> C
C --> V[Venue resources]
C --> M[Combined module resources]
V --> P[Static JSON API]
M --> P
M --> S[Module search index]
The four semesters are semester 1, semester 2, Special Term 1, and Special Term 2. During an academic-year transition, special-term records can come from the prior year while the published resources remain under the current year.
References: scrapers/…/DataPipeline.ts• L21-35 scrapers/…/DataPipeline.ts• L39-122 scrapers/…/modules.ts• L87-92
Module transformation
Module records are changed from source names and codes into the NUSMods module shape. The transformation adds readable faculty and department names, removes empty source values, cleans text, and turns workload text into five weekly hour values when the source format is clear. Values that cannot be understood remain available as text instead of being discarded.
The scraper keeps module information separate from semester activity. A module can exist in the catalog without being offered in a given semester. Offered semesters receive timetable and exam data, while the year-wide module record keeps the full set of semester records.
References: scrapers/…/GetSemesterData.ts• L153-213 scrapers/…/GetSemesterData.ts• L223-281 scrapers/…/GetSemesterData.ts• L317-410
Timetable and exam transformation
Timetable records are grouped by module and merged when the same lesson occurs on several dates. Normal teaching weeks become sorted week numbers. Lessons outside the normal academic calendar become a date range with an interval or a list of week positions.
The scraper validates lesson and exam records before it publishes them. Invalid records are left out of lesson or exam data, but a module remains marked as offered when the source contains a lesson for it. Valid lessons can include Sunday sessions and lessons without a room. Repeated lesson dates are reduced to one date and logged as a source-data fault.
Timetable order can also remain compatible with existing client timetables. For the academic year with a saved legacy order, known lessons keep that order and new lessons follow after them. Other academic years keep their generated order.
Exam data becomes an ISO date and an exam duration in minutes. A semester can have no exam fields when NUS has not published exam data yet.
References: scrapers/…/GetSemesterTimetable.ts• L62-109 scrapers/…/GetSemesterTimetable.ts• L190-287 scrapers/…/validation.ts• L13-79 scrapers/…/validation.test.ts• L58-117 scrapers/…/GetSemesterTimetable.test.ts• L52-86 scrapers/…/GetSemesterTimetable.test.ts• L1726-1740 scrapers/…/data.ts• L257-323 scrapers/…/GetSemesterExams.ts• L17-28 scrapers/…/GetSemesterExams.ts• L93-107
Venue transformation
Venue data is the timetable viewed from the classroom rather than from the module. The scraper groups lessons by venue and day, keeps the class details, and marks the time slots that are occupied. It also detects dual-coded modules that share a title and timetable, then records their aliases for module consumers.
The venue list contains only locations used by lessons in that semester. It is not a full campus location list.
References: scrapers/…/CollateVenues.ts• L15-48 scrapers/…/CollateVenues.ts• L59-66 scrapers/…/CollateVenues.ts• L91-151 scrapers/…/data.ts• L118-189
Requisite enrichment
Prerequisite text is converted into a tree of module requirements when the text can be parsed. The tree expresses groups such as all-of, any-of, and a required number from a set. The scraper also adds the reverse relation: a module records which other modules can use it to meet a requirement.
This enrichment lets clients show prerequisites and find the later modules that depend on a selected module without parsing source text themselves.
References: scrapers/…/index.ts• L11-15 scrapers/…/index.ts• L27-47 scrapers/…/index.ts• L51-97
Published API
Read-only resources
The API is a set of static JSON resources under https://api.nusmods.com/v2. A path includes an academic year, and semester resources also include a semester number. Semester values 1 and 2 represent the main semesters. Values 3 and 4 represent the two Special Terms.
The main resources are:
| Resource | Purpose |
|---|---|
| moduleList.json | Small module code and title list with offered semesters for search and validation |
| moduleInfo.json | Module details and summary semester data for detailed search |
| modules/{moduleCode}.json | Full data for one module, including timetable and prerequisite data |
| semesters/…/venues.json | Venue names used in one semester |
| semesters/…/venueInformation.json | Venue classes and occupied time slots for one semester |
All successful responses are JSON. The resources are read-only and do not require API authentication for public access.
References: scrapers/…/swagger.yaml• L3-19 scrapers/…/swagger.yaml• L226-236 scrapers/…/swagger.yaml• L328-359
Consumer data views
The API provides different views of the same academic data so each client can load only what it needs. Search uses condensed module records. A module page uses one full module record. Venue views use the reverse timetable grouped by location.
Module records expose academic identity, descriptions, credits, workload, faculty and department, requisites, aliases, and semester activity. Semester activity can include timetable lessons, exam details, and the zones derived from lesson venues. Workload can be a five-number tuple or source text when parsing is not safe.
References: scrapers/…/swagger.yaml• L234-324 scrapers/…/swagger.yaml• L328-389 scrapers/…/modules.ts• L109-170 scrapers/…/CollateModules.ts• L168-225
Change checklist
When these files or sections are changed, remember to consider these:
- When source API fields or term rules change, check the source gateway, semester mapping, validation, and the public schemas together.
- When module or semester data fields change, update the full module view and the condensed search view so they do not drift apart.
- When timetable lesson shape, validation rules, week mapping, or ordering changes, check both module timetable output and reverse venue output. Preserve the legacy ordering contract until client migration ends.
- When academic-year or Special Term rules change, check the year used for source fetches, output paths, and module merge precedence.
- When persistence fields change, check both file output and the Elasticsearch search view. Elasticsearch only stores the module search view.
- When the public resource names or shapes change, update swagger.yaml and the generated API documentation copied by the production run.
- When package run scripts, quality checks, or the supported Node version change, check the scheduled job and local run commands together.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Required source URL or API key is missing | The scraper cannot start | Configuration checks required source settings before creating the runtime config |
| A source request fails and its cache is missing or expired | That task fails instead of publishing an untrusted partial result | Request retries and expiring cache fallback cover transient failures where cached data exists |
| The source returns duplicate module catalog revisions | A stale title or prerequisite set could become the module record | Module records are deduplicated by catalog version and effective date before semester joins |
| A timetable lesson has invalid dates, times, or type data | The bad lesson is excluded; a module may still be marked as offered if the source listed it | Lesson validation filters invalid records and logs the fault |
| Timetable dates repeat for one lesson | The lesson can have an incorrect week range if duplicates are treated as separate events | Week mapping removes duplicate dates and logs the condition |
| A timetable order changes for the current client year | Existing user timetables may no longer match saved lesson positions | The scraper applies the saved legacy order for the configured academic year |
| A semester has no exam data because exams are not published yet | That semester has no exam fields, but the module and timetable can still publish | Missing exam data is treated as an expected empty result |
| A module disappears from the source catalog | Old module files could remain visible to clients | The full run compares existing output with the current catalog and removes inactive modules |
| Elasticsearch is unavailable or not configured | File API data can still be produced locally, but module search indexing is unavailable | Production requires the search destination; local runs fall back to file persistence |
Key files
| File | Why to read it |
|---|---|
| scrapers/…/index.ts | Defines the operator commands and their order |
| scrapers/…/package.json | Defines the supported Node version and the build, scrape, local run, data-serving, documentation, and quality-check entrypoints |
| scrapers/…/DataPipeline.ts | Shows the full academic-year data flow and Special Term handling |
| scrapers/…/nus-api.ts | Describes the boundary between the scraper and NUS source APIs |
| scrapers/…/GetSemesterData.ts | Shows how module, timetable, and exam records become one semester view |
| scrapers/…/GetSemesterTimetable.ts | Shows validation, week mapping, lesson ordering, and timetable output |
| scrapers/…/validation.ts | Defines the source-data checks that protect published lesson and exam data |
| scrapers/…/CollateModules.ts | Shows how semester views become public module resources |
| scrapers/…/CollateVenues.ts | Shows how module lessons become venue resources and aliases |
| scrapers/…/data.ts | Defines shared lesson cleanup, module merging, and legacy timetable ordering |
| scrapers/…/fs.ts | Defines the static JSON resource layout |
| scrapers/…/swagger.yaml | Defines the published API resources and their data contracts |
| scrapers/…/run.sh and scrapers/…/ecosystem.config.js | Define the production run and restart schedule |