Giter Club home page Giter Club logo

trainspy's Introduction

Trainspy 🔍

Downloads Discord

Get departures at any UK train station & recieve instant real-time updates on trains throughout their journey, without an API key. Version 2.0 brings exact location tracking with precise latitude & longitude co-ordinates!

Add me on discord - I'd love to hear your feedback! @thaitiesthetie

Install trainspy

npm i trainspy

Find departures

findTrains(stationNameOrCode) -> Promise<typeof Object>

import { findTrains } from "trainspy";

(async () => {
  const trains = await findTrains("WLF");
  const moreTrains = await findTrains("Solihull");

  console.log(trains);
})();

this yields a response:

{
  name: 'Whittlesford Parkway',
  code: 'WLF',
  location: { latitude: 52.1035981209, longitude: 0.1656457011 },
  departures: [
    {
      serviceID: 'L13825',
      destination: 'Norwich',
      arrival: { actual: '2109', scheduled: '2109' },
      departure: { actual: '2109¼', scheduled: '2109' },
      platform: '2',
      stopsHere: true,
      state: {
        status: 'Passed',
        station: {
          name: 'Waterbeach',
          code: 'WBC',
          location: { latitude: 52.2623177182, longitude: 0.1968191721 },
          platform: '2',
          stopsHere: false,
          delay: 0,
          arrival: { actual: null, scheduled: null },
          departure: { actual: '2127½', scheduled: '2128' }
        }
      }
    },
    ...]
}

Tracking a train

trackTrain(serviceID, date?, timeTillRefresh?) -> Promise<typeof EventEmitter>

Emit live updates on a train until its journey is complete. You first need the serviceID. Retrieved by findTrains(stationNameOrCode) as shown above.

E.g. ServiceID P70052 departing on 18/08/2023:

import { trackTrain } from "trainspy";

trackTrain("P70052", "2023-08-18").then((emitter) => {
  emitter.on("journey", (update) => {
    //your code for journey updates!
  });
  emitter.on("information", (update) => {
    console.log(update);
  });
});

🌻 Note: Date must be in the form YYYY-MM-DD, defaults to today. You must enter an event name of "journey" for journey updates, and "information" for information (error, cancellation etc.) updates.

Example journey update:

{
  status: 'At platform',
  station: {
    name: 'Smethwick Rolfe Street',
    code: 'SMR',
    location: { latitude: 52.4963984863, longitude: -1.9706383988 },
    platform: '2',
    stopsHere: true,
    delay: 0,
    arrival: { actual: '2146¾', scheduled: '2146½' },
    departure: { actual: null, scheduled: '2147' }
  },
  callingPoints: [
  {
    name: 'Sandwell & Dudley',
    code: 'SAD',
    location: { latitude: 52.508672806, longitude: -2.0115900516 },
    platform: '2',
    stopsHere: true,
    delay: 0,
    arrival: { actual: null, scheduled: '2152' },
    departure: { actual: null, scheduled: '2153' }
  },
    ...
  ]
}

Example information update:

  {
    information: 'Error', details: 'Check service ID.'
  }

Return values

Journey updates

Property Type
status string
station { name: string, code: string | null, location: { longitude: number | null, latitude: number | null }stopsHere: boolean, delay: number | null, arrival: { actual: string | null, scheduled: string | null }, departure: -same as arrival- }
callingPoints Array<station>

Information updates

Property Type
information string
details string

Statuses

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, today:

import { trackTrain, findTrains } from "trainspy";

const response = await findTrains("EUS");
const serviceID = response.departures.find(
  (departure) => departure.destination == "Manchester Piccadilly"
).serviceID;
trackTrain(serviceID).then((emitter) => {
  emitter.on("journey", (update) => {
    //do stuff!
  });
});

Basic react implementation

import {trackTrain} from "trainspy";

class trainInformationComponent extends Component {
  state = {
    Status: '',
    Station:''
  };

  trackTrain("P56592").then(emitter=>{
    emitter.on("journey",(update)=>{
      handleInfoChange(update);
    })
  });

  handleInfoChange = (newData) => {
    const newStatus = newData.status;
    const newStation = newData.station.name;
    this.setState({
      Status: newStatus,
      Station: newStation
    });
  }

  render() {
    return (
      <>
      <title>Tracking service P56592</title>
      <label>Status: {state.Status}</label>
      <label>Station: {state.Station}</label>
      </>
    );
  }
};

A project by Tye. Special thanks to @ellcom for their list of longitude & latitude for each station, and to RealTimeTrains for providing the primitives for this project.

trainspy's People

Contributors

dependabot[bot] avatar tgoulder4 avatar

Watchers

 avatar

trainspy's Issues

Create tests

Drag and drop test html files to tgoulder4.github.io/tests/trainspy/testfile.html,
create test.yaml for actions

Station code issue

Match returns null occasionally. Haven’t replicated. I think it’s a service leaves a non-pass station.

2-Convert times

Use dayjs to Change times into datetime objects with time respective to locale, E.g. '1304 1/2' → 13:04:05. In another country where this is 22:00 and adjusts accordingly e.g. depart at 22:05

No support for split services

Shows concatenated DOM matches
{ status: 'ApproachingAt platform', station: 'Northumberland Park [NUM]Cambridge [CBG]' }

3-[MAJOR]-trackTrain("..").that_

func tracktrain(service){
thatDepartsOnDate(emitter, date){
getHTML(date)
}
return emitter
}

They enter the date in the form yyyy-mm-dd or 'today' or 'tomorrow'

trackTrain(serviceID).thatDepartsOnDate(date)

sample use

const emitter = trackTrain("P70538").thatDepartsOnDate(today);
emitter.on('journeyUpdate',(data)=>console.log(data))

default behaviour:

trackTrain(serviceID).thatDepartsOnDate(today)

[MUST READ] Rules: Creating an issue 🌻

Hi there, thanks for seeking to improve trainspy. Please follow this format for creating an issue:

  • Attach the link to the RTT webpage: realtimetrains.co.uk/service/gb-nr: {THE SERVICE ID} / {THE DATE} /detailed
  • Describe what happened in as much detail as possible! 🌻

Vary refresh rate

Refresh rate should be varied according to scheduled times.
E.g. upon 30 seconds (+optional delay amount) before time of departure, check every second. This applies to every location.

Emit different types of updates

Emit update_type: 'connection', 'journey', types instead.
Users can choose what type of events they wish to subscribe to.

4-[MAJOR]-findTrains("").thatDepartOnDate(date)

findTrains("BRV").thatGoTo(dest).thatDepartOnDate(date).fromTime(time),.aroundTime(time);

new globals:
preferredDestination, startTimeLimit,dateOfDeparture

Insight:
new functions amend the globals which influence results
Default behaviour:

findServices(station).thatDepartOnDate(today).aroundTime(time),.fromTime(time)

tracking the next train that goes to manchester:
trackService(findServices("EUS").thatGoTo("MAN").thatDepartsOnDate(today).aroundTime(now).serviceID))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.