The norwegian equivalent of the DMV, Statens Vegvesen, have a website where they publish real time traffic information collected from the electronic toll payment tags that most cars are fitted with here near Oslo.
Here’s how it looks right now at noon – no congestion at all:
Let me translate the important parts:
Reisetid uten forsinkelse: 12 min
(Travelling time without delays: 12 min)
Forsinkelse: 1 min
(Delay: 1 min)
Forventet reisetid nå: 13 min
(Expected travelling time now: 13 min)
This is what the script will output:
PS > Get-Traffic This looks OK, maybe it's time to leave? Current : 13 Normal : 12 Delay : 0 DelayFactor : 1,08333333333333 Route : Skøyen - Asker
And here’s the script:
function Get-Traffic { [cmdletbinding()] Param( [int]$ID, [int]$DelayThreshold = 1.5 ) if(-not($ID)) { Write-Verbose "No route ID was defined" if ((get-date).hour -in 6..12) { $ID = "1039" } else { $ID = "1040" } Write-Verbose "Route ID is $($ID)" } $URL = "http://www.reisetider.no/reisetid/strekning.html?method=trafikkinformasjon&id=$ID" $Response = Invoke-WebRequest $URL $ExpectedTravellingTime = (($Response.ParsedHtml.body.innerText) -split ":" | select -Last 1) -replace " min","" $NormalTravellingTime = (((($Response.ParsedHtml.body.innerText) -split "`r`n")[0]) -split ":")[1] -replace " min","" $Delay = $ExpectedTravellingTime - $NormalTravellingTime $Route = ((((Invoke-WebRequest "http://www.reisetider.no/reisetid/strekning.html?id=$ID").ParsedHtml.getElementsByTagName("span")) | Where-Object className -eq "brodsmulemeny-valgt").innertext) $Traffic = [pscustomobject]@{ "Current" = $ExpectedTravellingTime "Normal" = $NormalTravellingTime "Delay" = $Delay "DelayFactor" = ($ExpectedTravellingTime / $NormalTravellingTime) "Route" = $Route } if ($Traffic.Delayfactor -ge $DelayThreshold) { Write-Output "Too much traffic, go back to work!" Write-Output $Traffic } else { Write-Output "This looks OK, maybe it's time to leave?" Write-Output $Traffic } }
As you can see, pretty easy stuff. Parsing the HTML requires some trial and error, but with the help of Chrome Developer Tools, this took no more than 2 minutes.