traindeps

package module
v0.0.0-...-5fa5ec5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 24, 2023 License: MIT Imports: 10 Imported by: 0

README

Train Departures

Overview

A simple project that uses web scraping to capture UK Train departures from the Nation Rail website.

This is currently using the OJP board which as of August 2023 looks like it will be replaced with an improved board.

Current API

To fetch a train departure board it is as easy as referencing the project and calling:

package main

import (
	"fmt"
	"github.com/bperryman/traindeps"
	"os"
)

func main() {
	board, err := traindeps.LoadFromInternet("STATION")
	if err != nil {
		fmt.Printf("Error loading the departure board")
		os.Exit(-1)
	}
	for _, dep := range board.Departures {
		fmt.Printf("To: %s, leaving from: %s\n", dep.Destination, dep.Platform)
    }
}

Here STATION is the 3-letter code, for example KGX is the code for London Kings Cross. A list of these can be found on Wikipedia

History

During a particularly bad spell of train lateness I got so fed up that I wrote a scraper to pull down all the delays on the journeys that I would make. Just for kicks I ended up writing this in Common Lisp, Elixir, Python and the go version here.

Then the pandemic happened, along with a lack off journeys into work, and this project just sort of languished. Now I want to use this for a notice board, based around the Pimoroni Galactic Unicorn

  • yes, yes it looked cool and I bought it, so now I have a solution looking for a problem (again).

TODO

  • Rename the package to something more go friendly.
  • Update the scraper to retrieve the departure boards from the new presentation (currently beta?)
  • Update to include any special notifiers that are displayed - for example delays due to bad weather or special services being run
  • Allow for a specific destination - so your journey into work for example.
  • Allow for intermediate stations on the journey to be retrieved.
  • Update any testing.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Board

type Board struct {
	Station    string
	AsAt       time.Time
	Departures []Departure
}

Board represents a snapshot in time of the departure board at a station. Station is the statation the departure board is for. AsAt is the time the snapshot was taken. Departures holds the departing trains.

func LoadFromFile

func LoadFromFile(fileName, station string, asAt time.Time) (*Board, error)

LoadFromFile processes a National Rail HTML snapshot, stored in a file, and returns the departure board that it represents. No attempt is made to identify the station or the time the information was captured so these must also be supplied as additional arguments.

func LoadFromInternet

func LoadFromInternet(station string) (*Board, error)

LoadFromInternet makes a call to fetch the departure board for the specified station and returns the associated departure board.

type Departure

type Departure struct {
	Destination   string
	Platform      string
	DepartureTime time.Time
	ExpectedTime  time.Time
	LateByMins    int
	Status        TrainStatus
}

Departure represents a single train departure. Destination is the destination of the train Platform is the platform the train will be departing from - will be blank until a platform is assigned. DepartureTime is the scheduled departure time for the train. ExpectedTime is the actual time the trains is expected to leave. LateByMins is the delay in minutes of the train when the status is late Status is the status of the train.

func (Departure) String

func (d Departure) String() string

type TrainStatus

type TrainStatus int

TrainStatus defines that status that the departing train is in. The values are limited to: OnTime, Delayed, Cancelled, Late and Unknown

const (
	// OnTime - the train is expect at it's scheduled departure time
	OnTime TrainStatus = iota + 1

	// Delayed - there is a delay in the departure, no further details about the delay
	Delayed

	// Cancelled - the train has been cancelled
	Cancelled

	// Late - the train is delayed and is running late by a known amout of time
	Late

	// Unknown - No know information about the train exists
	Unknown
)

func (TrainStatus) String

func (ts TrainStatus) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL