Skip to content

Commit

Permalink
stable
Browse files Browse the repository at this point in the history
  • Loading branch information
tgoulder4 committed Aug 22, 2023
1 parent 21914f5 commit d1741e0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 33 deletions.
6 changes: 2 additions & 4 deletions dist/src/findTrains.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const findStationNameAndCode = (stationNameOrCode) => {
}
else {
stationName = stationNameOrCode;
const jsonMatch = stationCodes["stations"].find(station => station["Station Name"] == stationNameOrCode);
const jsonMatch = stationCodes["stations"].find((station) => station["Station Name"] == stationNameOrCode);
if (jsonMatch) {
stationCode = jsonMatch["CRS Code"];
}
Expand Down Expand Up @@ -80,7 +80,7 @@ function findTrains(stationNameOrCode, dateOfDeparture = getCurrentDayTime("YYYY
};
const services = [];
//rate limiter
yield new Promise((r) => setTimeout(r, 1000));
yield new Promise((r) => setTimeout(r, 500));
const res = yield fetch(`https://www.realtimetrains.co.uk/search/detailed/gb-nr:${stationCode}/${dateOfDeparture}/${timeOfDeparture}`);
const $ = cheerio.load(yield res.text());
for (const el of $("a.service").toArray()) {
Expand Down Expand Up @@ -109,8 +109,6 @@ function findTrains(stationNameOrCode, dateOfDeparture = getCurrentDayTime("YYYY
: service.find(".platform.exp").text()
? service.find(".platform.exp").text()
: null;
// console.log(`Platform: ${platform}`);
yield new Promise((r) => setTimeout(r, 1000));
const currentTrainState = yield (0, trackTrain_1.trackOnce)(UID[1]); //.status and .station only
if (!service.hasClass("pass")) {
services.push((0, types_1.createDeparture)(UID[1], destination, arrival, departure, platform, stopsHere, currentTrainState));
Expand Down
19 changes: 14 additions & 5 deletions dist/src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const findTrains_1 = require("./findTrains");
const trackTrain_1 = require("./trackTrain");
// const util = require("util");
module.exports = {
findTrains: findTrains_1.default,
trackTrain: trackTrain_1.trackTrain,
};
const util = require("util");
// trackTrain("G59487").then((emitter) => {
// emitter.on("journey", (data) => {
// console.log(util.inspect(data, false, null, true));
Expand All @@ -15,7 +24,7 @@ module.exports = {
// console.log(util.inspect(data, false, null, true));
// });
// });
// (async () => {
// const data = await findTrains("BHM", "2023-08-19")
// console.log(util.inspect(data, false, null, true));
// })();
(() => __awaiter(void 0, void 0, void 0, function* () {
const data = yield (0, findTrains_1.default)("BHM");
console.log(util.inspect(data, false, null, true));
}))();
36 changes: 17 additions & 19 deletions src/findTrains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@ export const findStationNameAndCode = (stationNameOrCode: string) => {
const match = stationNameOrCode.match(/^[A-Z]{3}$/);
if (match) {
// console.log(`match: ${match}`);
const jsonMatch = stationLocations[match[0]]
if (jsonMatch){
stationName = stationLocations[match[0]].station_name
stationCode = match[0];
}
else{
stationName = null;
stationCode = null;
const jsonMatch = stationLocations[match[0]];
if (jsonMatch) {
stationName = stationLocations[match[0]].station_name;
stationCode = match[0];
} else {
stationName = null;
stationCode = null;
}
// console.log(`stationName: ${stationName}`);
// console.log(`stationCode: ${stationCode}`);
} else {
} else {
stationName = stationNameOrCode;
const jsonMatch = stationCodes["stations"].find(station=> station["Station Name"] == stationNameOrCode)
if (jsonMatch){
stationCode = jsonMatch["CRS Code"]
}
else{
stationCode = null;
const jsonMatch = stationCodes["stations"].find(
(station) => station["Station Name"] == stationNameOrCode
);
if (jsonMatch) {
stationCode = jsonMatch["CRS Code"];
} else {
stationCode = null;
}
}
return { stationName, stationCode };
Expand All @@ -61,7 +61,7 @@ export default async function findTrains(
): Promise<stationResponse | information["body"]> {
//if stationName is 3 letters, destructure from map
const { stationName, stationCode } =
findStationNameAndCode(stationNameOrCode)
findStationNameAndCode(stationNameOrCode);
// console.log(`stationName: ${stationName}, stationCode: ${stationCode}`)
const callOutAndInfoValue = await fetch(
`https://www.realtimetrains.co.uk/search/handler?location=${stationCode}`
Expand All @@ -88,7 +88,7 @@ export default async function findTrains(
};
const services: Array<Departure> = [];
//rate limiter
await new Promise((r) => setTimeout(r, 1000));
await new Promise((r) => setTimeout(r, 500));
const res = await fetch(
`https://www.realtimetrains.co.uk/search/detailed/gb-nr:${stationCode}/${dateOfDeparture}/${timeOfDeparture}`
);
Expand Down Expand Up @@ -120,8 +120,6 @@ export default async function findTrains(
: service.find(".platform.exp").text()
? service.find(".platform.exp").text()
: null;
// console.log(`Platform: ${platform}`);
await new Promise((r) => setTimeout(r, 1000));
const currentTrainState: state["body"] | null = await trackOnce(UID[1]); //.status and .station only
if (!service.hasClass("pass")) {
services.push(
Expand Down
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import findTrains from "./findTrains";
import { trackTrain } from "./trackTrain";
// const util = require("util");
module.exports = {
findTrains,
trackTrain,
};
const util = require("util");

// trackTrain("G59487").then((emitter) => {
// emitter.on("journey", (data) => {
Expand All @@ -14,7 +14,7 @@ module.exports = {
// console.log(util.inspect(data, false, null, true));
// });
// });
// (async () => {
// const data = await findTrains("BHM", "2023-08-19")
// console.log(util.inspect(data, false, null, true));
// })();
(async () => {
const data = await findTrains("BHM");
console.log(util.inspect(data, false, null, true));
})();

0 comments on commit d1741e0

Please sign in to comment.