#!/usr/bin/env ts-node /** * Downloads and extracts GTFS static data * Run: npm run setup-gtfs */ import * as fs from 'fs'; import * as path from 'path'; import * as https from 'https'; import { execSync } from 'child_process'; const GTFS_ZIP_URL = 'https://www.modeshift.app/api/v1/9814b106-2afe-47c8-919b-bdec6a5e521e/transport/gtfs/gtfs.zip'; const GTFS_DIR = path.join(__dirname, 'gtfs'); const ZIP_FILE = path.join(__dirname, 'gtfs.zip'); async function downloadFile(url: string, dest: string): Promise { return new Promise((resolve, reject) => { console.log(`Downloading GTFS data from ${url}...`); const file = fs.createWriteStream(dest); https.get(url, (response) => { if (response.statusCode === 302 || response.statusCode === 301) { // Handle redirect if (response.headers.location) { https.get(response.headers.location, (redirectResponse) => { redirectResponse.pipe(file); file.on('finish', () => { file.close(); resolve(); }); }).on('error', reject); } } else { response.pipe(file); file.on('finish', () => { file.close(); resolve(); }); } }).on('error', (err) => { fs.unlink(dest, () => reject(err)); }); }); } async function extractZip(zipPath: string, destDir: string): Promise { console.log(`Extracting GTFS data to ${destDir}...`); // Create directory if it doesn't exist if (!fs.existsSync(destDir)) { fs.mkdirSync(destDir, { recursive: true }); } // Use unzip command (available on most Linux/Mac systems) try { execSync(`unzip -o "${zipPath}" -d "${destDir}"`, { stdio: 'inherit' }); } catch (error) { throw new Error('Failed to extract zip. Make sure unzip is installed.'); } } async function main() { try { // Download GTFS zip await downloadFile(GTFS_ZIP_URL, ZIP_FILE); console.log('āœ“ Download complete'); // Extract zip await extractZip(ZIP_FILE, GTFS_DIR); console.log('āœ“ Extraction complete'); // Clean up zip file fs.unlinkSync(ZIP_FILE); console.log('āœ“ Cleanup complete'); // List extracted files const files = fs.readdirSync(GTFS_DIR); console.log(`\nāœ“ GTFS data ready! Files extracted:\n${files.map(f => ` - ${f}`).join('\n')}`); } catch (error) { console.error('Error setting up GTFS data:', error); process.exit(1); } } main();