npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

trainspy

v2.0.9

Published

Get departures at any UK train station & recieve instant real-time updates on trains throughout their journey via this custom made Realtimetrains scraper.

Downloads

16

Readme

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.