Skip to content

Commit

Permalink
Merge pull request #6 from bengawalk/feature/intermediate_stops
Browse files Browse the repository at this point in the history
Add intermediate stops for selected route
  • Loading branch information
seadeep42 authored Jan 8, 2024
2 parents 84eb3d1 + 64ebcb2 commit c9b85af
Show file tree
Hide file tree
Showing 89 changed files with 2,796 additions and 369 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
yarn install
yarn start
```

## bmtc_utils
This folder contains node.js scripts to query and manipulate BMTC API data.
`bmtc_api_data.csv`: KIA subset of the BMTC data with route names, IDs and other relevant info
`get_timings.json`: Gets the latest timetable and saves it to `src/utils/timings.json`
`get_stops.json`: Get the latest data of stops along all routes and saves to `src/utils/stops.json`

## Contributing to the repo
Before raising a pull request for feature additions, please open an issue discussing the change.
Expand Down
42 changes: 42 additions & 0 deletions cron/bmtc_api_data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Route name,Route ID,From station ID,To station ID,Parent Route ID
KIA-9 UP,1863,20921,35805,1101
KIA-9 DOWN,1864,23485,20922,1101
KIA-4 UP,2985,35189,35805,1697
KIA-4 DOWN,2986,23485,25675,1697
KIA-4A UP,2987,21277,35805,1698
KIA-4A DOWN,2988,23485,23656,1698
KIA-6 UP,2989,20902,35805,1699
KIA-6 DOWN,2990,23485,20902,1699
KIA-6W UP,2991,23656,35805,1700
KIA-6A UP,2993,20902,35805,1701
KIA-6A DOWN,2994,23485,20902,1701
KIA-15 UP,2995,23656,35805,1702
KIA-15 DOWN,2996,23485,23656,1702
KIA-15A DOWN,2998,23485,23656,1703
KIA-10 UP,3812,21042,35805,2124
KIA-10 DOWN,3813,23485,21042,2124
KIA-8C UP,3835,21818,35805,2136
KIA-8C DOWN,3836,23485,21817,2136
KIA-5 UP,3914,20623,35805,2164
KIA-5 DOWN,3917,23485,20624,2164
KIA-7A UP,3953,23716,35805,2193
KIA-7A DOWN,3954,23485,23925,2193
KIA-8 UP,3964,21934,35805,2200
KIA-8 DOWN,3965,23485,21933,2200
KIA-8D UP,3968,28169,35805,2202
KIA-8D DOWN,3976,23485,34475,2202
KIA-8A UP,4002,20687,35805,2223
KIA-8A DOWN,4003,23485,20686,2223
KIA-8E UP,4420,20772,35805,2431
KIA-8E DOWN,4421,23485,20772,2431
KIA-5D UP,8099,36503,35805,4351
KIA-5D DOWN,8101,23485,36502,4351
KIA-17 UP,8638,29871,35805,4638
KIA-17 DOWN,8641,23485,29871,4638
KIA-9H UP,11732,25819,35805,6250
KIA-9H DOWN,11733,23485,20823,6250
KIA-8EW DOWN,11797,23485,20772,6283
KIA-14 UP,12136,23485,28059,6463
KIA-14 DOWN,12137,34431,35805,6463
KIA-7 UP,12138,23485,32987,6464
KIA-7 DOWN,12139,26024,35805,6464
96 changes: 96 additions & 0 deletions cron/get_stops.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
const fs = require("node:fs");
const path = require("node:path");
const axios = require("axios");
const _ = require("lodash");
const jsonStringify = require("fast-json-stable-stringify");
const { getBmtcData, DEFAULT_HEADERS } = require("./utils");

require("dotenv").config({ path: path.join(__dirname, "../.env") });

try {
const getStops = async () => {
const stopsData = {};
const kiaData = getBmtcData();

for (let i = 0; i < kiaData.length; i++) {
const {
routeno,
// parentrouteid
} = kiaData[i];

/************************************************************************
// This is ideally how we should've done it but the API is unreliable.
// So for now we'll just use a backup of the API responses taken from Vonter/bmtc-gtfs
// Instead of Axios call, it'll be file read and parse. Rest of the code will remain the same
************************************************************************/
// const { data: knData } = await axios.post(
// `${process.env.BMTC_API_ENDPOINT}/SearchByRouteDetails_v4`,
// {
// routeid: parentrouteid,
// },
// {
// headers: DEFAULT_HEADERS,
// },
// );
// // Same API call as above. Only lan header is different
// const { data: enData } = await axios.post(
// `${process.env.BMTC_API_ENDPOINT}/SearchByRouteDetails_v4`,
// {
// routeid: parentrouteid,
// },
// {
// headers: {
// ...DEFAULT_HEADERS,
// lan: "en",
// },
// },
// );

const enDataRaw = fs.readFileSync(
path.join(__dirname, `stops_responses/en/${routeno}.json`),
);
const knDataRaw = fs.readFileSync(
path.join(__dirname, `stops_responses/en/${routeno}.json`),
);

const enData = JSON.parse(enDataRaw);
const knData = JSON.parse(knDataRaw);

const direction = _.toLower(_.split(routeno, " ")[1]);
const enStops = enData[direction].data;

const stopsDataForTheRoute = {
totalDistance: _.last(enStops).distance_on_station,
stops: [],
};

// Exclude the first and last stops.
// BMTC response includes source and destination. We only want intermediate stops
for (let i = 1; i < enStops.length - 1; i++) {
stopsDataForTheRoute.stops.push({
name: enStops[i].stationname,
name_kn: knData[direction].data[i].stationname,
loc: [enStops[i].centerlat, enStops[i].centerlong],
distance: enStops[i].distance_on_station,
});
}

stopsData[routeno] = stopsDataForTheRoute;
console.log(`Stops fetched for: ${routeno}`);
}

try {
fs.writeFileSync(
path.join(__dirname, "../src/utils/stops.json"),
jsonStringify(stopsData),
);
console.log("Successfully wrote stops to file");
} catch (err) {
console.error(err);
}
};

getStops();
} catch (err) {
console.error(err);
}
89 changes: 89 additions & 0 deletions cron/get_timings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const fs = require("node:fs");
const path = require("node:path");
const axios = require("axios");
const _ = require("lodash");
const jsonStringify = require("fast-json-stable-stringify");
const { getBmtcData, DEFAULT_HEADERS } = require("./utils");

require("dotenv").config({ path: path.join(__dirname, "../.env") });

/**
* We need to swap some timings
* because the API returns inverted timings
* for UP and DOWN for some routes.
*
* @param {{
* [key: string]: number[]
* }} timings
*/
const swapTimings = (timings) => {
const swaps = [
["KIA-7 UP", "KIA-7 DOWN"],
["KIA-14 UP", "KIA-14 DOWN"],
];

swaps.forEach(([a, b]) => {
const temp = timings[a];
timings[a] = timings[b];
timings[b] = temp;

console.log("Swapped timings for", a, "and", b, "routes");
});
};

try {
const checkTimings = async () => {
const timings = {};
const kiaData = getBmtcData();

// Check for tomorrow, not today
const currentDate = new Date(Date.now() + 86400000);

for (let i = 0; i < kiaData.length; i++) {
const { routeno, routeid, fromstationid, tostationid } = kiaData[i];

const { data } = await axios.post(
`${process.env.BMTC_API_ENDPOINT}/GetTimetableByRouteId_v2`,
{
routeid,
fromStationId: fromstationid,
toStationId: tostationid,
current_date: currentDate.toISOString(),
},
{
headers: DEFAULT_HEADERS,
},
);
const timingsArray = _.map(data.data[0]?.tripdetails, (timeItem) => {
const { starttime, endtime } = timeItem;
const [hoursStart, minutesStart] = _.split(starttime, ":");
const [hoursEnd, minutesEnd] = _.split(endtime, ":");
const startMinutes =
_.toNumber(hoursStart) * 60 + _.toNumber(minutesStart);
const endMinutes = _.toNumber(hoursEnd) * 60 + _.toNumber(minutesEnd);
return {
start: startMinutes,
duration: endMinutes - startMinutes,
};
});
timings[routeno] = _.sortBy(timingsArray, "start");
console.log(`Timings fetched for: ${routeno}`);
}

swapTimings(timings);

try {
fs.writeFileSync(
path.join(__dirname, "../src/utils/timings.json"),
jsonStringify(timings),
);
console.log("Successfully wrote timings to file");
} catch (err) {
console.error(err);
}
};

checkTimings();
} catch (err) {
console.error(err);
}
133 changes: 0 additions & 133 deletions cron/index.js

This file was deleted.

1 change: 1 addition & 0 deletions cron/stops_responses/en/KIA-10 DOWN.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cron/stops_responses/en/KIA-10 UP.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cron/stops_responses/en/KIA-14 DOWN.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cron/stops_responses/en/KIA-14 UP.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cron/stops_responses/en/KIA-15 DOWN.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cron/stops_responses/en/KIA-15 UP.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cron/stops_responses/en/KIA-15A DOWN.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cron/stops_responses/en/KIA-17 DOWN.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cron/stops_responses/en/KIA-17 UP.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cron/stops_responses/en/KIA-18 DOWN.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cron/stops_responses/en/KIA-18 UP.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cron/stops_responses/en/KIA-4 DOWN.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cron/stops_responses/en/KIA-4 UP.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cron/stops_responses/en/KIA-4A DOWN.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cron/stops_responses/en/KIA-4A UP.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cron/stops_responses/en/KIA-5 DOWN.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cron/stops_responses/en/KIA-5 UP.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cron/stops_responses/en/KIA-5D DOWN.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cron/stops_responses/en/KIA-5D UP.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cron/stops_responses/en/KIA-6 DOWN.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cron/stops_responses/en/KIA-6 UP.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cron/stops_responses/en/KIA-6A DOWN.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cron/stops_responses/en/KIA-6A UP.json

Large diffs are not rendered by default.

Loading

0 comments on commit c9b85af

Please sign in to comment.