Implementation Notes
GET /route - List all routes grouped by region (HKI, KLN, NT)
GET /route/{region}/{route} - Get route details including directions (route_seq) and route_id
GET /stop-route/{stop_id} - Get stop names (name_en, name_tc) and the routes serving that stop
GET /eta/stop/{stop_id} - Get real-time ETA for all routes at that stop
Additionally, static route data is sourced from:
https://hkbus.github.io/hk-bus-crawling/routeFareList.min.json - Contains mapping from route identifiers to stop ID sequences for GMB (and other operators).
Script: gmb_arrival.py
Key functions:
searchRoutes(route): Queries /route, finds which regions contain this route number. If none, suggests similar route numbers.
get_gmb_arrival(route, direction, stop_name, region):
- Fetch
/route/{region}/{route} to obtain route_id and the direction details (origin/destination names).
- Construct composite route key using the origin/destination English names and load from
routeFareList.json to get the ordered list of stop IDs for that direction.
- For each stop ID along the route, fetch
/stop-route/{stop_id} to retrieve stop names (cached).
- Match the user-provided
stop_name (case-insensitive) exactly; if not found, perform fuzzy matching and return suggestions.
- Once stop ID is identified, call
/eta/stop/{stop_id}.
- Filter ETA entries for the desired
route_id and route_seq (direction), extract up to 3 next arrival timestamps, format as "HH:MM HKT".
- Return JSON:
{ "stopId": "...", "stopName": "...", "arrivals": [ "17:35 HKT", ... ] }.
Caching Strategy
routes_all.json: All route list (1 hour)
route_details.json: Route details per region/route (5 minutes)
routeFareList.json: Static route-to-stop mapping (1 day)
stop_names.json: Stop ID to names mapping (1 week)
- ETA responses: 30 seconds
Cache files stored in data/ subdirectory.
Error Handling
- Network errors and API failures are caught and reported in JSON with an
error field.
- If route not found: returns
found: false with suggestions array.
- If stop name not found: returns
error with suggestions mapping suggestion → stop ID.
- If no active ETA: returns empty
arrivals array with an informative message.
Usage Example (Command Line)
# Search route
python3 gmb_arrival.py searchRoutes 1
# => {"route":"1","found":true,"regions":["HKI","KLN","NT"]}
# Get arrival for route 1 direction 1 (The Peak → Central) at "Hong Kong Station Minibus Terminus" in HKI
python3 gmb_arrival.py getGMBArrival 1 1 "Hong Kong Station Minibus Terminus" HKI
# => {"stopId":"20014492","stopName":"Hong Kong Station Minibus Terminus","arrivals":["14:38 HKT","14:45 HKT","14:52 HKT"]}
Notes
- Direction sequence: For each GMB route, the API defines
route_seq 1 and 2. Use searchRoutes then inspect route details to determine which sequence corresponds to your desired direction, or use getGMBArrival directly if you know the direction number.
- Stop names are matched case-insensitively. Chinese or English names both work.
- The static
routeFareList may lag behind official data by up to one day but is generally reliable.
- Rate limits: The script caches aggressively to minimize API calls; still, avoid excessive polling (ETA updates every minute).
1---2name: hk-gmb-arrival3description: Real-time arrival information for Hong Kong Green Mini Buses (GMB). Supports fuzzy stop name matching and multi-region route lookup.4---5# Implementation Notes67## API Endpoints (Base URL: https://data.etagmb.gov.hk)89- `GET /route` - List all routes grouped by region (HKI, KLN, NT)10- `GET /route/{region}/{route}` - Get route details including directions (route_seq) and route_id11- `GET /stop-route/{stop_id}` - Get stop names (name_en, name_tc) and the routes serving that stop12- `GET /eta/stop/{stop_id}` - Get real-time ETA for all routes at that stop1314Additionally, static route data is sourced from:15- `https://hkbus.github.io/hk-bus-crawling/routeFareList.min.json` - Contains mapping from route identifiers to stop ID sequences for GMB (and other operators).1617## Script: gmb_arrival.py1819Key functions:20- `searchRoutes(route)`: Queries `/route`, finds which regions contain this route number. If none, suggests similar route numbers.21- `get_gmb_arrival(route, direction, stop_name, region)`:22 1. Fetch `/route/{region}/{route}` to obtain route_id and the direction details (origin/destination names).23 2. Construct composite route key using the origin/destination English names and load from `routeFareList.json` to get the ordered list of stop IDs for that direction.24 3. For each stop ID along the route, fetch `/stop-route/{stop_id}` to retrieve stop names (cached).25 4. Match the user-provided `stop_name` (case-insensitive) exactly; if not found, perform fuzzy matching and return suggestions.26 5. Once stop ID is identified, call `/eta/stop/{stop_id}`.27 6. Filter ETA entries for the desired `route_id` and `route_seq` (direction), extract up to 3 next arrival timestamps, format as "HH:MM HKT".28 7. Return JSON: `{ "stopId": "...", "stopName": "...", "arrivals": [ "17:35 HKT", ... ] }`.2930## Caching Strategy3132- `routes_all.json`: All route list (1 hour)33- `route_details.json`: Route details per region/route (5 minutes)34- `routeFareList.json`: Static route-to-stop mapping (1 day)35- `stop_names.json`: Stop ID to names mapping (1 week)36- ETA responses: 30 seconds3738Cache files stored in `data/` subdirectory.3940## Error Handling4142- Network errors and API failures are caught and reported in JSON with an `error` field.43- If route not found: returns `found: false` with `suggestions` array.44- If stop name not found: returns `error` with `suggestions` mapping suggestion → stop ID.45- If no active ETA: returns empty `arrivals` array with an informative `message`.4647## Usage Example (Command Line)4849```bash50# Search route51python3 gmb_arrival.py searchRoutes 152# => {"route":"1","found":true,"regions":["HKI","KLN","NT"]}5354# Get arrival for route 1 direction 1 (The Peak → Central) at "Hong Kong Station Minibus Terminus" in HKI55python3 gmb_arrival.py getGMBArrival 1 1 "Hong Kong Station Minibus Terminus" HKI56# => {"stopId":"20014492","stopName":"Hong Kong Station Minibus Terminus","arrivals":["14:38 HKT","14:45 HKT","14:52 HKT"]}57```5859## Notes6061- Direction sequence: For each GMB route, the API defines `route_seq` 1 and 2. Use `searchRoutes` then inspect route details to determine which sequence corresponds to your desired direction, or use `getGMBArrival` directly if you know the direction number.62- Stop names are matched case-insensitively. Chinese or English names both work.63- The static `routeFareList` may lag behind official data by up to one day but is generally reliable.64- Rate limits: The script caches aggressively to minimize API calls; still, avoid excessive polling (ETA updates every minute).