Code Monkey home page Code Monkey logo

go-national-rail-client's Introduction

go-national-rail-client

Go Report Card FOSSA Status codecov

An open-source Golang library for interfacing with the National Rail's Darwin OpenLDBWS web service. This client enables the retrieval of real-time train information, focusing on departures and arrivals between specific stations.

The client have been developed based on Live Departure Boards Web Service documentation.

Getting Started

To use this client, register for an OpenLDBWS access token at National Rail Enquiries.

Configuration Access Token

The access token is mandatory and can be set in one of two ways:

  1. By setting the NR_ACCESS_TOKEN environment variable.
  2. By using the AccessTokenOpt("YOUR_ACCESS_TOKEN") client option.

If the client option is not provided, the client will attempt to use the access token from the environment variable. Please note that the access token is crucial for OpenLDBWS authentication; without it, client initialisation will fail.
If an invalid access token is provided, requests will fail authentication.

Examples

Obtain Departure Board With Details

To retrieve the departure board for a specific train station, provide the station code and the desired number of departures. This example demonstrates how to obtain the departure board for Gillingham (Kent) [GLM], displaying 5 upcoming train departures.

package main

import (
	"fmt"
	"os"
	"strconv"

	"github.com/jedib0t/go-pretty/v6/table"

	nr "github.com/martinsirbe/go-national-rail-client/nationalrail"
)

func main() {
	client, err := nr.NewClient(
		nr.AccessTokenOpt("e995265f-df60-4787-bafc-af5a433f9b22"),
	)
	if err != nil {
		panic(err)
	}

	board, err := client.GetDeparturesWithDetails(nr.StationCodeGillinghamKent, nr.NumRowsOpt(5))
	if err != nil {
		panic(err)
	}

	t := table.NewWriter()
	t.SetOutputMirror(os.Stdout)
	t.AppendHeader(table.Row{"Time", "Platform", "Departure Status", "Destination"})

	for _, s := range board.Services {
		std := "?"
		if s.STD != nil {
			std = *s.STD
		}

		platform := "X"
		if s.Platform != 0 {
			platform = strconv.Itoa(s.Platform)
		}

		etd := "?"
		if s.ETD != nil {
			etd = *s.ETD
		}

		t.AppendRow(table.Row{std, platform, etd, fmt.Sprintf("%s [%s]", s.Destination.Name, s.Destination.CRS)})
	}

	t.Render()
}

Output:

+-------+----------+------------------+--------------------------------+
| TIME  | PLATFORM | DEPARTURE STATUS | DESTINATION                    |
+-------+----------+------------------+--------------------------------+
| 19:07 | 3        | 19:09            | Ramsgate [RAM]                 |
| 19:11 | 3        | On time          | Rainham (Kent) [RAI]           |
| 19:15 | 1        | On time          | London Victoria [VIC]          |
| 19:24 | 2        | On time          | London St Pancras (Intl) [STP] |
| 19:30 | 2        | On time          | London Victoria [VIC]          |
+-------+----------+------------------+--------------------------------+

License

See the LICENSE file for license rights and limitations (MIT).

go-national-rail-client's People

Contributors

martinsirbe avatar

Forkers

fossabot

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.