tracktracker/script/show-courses.js

94 lines
2.0 KiB
JavaScript
Executable File

#!/usr/bin/env node
import * as courses from '../src/tam/courses.js';
import {displayTime} from '../src/util.js';
import process from 'process';
import path from 'path';
import { readFile } from 'fs/promises';
const network = JSON.parse(await readFile(
new URL('../src/tam/network.json', import.meta.url)
));
/**
* Convert stop ID to human-readable stop name.
*
* If the stop ID is not known, the ID will be kept as-is.
*/
const getStopName = stopId =>
{
if (stopId in network.stops)
{
return network.stops[stopId].properties.name;
}
return stopId;
};
/** Create a string representing a course for printing. */
const courseToString = course =>
{
let result = `Course #${course.id}
Line ${course.line} - Direction ${course.direction} - Bound for ${getStopName(course.finalStopId)}
Next stops:
`;
for (let [stopId, time] of course.passings)
{
result += `${displayTime(new Date(time))} - ${getStopName(stopId)}\n`;
}
return result;
};
/** Show user help. */
const doHelp = () =>
{
const name = "./" + path.relative(process.cwd(), process.argv[1]);
process.stdout.write(`Usage: ${name} TYPE [COURSE]
Show TaM courses data.
Set TYPE to 'realtime' to fetch real-time data (limited time scope) or to
'theoretical' to fetch planned courses for the day.
Set COURSE to a valid course ID to limit the output to a given course.
`);
};
/** Print realtime information for a course or all courses. */
const doPrint = async (kind, courseId) =>
{
const results = await courses.fetch(kind);
if (courseId)
{
if (courseId in results)
{
console.log(courseToString(results[courseId]));
}
else
{
console.log('Unknown course');
}
}
else
{
for (let course of Object.values(results))
{
console.log(courseToString(course));
console.log("======\n");
}
}
};
const argv = process.argv.slice(2);
if (argv.length === 0)
{
doHelp();
process.exit(1);
}
doPrint(argv[0], argv[1]);