gtfs 4.13.4 → 4.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3350,31 +3350,34 @@ function padLeadingZeros(time) {
3350
3350
 
3351
3351
  // src/lib/import.ts
3352
3352
  var downloadFiles = async (task) => {
3353
- task.log(`Downloading GTFS from ${task.agency_url}`);
3353
+ task.log(`Downloading GTFS from ${task.url}`);
3354
3354
  task.path = `${task.downloadDir}/gtfs.zip`;
3355
- const response = await fetch(task.agency_url, {
3355
+ const response = await fetch(task.url, {
3356
3356
  method: "GET",
3357
3357
  headers: task.headers || {},
3358
3358
  signal: task.downloadTimeout ? AbortSignal.timeout(task.downloadTimeout) : void 0
3359
3359
  });
3360
3360
  if (response.status !== 200) {
3361
- throw new Error(`Unable to download GTFS from ${task.agency_url}`);
3361
+ throw new Error(`Unable to download GTFS from ${task.url}`);
3362
3362
  }
3363
3363
  const buffer = await response.arrayBuffer();
3364
3364
  await writeFile(task.path, Buffer.from(buffer));
3365
3365
  task.log("Download successful");
3366
3366
  };
3367
- var downloadGtfsRealtimeData = async (url, task) => {
3368
- const response = await fetch(url, {
3367
+ var downloadGtfsRealtimeData = async (urlAndHeaders, task) => {
3368
+ task.log(`Downloading GTFS-Realtime from ${urlAndHeaders.url}`);
3369
+ const response = await fetch(urlAndHeaders.url, {
3369
3370
  method: "GET",
3370
3371
  headers: {
3371
- ...task.realtime_headers || {},
3372
+ ...urlAndHeaders.headers ?? {},
3372
3373
  "Accept-Encoding": "gzip"
3373
3374
  },
3374
3375
  signal: task.downloadTimeout ? AbortSignal.timeout(task.downloadTimeout) : void 0
3375
3376
  });
3376
3377
  if (response.status !== 200) {
3377
- task.logWarning(`Unable to download GTFS-Realtime from ${url}`);
3378
+ task.logWarning(
3379
+ `Unable to download GTFS-Realtime from ${urlAndHeaders.url}`
3380
+ );
3378
3381
  return null;
3379
3382
  }
3380
3383
  const buffer = await response.arrayBuffer();
@@ -3434,48 +3437,70 @@ var prepareRealtimeValue = (entity, column, task) => {
3434
3437
  );
3435
3438
  };
3436
3439
  var updateRealtimeData = async (task) => {
3437
- if (!task.realtime_urls) {
3440
+ if (task.realtimeAlerts === void 0 && task.realtimeTripUpdates === void 0 && task.realtimeVehiclePositions === void 0) {
3438
3441
  return;
3439
3442
  }
3440
3443
  const db = openDb({
3441
3444
  sqlitePath: task.sqlitePath
3442
3445
  });
3443
- task.log(
3444
- `Starting GTFS-Realtime import from ${task.realtime_urls.length} urls`
3445
- );
3446
- for (const realtimeUrl of task.realtime_urls) {
3447
- task.log(`Downloading GTFS-Realtime from ${realtimeUrl}`);
3448
- const gtfsRealtimeData = await downloadGtfsRealtimeData(realtimeUrl, task);
3449
- if (!gtfsRealtimeData?.entity) {
3450
- continue;
3451
- }
3452
- task.log(`Download successful`);
3453
- let totalLineCount = 0;
3454
- for (const entity of gtfsRealtimeData.entity) {
3455
- let model;
3456
- if (entity.vehicle) {
3457
- model = vehiclePositions;
3458
- }
3459
- if (entity.tripUpdate) {
3460
- model = tripUpdates;
3461
- }
3462
- if (entity.alert) {
3463
- model = serviceAlerts;
3464
- }
3465
- if (!model) {
3466
- break;
3467
- }
3468
- const fieldValues = model.schema.map(
3469
- (column) => prepareRealtimeValue(entity, column, task)
3470
- );
3471
- try {
3472
- db.prepare(
3473
- `REPLACE INTO ${model.filenameBase} (${model.schema.map((column) => column.name).join(", ")}) VALUES (${fieldValues.join(", ")})`
3474
- ).run();
3475
- } catch (error) {
3476
- task.logWarning("Import error: " + error.message);
3446
+ if (task.realtimeAlerts?.url) {
3447
+ const gtfsRealtimeData = await downloadGtfsRealtimeData(
3448
+ task.realtimeAlerts,
3449
+ task
3450
+ );
3451
+ if (gtfsRealtimeData?.entity) {
3452
+ task.log(`Download successful`);
3453
+ let totalLineCount = 0;
3454
+ for (const entity of gtfsRealtimeData.entity) {
3455
+ const fieldValues = serviceAlerts.schema.map(
3456
+ (column) => prepareRealtimeValue(entity, column, task)
3457
+ );
3458
+ try {
3459
+ db.prepare(
3460
+ `REPLACE INTO ${serviceAlerts.filenameBase} (${serviceAlerts.schema.map((column) => column.name).join(", ")}) VALUES (${fieldValues.join(", ")})`
3461
+ ).run();
3462
+ } catch (error) {
3463
+ task.logWarning("Import error: " + error.message);
3464
+ }
3465
+ const alertTargetArray = [];
3466
+ for (const informedEntity of entity.alert.informedEntity) {
3467
+ informedEntity.parent = entity;
3468
+ const subValues = serviceAlertTargets.schema.map(
3469
+ (column) => prepareRealtimeValue(informedEntity, column, task)
3470
+ );
3471
+ alertTargetArray.push(`(${subValues.join(", ")})`);
3472
+ totalLineCount++;
3473
+ }
3474
+ try {
3475
+ db.prepare(
3476
+ `REPLACE INTO ${serviceAlertTargets.filenameBase} (${serviceAlertTargets.schema.map((column) => column.name).join(", ")}) VALUES ${alertTargetArray.join(", ")}`
3477
+ ).run();
3478
+ } catch (error) {
3479
+ task.logWarning("Import error: " + error.message);
3480
+ }
3481
+ task.log(`Importing - ${totalLineCount++} entries imported\r`, true);
3477
3482
  }
3478
- if (entity.tripUpdate) {
3483
+ }
3484
+ }
3485
+ if (task.realtimeTripUpdates?.url) {
3486
+ const gtfsRealtimeData = await downloadGtfsRealtimeData(
3487
+ task.realtimeTripUpdates,
3488
+ task
3489
+ );
3490
+ if (gtfsRealtimeData?.entity) {
3491
+ task.log(`Download successful`);
3492
+ let totalLineCount = 0;
3493
+ for (const entity of gtfsRealtimeData.entity) {
3494
+ const fieldValues = tripUpdates.schema.map(
3495
+ (column) => prepareRealtimeValue(entity, column, task)
3496
+ );
3497
+ try {
3498
+ db.prepare(
3499
+ `REPLACE INTO ${tripUpdates.filenameBase} (${tripUpdates.schema.map((column) => column.name).join(", ")}) VALUES (${fieldValues.join(", ")})`
3500
+ ).run();
3501
+ } catch (error) {
3502
+ task.logWarning("Import error: " + error.message);
3503
+ }
3479
3504
  const stopTimeUpdateArray = [];
3480
3505
  for (const stopTimeUpdate of entity.tripUpdate.stopTimeUpdate) {
3481
3506
  stopTimeUpdate.parent = entity;
@@ -3492,26 +3517,31 @@ var updateRealtimeData = async (task) => {
3492
3517
  } catch (error) {
3493
3518
  task.logWarning("Import error: " + error.message);
3494
3519
  }
3520
+ task.log(`Importing - ${totalLineCount++} entries imported\r`, true);
3495
3521
  }
3496
- if (entity.alert) {
3497
- const alertTargetArray = [];
3498
- for (const informedEntity of entity.alert.informedEntity) {
3499
- informedEntity.parent = entity;
3500
- const subValues = serviceAlertTargets.schema.map(
3501
- (column) => prepareRealtimeValue(informedEntity, column, task)
3502
- );
3503
- alertTargetArray.push(`(${subValues.join(", ")})`);
3504
- totalLineCount++;
3505
- }
3522
+ }
3523
+ }
3524
+ if (task.realtimeVehiclePositions?.url) {
3525
+ const gtfsRealtimeData = await downloadGtfsRealtimeData(
3526
+ task.realtimeVehiclePositions,
3527
+ task
3528
+ );
3529
+ if (gtfsRealtimeData?.entity) {
3530
+ task.log(`Download successful`);
3531
+ let totalLineCount = 0;
3532
+ for (const entity of gtfsRealtimeData.entity) {
3533
+ const fieldValues = tripUpdates.schema.map(
3534
+ (column) => prepareRealtimeValue(entity, column, task)
3535
+ );
3506
3536
  try {
3507
3537
  db.prepare(
3508
- `REPLACE INTO ${serviceAlertTargets.filenameBase} (${serviceAlertTargets.schema.map((column) => column.name).join(", ")}) VALUES ${alertTargetArray.join(", ")}`
3538
+ `REPLACE INTO ${tripUpdates.filenameBase} (${tripUpdates.schema.map((column) => column.name).join(", ")}) VALUES (${fieldValues.join(", ")})`
3509
3539
  ).run();
3510
3540
  } catch (error) {
3511
3541
  task.logWarning("Import error: " + error.message);
3512
3542
  }
3543
+ task.log(`Importing - ${totalLineCount++} entries imported\r`, true);
3513
3544
  }
3514
- task.log(`Importing - ${totalLineCount++} entries imported\r`, true);
3515
3545
  }
3516
3546
  }
3517
3547
  task.log(`GTFS-Realtime data import complete`);
@@ -3823,10 +3853,11 @@ async function importGtfs(initialConfig) {
3823
3853
  const tempPath = temporaryDirectory();
3824
3854
  const task = {
3825
3855
  exclude: agency2.exclude,
3826
- agency_url: agency2.url,
3856
+ url: agency2.url,
3827
3857
  headers: agency2.headers,
3828
- realtime_headers: agency2.realtimeHeaders,
3829
- realtime_urls: agency2.realtimeUrls,
3858
+ realtimeAlerts: agency2.realtimeAlerts,
3859
+ realtimeTripUpdates: agency2.realtimeTripUpdates,
3860
+ realtimeVehiclePositions: agency2.realtimeVehiclePositions,
3830
3861
  downloadDir: tempPath,
3831
3862
  downloadTimeout: config.downloadTimeout,
3832
3863
  gtfsRealtimeExpirationSeconds: config.gtfsRealtimeExpirationSeconds,
@@ -3841,7 +3872,7 @@ async function importGtfs(initialConfig) {
3841
3872
  logError: logError2
3842
3873
  };
3843
3874
  try {
3844
- if (task.agency_url) {
3875
+ if (task.url) {
3845
3876
  await downloadFiles(task);
3846
3877
  }
3847
3878
  await readFiles(task);