December 16, 2015 | 12 Comments Let’s play with SNCF Api and Powershell ! My goal was to get a list of the next train in the train station near my home, from powershell… My functions are below, feel free to leave a comment, copy it, it’s free ! (Now on Github ! git it !) Of course, it’s not a serious post, but more is coming 😉 You can try this : PS C:\> (Get-gare -gare "clamart").idgare | Get-TrainDirection |%{ Get-NextTrain -idgare $_.idgare -traindirection $_.direction } | FT ETADeparture Direction ETAArrival ETAinMin From GareName ------------ --------- ---------- -------- ---- -------- 16/12/2015 13:49:00 Paris-Montparnasse 1-2 (Paris) 16/12/2015 13:49:00 0:1:18 Rambouillet vers Paris-Montparnasse 1-2 gare de Clamart 16/12/2015 14:04:00 Paris-Montparnasse 1-2 (Paris) 16/12/2015 14:04:00 0:16:18 Mantes-la-Jolie vers Paris-Montparnasse 1-2 gare de Clamart 16/12/2015 14:19:00 Paris-Montparnasse 1-2 (Paris) 16/12/2015 14:19:00 0:31:18 Rambouillet vers Paris-Montparnasse 1-2 gare de Clamart 16/12/2015 14:34:00 Paris-Montparnasse 1-2 (Paris) 16/12/2015 14:34:00 0:46:18 Plaisir-Grignon vers Paris-Montparnasse 1-2 gare de Clamart 16/12/2015 14:49:00 Paris-Montparnasse 1-2 (Paris) 16/12/2015 14:49:00 1:1:18 Rambouillet vers Paris-Montparnasse 1-2 gare de Clamart 16/12/2015 13:57:00 Mantes-la-Jolie (Mantes-la-Jolie) 16/12/2015 13:57:00 0:9:18 Paris-Montparnasse 1-2 vers Mantes-la-Jolie gare de Clamart 16/12/2015 14:57:00 Mantes-la-Jolie (Mantes-la-Jolie) 16/12/2015 14:57:00 1:9:18 Paris-Montparnasse 1-2 vers Mantes-la-Jolie gare de Clamart 16/12/2015 14:12:00 Rambouillet (Rambouillet) 16/12/2015 14:12:00 0:24:17 Paris-Montparnasse 1-2 vers Rambouillet gare de Clamart 16/12/2015 14:42:00 Rambouillet (Rambouillet) 16/12/2015 14:42:00 0:54:17 Paris-Montparnasse 1-2 vers Rambouillet gare de Clamart 16/12/2015 14:27:00 Plaisir-Grignon (Plaisir) 16/12/2015 14:27:00 0:39:18 Paris-Montparnasse 1-2 vers Plaisir-Grignon gare de Clamart Function Get-NextTrain { [cmdletbinding()] param( [Parameter(Mandatory = $true, ValueFromPipeline=$true)] $idgare, [Parameter(Mandatory = $true, ValueFromPipeline=$true)] $traindirection, $apikey = 'YourApiKeyfromapisncfwebsite' ) $datenow = Get-date -Format yyyyMMddTHHmmss $params2 = @{uri = "https://api.sncf.com/v1/coverage/sncf/stop_areas/$($idgare)/departures?from_datetime=$($datenow)"; Method = 'Get'; #(or POST, or whatever) Headers = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($apikey):")); } #end headers hash table } #end $params hash table $string = @" Liste des prochains trains pour {0} en provenance de {1} doit arriver a la {2} a {3} et partir a {4} "@ $var2 = invoke-restmethod @params2 $alltrain = ,@() foreach($departure in $($var2.departures)){ if($departure.display_informations.direction -like $($traindirection)){ $direction = $($departure.display_informations.direction -replace "gare de ","") -replace " 1-2 (Paris)","" [datetime]$convertarrival = ConvertTo-SncfDateTime -sncfdate $($departure.stop_date_time.arrival_date_time) [datetime]$convertdeparture = ConvertTo-SncfDateTime -sncfdate $($departure.stop_date_time.departure_date_time) [timespan]$timefromnow = NEW-TIMESPAN –Start $(Get-date) –End $convertarrival $hash = @{ Direction = $($direction) From = $($departure.route.name) GareName = $($departure.stop_point.name) ETAArrival = $($convertarrival) ETADeparture = $($convertdeparture) ETAinMin = "$($timefromnow.Hours):$($timefromnow.Minutes):$($timefromnow.Seconds)" } $Object = New-Object PSObject -Property $hash $alltrain += $Object } } return $alltrain } Function Get-TrainDirection { [cmdletbinding()] param( [Parameter(Mandatory = $true, ValueFromPipeline=$true)] [string]$idgare, $apikey = 'YourApiKeyfromapisncfwebsite' ) $datenow = Get-date -Format yyyyMMddTHHmmss $params2 = @{uri = "https://api.sncf.com/v1/coverage/sncf/stop_areas/$($idgare)/departures?from_datetime=$($datenow)"; Method = 'Get'; #(or POST, or whatever) Headers = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($apikey):")); } #end headers hash table } #end $params hash table $var2 = invoke-restmethod @params2 $direction = ,@() foreach($departure in $($var2.departures)){ $hash = @{ Direction = $($departure.display_informations.direction.ToString()) idgare = $($idgare) } $Object = New-Object PSObject -Property $hash $direction += $Object } $direction | Select-Object idgare,direction -Unique -Skip 1 } Function Get-Gare { param( $gare, $apikey = 'YourApiKeyfromapisncfwebsite' ) $params = @{uri = "https://api.sncf.com/v1/coverage/sncf/places?q=$($gare)"; Method = 'Get'; #(or POST, or whatever) Headers = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($apikey):")); } #end headers hash table } #end $params hash table $var = invoke-restmethod @params foreach($stoparea in $($var.places |?{ $_.embedded_type -eq "stop_area"})){ $hash = @{ Direction = $($stoparea.Name) idgare = $($stoparea.id) } $Object = New-Object PSObject -Property $hash Write-Output $Object } } function ConvertTo-SncfDateTime { param( $sncfdate ) $sncfyear = $sncfdate.Substring(0,4) $sncfmonth = $sncfdate.Substring(4,2) $sncfday = $sncfdate.Substring(6,2) $sncfhour = $sncfdate.Substring(9,2) $sncfminute = $sncfdate.Substring(11,2) $sncfsecond = $sncfdate.Substring(13,2) $datetime = Get-date -Year $sncfyear -Day $sncfday -Month $sncfmonth -Hour $sncfhour -Second $sncfsecond -Minute $sncfminute Write-Output $datetime } Share this:TwitterLinkedInFacebookLike this:Like Loading... Related
Woah! I’m really digging the template/theme of this site. It’s simple, yet effective. A lot of times it’s challenging to get that “perfect balance” between usability and visual appearance. I must say that you’ve done a excellent job with this. Also, the blog loads extremely fast for me on Firefox. Excellent Blog! Reply
I see your page needs some unique articles. Writing manually is time consuming, but there is solution for this hard task. Just search for: Miftolo’s tools rewriter Reply
Pretty component of content. I simply stumbled upon your weblog and in accession capital to assert that I acquire in fact enjoyed account your weblog posts. Any way I will be subscribing to your augment and even I achievement you get admission to persistently quickly. Reply
wonderful post, very informative. I wonder why the other specialists of this sector don’t notice this. You should continue your writing. I’m confident, you’ve a gret readers’ baase already! Reply
Fantastic website. Plenty of helpful information here. I’m sending it to some buddies ans also sharing in delicious. And naturally, thanks on your sweat! Reply
Thanks on your marvelous posting! I certainly enjoyed reading it, you happen to be a great author. I will remember to bookmark your blog and will often come back later in life. I want to encourage you to continue your great posts, have a nice holiday weekend! Reply
Thank you for some other great article. Where else may anyone get that kind of information in such a perfect approach of writing? I’ve a presentation next week, and I am on the search for such info. Reply