Documentation ¶
Overview ¶
Package transit provides functions for fetching and processing public transit information.
Package transit specifies the model (types, const, data structures,...) of the library.
Index ¶
- Constants
- Variables
- func ApplyRegexAllSubmatch(content string, regex string) ([][]string, error)
- func BuildLineIdWithDirection(id, direction string) string
- func CRC32(s string) int
- func CheckConsistency(td TransitData) (report string, err error)
- func CreateFile(path, content string) error
- func CurrentPath() string
- func Download(address string, fileFullPath string, validateFunc func(string) bool) (err error)
- func Exists(path string) bool
- func GetEnvVariableValueBool(v string) bool
- func IsFileSizeGreaterThanZero(p string) bool
- func IsIgnored(id string) bool
- func LinesParser(l *[]Line, ts TransitSource) error
- func LoadIgnoreLineIds() map[string]bool
- func MD5(s string) string
- func ParseAgencyLinesFile(filePath string) (*[]Line, error)
- func Publish(td TransitData, destPath string, p Presenter) error
- func RemediateLineName(lines *[]Line, agencyLineId string, expected string) error
- func RemoveDuplicatedStopsInLine() bool
- func ReverseLineName(name string) (string, error)
- func ScheduleParser(l *[]Line, ts TransitSource) error
- func StopsParser(lines *[]Line, ts TransitSource) error
- func ToDirectionNumber(direction string) string
- func ToDirectionPrefix(direction string) string
- func UnzipFromArchive(archive, file, dest string) error
- func UseCachedData() bool
- type AgencyLine
- type Bilbobus
- type Coordinates
- type JobSchedule
- type JsonPresenter
- type Line
- type MetadataItem
- type Parse
- type Parser
- type Presenter
- type Stop
- type Timetable
- type TransitData
- type TransitSource
Constants ¶
const AgencyNameSeparator string = "-"
const DirectionBackward string = "BACKWARD"
const DirectionBackwardNumber string = "2"
const DirectionBackwardShortPrefix string = "V"
const DirectionForward string = "FORWARD"
const DirectionForwardNumber string = "1"
const DirectionForwardShortPrefix string = "I"
const EnvBilbobusAgencyLineWithStops = "BILBOBUS_AGENCY_LINE_WITH_STOPS"
const EnvMetadata string = "METADATA"
const EnvNameBilbao string = "BILBAO_TRANSIT"
Constants
const EnvNameBilbobusSummerEnd string = "BILBOBUS_SUMMER_END"
const EnvNameBilbobusSummerStart string = "BILBOBUS_SUMMER_START"
Constants
const EnvNameReuseLocalData string = "REUSE_TRANSIT_LOCAL_FILES"
const EnvRemoveDuplicatedStopsInLine string = "REMOVE_DUPLICATED_STOPS_IN_LINE"
const GeneratedBaseNumber int = 9000
Constants
const RegexPatternStopConnections = `(?mU)<td headers="correspondencias_(ida|vuelta) correspondencia_parada">(?s)(.*)<\/td>` // flag as Ungreedy
const RegexPatternStopConnectionsNumbers = `(?m).*ntido=(1|2)">\s*(\S*)\s*<\/a>`
const RegexPatternStopId = `(?m).*codLinea=.{1,3}&temporada=.{1,2}&parada=(.*)\">`
const RegexPatternStopName = `(?m).*<td headers="parada_(.*)"><span.*</span>(.*)</td>`
const RegexPatternStopPosition = `(?m).*href="https:\/\/maps\.google\.com\/\?q=(.*),(.*)"`
const RemediationTag = "REMEDIATE"
const SaturdayTypeId string = "2"
const SeasonSummer string = "VE"
const SeasonWinter string = "IV"
const SourceLines string = "Lines"
Consts
const SourceSchedule string = "Schedule"
const SourceStops string = "Stops"
const StopNameBackwardIdentifier = "VUELTA"
const StopNameForwardIdentifier = "IDA"
const SundayTypeId string = "3"
const TokenDay string = "<DAYTYPE>"
const TokenDirection string = "<DIRECTIONID>"
const TokenLine string = "<LINEID>"
const TokenSeason string = "<SEASON>"
const TokenStop string = "<STOPID>"
const WeekDayTypeId string = "1"
Variables ¶
var Directions = [2]string{DirectionForward, DirectionBackward}
Globals
var DirectionsPrefixes = [2]string{DirectionForwardShortPrefix, DirectionBackwardShortPrefix}
var LinesIgnored map[string]bool
var MaxDownloadRetries int = 10
Functions ¶
func ApplyRegexAllSubmatch ¶
ApplyRegexAllSubmatch compile and apply the supplied regular expression over content and returns the list of matches
func BuildLineIdWithDirection ¶
BuildLineIdWithDirection returns a line identifier based on the agency Id and the direction (e.g. I182 for agency Id 182 and direction FORWARD)
func CheckConsistency ¶
func CheckConsistency(td TransitData) (report string, err error)
CheckConsistency verifies that the output data is consistent with the information published by the transit agency
func CreateFile ¶
CreateFile creates a file in path with the supplied content.
func Download ¶
Pulls down the content of the provided url and stores in filepath. If anything goes wrong it returns an error otherwise nil
func GetEnvVariableValueBool ¶
GetEnvVariableValueBool returns true if the variable is defined and the value can be mapped to true. False otherwise.
func IsFileSizeGreaterThanZero ¶
IsFileSizeGreaterThanZero returns true if the file p is greater than zero.
func LinesParser ¶
func LinesParser(l *[]Line, ts TransitSource) error
checkStopsSchedules verifies that the schedule information associated with with the information published by the transit agency
func LoadIgnoreLineIds ¶
LoadIgnoreLineIds loads the list of line ids to ignore from environment
func ParseAgencyLinesFile ¶
parseAgencyLinesFile parses the agency file containing the list of lines.
func Publish ¶
func Publish(td TransitData, destPath string, p Presenter) error
Publish deploys lines in the correct format in path. The format is determined by the presenter.
func RemediateLineName ¶
RemediateLineName replace the detected line name in data by the expected. The expected name shall correspond to direction FORWARD.
func RemoveDuplicatedStopsInLine ¶
func RemoveDuplicatedStopsInLine() bool
RemoveDuplicatedStopsInLine returns True if duplicated stops are not allowed in the same line for a given direction
func ReverseLineName ¶
ReverseLineName takes a line name formatted as "origin - destination" and returns "destination - origin"
func ScheduleParser ¶
func ScheduleParser(l *[]Line, ts TransitSource) error
ScheduleParser implements the signature of type Decorator. It's responsible for decorating lines with the location of the stops.
func StopsParser ¶
func StopsParser(lines *[]Line, ts TransitSource) error
StopsParser implements the signature of type Parse. It's responsible for adding stops to every line present in l.
func ToDirectionNumber ¶
ToDirectionNumber returns the identifier that matches the given direction string (either DirectionBackward or DirectionForward )
func ToDirectionPrefix ¶
ToDirectionPrefix returns the prefix that matches the given direction string (either DirectionBackward or DirectionForward )
func UnzipFromArchive ¶
UnzipFromArchive is a function that unzips a file from the supplied zip archive. The unzipped file is save to dest path.
func UseCachedData ¶
func UseCachedData() bool
UseCachedData returns True if the locally cached data must be used as data source for transit information.
Types ¶
type Bilbobus ¶
type Bilbobus struct {
// contains filtered or unexported fields
}
Bilbobus is a parser of transit information of Bilbao bus agency.
func (Bilbobus) BuildMetadata ¶
func (p Bilbobus) BuildMetadata() []MetadataItem
Read the metadata from the env var. Returns an object holding the Metadata information.
func (Bilbobus) Data ¶
func (p Bilbobus) Data() TransitData
Observer that returns the list of lines for this transit.
func (*Bilbobus) Digest ¶
func (p *Bilbobus) Digest(sources []TransitSource) error
Process the data files in folder dataPath and build the data model.
func (Bilbobus) GetSources ¶
func (p Bilbobus) GetSources() []TransitSource
Read the data sources from the env var. Returns the list of sources.
type Coordinates ¶
Location data (typically of a stop).
type JsonPresenter ¶
type JsonPresenter struct { }
Bilbobus is a parser of transit information of Bilbao bus agency.
func (JsonPresenter) Format ¶
func (p JsonPresenter) Format(l Line) (string, error)
Returns line with the right format to be presented. Tipically the chosen format is json.
func (JsonPresenter) FormatList ¶
func (p JsonPresenter) FormatList(l []Line) (string, error)
Returns the array of lines with the right format to be presented. Tipically the chosen format is json.
type Line ¶
type Line struct { Id string `json:"Id,omitempty"` AgencyId string `json:"AgencyId,omitempty"` Number int `json:"Number,omitempty"` Name string `json:"Name,omitempty"` Direction string `json:"Dir,omitempty"` Stops []Stop `json:"Stops,omitempty"` MapRoute []Coordinates `json:"Map,omitempty"` IsNightLine *bool `json:"Night,omitempty"` }
Line represents a line of transport mean. Consists of some data and the list of stops for the route of the line.
type MetadataItem ¶
type MetadataItem struct { MinVersion string `json:"MinVersion,omitempty"` MaxVersion string `json:"MaxVersion,omitempty"` PathData string `json:"PathData,omitempty"` Validity string `json:"Validity,omitempty"` UpdateClient string `json:"UpdateClient,omitempty"` LastUpdate string `json:"LastUpdate,omitempty"` }
Metadata contains meta-information about the data provided, such as: last time data was updated, path , etc. It is a list of MetadataItems so the consumers can be redirected to the right version of the data according to MinVersion and MaxVersion.
type Parse ¶
type Parse func(*[]Line, TransitSource) error
Parse is a type of function that receives a list of Lines and adds new information to that list. For example: a Decorator function might add the location of each stop in the provided list of lines.
type Parser ¶
type Parser interface { Digest(dataPath string) error Lines() []Line Stops(lineId string, direction string) []Stop }
Parser is an interface that must be implemented per transit agency. Exposes Parse method to digest the raw data provided by the agency. Once parsed, the agency information can be queries using the rest of the methods: Lines, Stops, etc.
type Presenter ¶
Presenter is an interface implemented by formatter classes. Exposes methods to transform lines into the presenting format, e.g. JSON
type Stop ¶
type Stop struct { Id string `json:"Id,omitempty"` Name string `json:"Na,omitempty"` Connections string `json:"Co,omitempty"` Schedule Timetable `json:"Sc,omitempty"` Location Coordinates `json:"Lc,omitempty"` }
Stop keeps the information of a (bus, metro,...) stop.
type Timetable ¶
type Timetable struct { Weekday string `json:"Wor,omitempty"` MondayToThrusday string `json:"M2T,omitempty"` Friday string `json:"Fri,omitempty"` Saturday string `json:"Sat,omitempty"` Sunday string `json:"Sun,omitempty"` }
Timetable stores the schedule per type of day.
type TransitData ¶
type TransitData struct {
// contains filtered or unexported fields
}
Bilbobus is a parser of transit information of Bilbao bus agency.
type TransitSource ¶
type TransitSource struct {
Path, Uri, Id string
}
TransitSource tells what data a source has to have.