Skip to content

Instantly share code, notes, and snippets.

@jazzbanzai
Last active August 5, 2022 10:51
Show Gist options
  • Save jazzbanzai/6f6c954aa5415eac4c3e594b4a1054e9 to your computer and use it in GitHub Desktop.
Save jazzbanzai/6f6c954aa5415eac4c3e594b4a1054e9 to your computer and use it in GitHub Desktop.
TFL Line Status - Powershell
<#
Using the API documentation on the Transport for London website, write a PowerShell script that can return the:
*status of a given Tube line along with any reason for disruption.
*The script should be capable of providing both the current status of the line
*and the future status of the line, if a date range is supplied.
*The script should be capable of returning all Tube lines that are currently facing unplanned disruption.”
#>
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Function Get-TflTubeStatus {
[CmdletBinding()]
param (
[Parameter(HelpMessage = "Line name, or 'all' for all tube lines (Default:all)")]
[String]$LineName = 'all',
[Parameter(HelpMessage = "The start date and time (Default: Today)")]
[System.DateTime]$StartDate = (Get-Date -Format "yyyy-MM-ddT00:00:01Z"),
[Parameter(HelpMessage = "The end date and time maximum (5) years (Default: Today)")]
[System.DateTime]$EndDate = (Get-Date -Format "yyyy-MM-ddT23:58:59Z")
)
begin {
#vars
$TFLAPIUrl = 'https://api.tfl.gov.uk'
$Statuses = @()
$EndDate = $EndDate.AddMinutes(1)
#Get all valid modes
#$ModeData = Invoke-RestMethod -Uri "$TFLAPIUrl/Line/Meta/Modes" -UseBasicParsing
#$ModeData | Select modeName
$mymodes = 'tube,dlr,elizabeth-line,overground,tram'
$LineIDs = (Invoke-RestMethod -Method GET "$TFLAPIUrl/Line/Mode/$mymodes" -UseBasicParsing)
#Get all the lines id's:
If ($LineName -eq 'all') {
#Do nothing
} else {
$LineIDs = ($LineIDs | Where-Object -FilterScript {$_.id -like $LineName.ToLower()})
If ([string]::IsNullOrEmpty($LineIDs) -eq $true) {
$Statuses = "ERROR: Invalid Line"
return $Statuses
}
}
}
process {
ForEach ($LineID in $LineIDs.id) {
$URI = "$TFLAPIUrl/Line/$LineID/Status/$($StartDate | Get-Date -Format "yyyy-MM-ddThh:mm:ssZ")/to/$($EndDate | Get-Date -Format "yyyy-MM-ddThh:mm:ssZ")"
#Write-Output $URI
$LineStatus = Invoke-RestMethod -Method GET -Uri $URI -UseBasicParsing
$LineOutput = [PSCustomObject]@{
LineName = $LineStatus.name
Status = $LineStatus.lineStatuses.statusSeverityDescription
Reason = $LineStatus.lineStatuses.reason
From = $LineStatus.lineStatuses.validityPeriods.fromDate
Until = $LineStatus.lineStatuses.validityPeriods.toDate
}
$Statuses += $LineOutput
Start-Sleep -Milliseconds 100 #do some throttling
}
}
end {
return $Statuses
}
}
##############
# EXAMPLES:
##############
#Get Status for 22nd-23rd Aug 2022
Get-TflTubeStatus -StartDate "08/22/2022" -EndDate "08/23/2022"
#Get today for the Central Line
Get-TflTubeStatus -LineName 'Central'
#Get today for all lines in GUI/Grid View
Get-TflTubeStatus | Out-GridView
# Status with some colours
$myStatus = Get-TflTubeStatus
$myStatus | ForEach-Object {
Switch ($_.LineName)
{
'Bakerloo' {
Write-Host " $_ " -BackgroundColor DarkRed
$mystatus | Where-Object { $_.LineName -eq 'Bakerloo' }
}
'Central' {
Write-Host " $_ " -BackgroundColor Red
$mystatus | Where-Object { $_.LineName -eq 'Central' }
}
'Circle' {
Write-Host " $_ " -BackgroundColor Yellow
$mystatus | Where-Object { $_.LineName -eq 'Circle' }
}
'District' {
Write-Host " $_ " -BackgroundColor Green
$mystatus | Where-Object { $_.LineName -eq 'District' }
}
'DLR' {
Write-Host " $_ " -BackgroundColor DarkCyan
$mystatus | Where-Object { $_.LineName -eq 'DLR' }
}
'Elizabeth line' {
Write-Host " $_ " -BackgroundColor DarkMagenta
$mystatus | Where-Object { $_.LineName -eq 'Elizabeth line' }
}
'Hammersmith & City' {
Write-Host " $_ " -BackgroundColor Magenta
$mystatus | Where-Object { $_.LineName -eq 'Hammersmith & City' }
}
'Jubilee' {
Write-Host " $_ " -BackgroundColor Gray
$mystatus | Where-Object { $_.LineName -eq 'Jubilee' }
}
'London Overground' {
Write-Host " $_ " -BackgroundColor DarkYellow
$mystatus | Where-Object { $_.LineName -eq 'London Overground' }
}
'Metropolitan' {
Write-Host " $_ " -BackgroundColor DarkMagenta
$mystatus | Where-Object { $_.LineName -eq 'Metropolitan' }
}
'Northern' {
Write-Host " $_ " -BackgroundColor Black
$mystatus | Where-Object { $_.LineName -eq 'Northern' }
}
'Piccadilly' {
Write-Host " $_ " -BackgroundColor DarkBlue
$mystatus | Where-Object { $_.LineName -eq 'Piccadilly' }
}
'Tram' {
Write-Host " $_ " -BackgroundColor DarkGreen
$mystatus | Where-Object { $_.LineName -eq 'Tram' }
}
'Victoria' {
Write-Host " $_ " -BackgroundColor Blue
$mystatus | Where-Object { $_.LineName -eq 'Victoria' }
}
'Waterloo & City' {
Write-Host " $_ " -BackgroundColor Cyan
$mystatus | Where-Object { $_.LineName -eq 'Waterloo & City' }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment