Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
tgoulder4 committed Aug 9, 2023
2 parents e51b6e1 + 54862cb commit 20534f3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
.env
node_modules/
node_modules/*
74 changes: 57 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# trainspy
Get departures at any UK train station & recieve updates on trains throughout their journey via the RTT API.
Get departures at any UK train station & recieve updates on trains throughout their journey via a custom made RTT API.

## Install trainspy
```js
npm i trainspy
```
# Get departures
Departures can be retrived by ```findTrains(stationCode)``` or ```findTrains(stationName)```:
Departures can be retrieved by ```stationCode``` or ```stationName```:
```js
findTrains("WLF") || findTrains("Whittlesford Parkway")
const x = findTrains("WLF")
const y = findTrains("Whittlesford Parkway") //same result as x
```
which returns in the following format:
```js
Expand All @@ -22,35 +23,74 @@ which returns in the following format:
```

# Tracking a train
You first need the ```serviceID```.
Can be retrived by ```findTrains(stationCode)``` as shown above.
```js
trackTrain(serviceID, refreshRate)
trackTrain(serviceID, timeTillRefresh?) //minimum timeTillRefresh of 5 seconds
```
You first need the ```serviceID```.
Can be retrived by ```findTrains(stationCode)``` as shown above.
```js
trackTrain("L14125").then((emitter) => {
emitter.on("UPDATE", (update) => {
console.log(update) //replace with your logic in terms of the update
trackTrain("P70052").then((emitter) => {
emitter.on("journeyUpdate", (data) => {
//your code for journey updates here
});
emitter.on("errorUpdate", (data) => console.log(data));
});
```
```trackTrain()``` returns a promise - ```emitter```. Subscribe to this emitter as shown above.
```trackTrain()``` returns a promise - ```emitter```. Subscribe as shown above.
This emits live updates (as JSON) on the train until the journey is complete.
```js
{ status: 'Approaching', station: 'Hackney Downs [HAC]' }
{
status: 'Approaching',
station: {
name: 'Rugeley Town',
code: [ 'RGT' ],
arrival: { actual: '0615½' },
departure: { actual: null },
stopsHere: true
},
destination: 'Rugeley Trent Valley',
delay: '+3'
}
```
| Status | Explanation |
| ------------- | ------------- |
| Passed | Train just passed this non-stopping station |
| Approaching | Train is now approaching this station |
| Arriving | Train is now arriving at this stopping station |
| At platform | Train is now at a platform of this stopping station |
| Departed | Train just departed this stopping station |
## More examples
### Track the next service from London to Manchester
(Provided this service departs in the next hour)
```js
{ status: 'Departed', station: 'Hackney Downs [HAC]' }
import {trackTrain, findTrains} from "trainspy";

const services = await findTrains("EUS");
services.forEach((service) => {
if (service.destination == "Manchester Piccadilly") {
trackTrain(service.serviceID).then((emitter) => {
emitter.on("journeyUpdate", (update) => console.log(update));
emitter.on("errorUpdate", (data) => console.log(data));
});
}
});
```
## More examples
### Change html content
```js
import trackTrain from "trainspy";
import {trackTrain} from "trainspy";

trackTrain("P71733").then((myTrainTracker) =>
myTrainTracker.on("UPDATE", (currentState) => {
myTrainTracker.on("journeyUpdate", (currentState) => {
document.getElementByID("status").innerHTML = currentState.status;
document.getElementByID("station").innerHTML = currentState.station;
})
document.getElementByID("station").innerHTML = currentState.station.name;
});
myTrainTracker.on("errorUpdate",(error)=>{
console.log(error)
}
);
```

0 comments on commit 20534f3

Please sign in to comment.