Skip to content

Instantly share code, notes, and snippets.

@comotion
Forked from tfheen/gist:893253
Created March 29, 2011 21:26
Show Gist options
  • Save comotion/893349 to your computer and use it in GitHub Desktop.
Save comotion/893349 to your computer and use it in GitHub Desktop.
#! /usr/bin/perl -CSDAL
use strict;
use warnings;
use JSON;
use LWP::Simple;
use POSIX qw(strftime);
use Data::Dumper;
use utf8;
my $placesURI = "http://api-test.trafikanten.no/Place/FindMatchesOfType/%s?placeType=Stop";
my $getTravelAfterByStopsURI = "http://api-test.trafikanten.no/Travel/GetTravelsAfterByStops?fromstops=%s&tostops=%s";
# parse arguments
my $from;
my $to;
if (@ARGV == 2) {
$from = $ARGV[0];
$to = $ARGV[1];
} else {
my $_ = join " ", @ARGV;
($from,$to )= /([-\w\s\d\.]*)[^\w\s\d\-\.]*([-\w\s\d\.]*)/;
print "$from -> $to\n";
}
my @fromplaces = map {$_->{ID}} @{decode_json(get(sprintf($placesURI, $from)))};
my @toplaces = map {$_->{ID}} @{decode_json(get(sprintf($placesURI, $to)))};
my $travels = decode_json(get(sprintf($getTravelAfterByStopsURI,
join(",", @fromplaces),
join(",", @toplaces))));
sub format_date {
my ($format, $date) = (@_);
$date =~ /Date\((\d+)/;
return strftime($format, localtime($1/1000));
}
foreach my $option (@{$travels}) {
print format_date("%Y-%m-%d %H%M - ", $option->{DepartureTime});
print format_date("%H%M\n", $option->{ArrivalTime});
foreach my $stage (@{$option->{TravelStages}}) {
if ($stage->{Destination} ne 'Gangledd') {
printf(" [%s-%s] Linje %s fra %s mot %s til %s\n",
format_date("%H%M", $stage->{DepartureTime}),
format_date("%H%M", $stage->{ArrivalTime}),
$stage->{LineName},
$stage->{DepartureStop}->{Name},
$stage->{Destination},
$stage->{ArrivalStop}->{Name});
} else {
printf(" [%s-%s] Gå fra %s til %s\n",
format_date("%H%M", $stage->{DepartureTime}),
format_date("%H%M", $stage->{ArrivalTime}),
$stage->{DepartureStop}->{Name},
$stage->{ArrivalStop}->{Name});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment