#!/usr/bin/perl -w # Copyright 2010 Paul Munday # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see . # or . use LWP::Simple; use XML::Simple; use Time::Local; use File::HomeDir; # config type variables my $appID = "8D8D196B90DA0E177306D3CE8"; $home = File::HomeDir->my_home; # get stop id. if (defined $ARGV[0]) { $stopID = $ARGV[0]; } elsif (-e "$home/.stopid") { open FILE, "$home/.stopid" or die "can't open .stopid"; $stopID = ; close FILE; chomp $stopID; } else { print "Please enter Stop ID number: \n"; chomp ($stopID = <>); } unless ($stopID =~ /^\d+$/){ die "Stop ID is not a number \n stopped" } # arrivals my $url = "http://developer.trimet.org/ws/V1/arrivals/locIDs/$stopID/appID/$appID"; # get xml from trimet my $trimetXML = get $url; die "Couldn't get trimet info :( \n" unless defined $trimetXML; # load xml into hashref my $busInfo = XMLin("$trimetXML", forcearray => [ 'arrival' ]); #Test for valid stop id. unless ( exists $busInfo->{location}->{desc} ){ die " Invalid Stop ID \n"; } #now define some useful variables # where am I (human readable) my $stopLocation = "$busInfo->{location}->{desc}"; #which direction are buses headed in my $stopDirection = lc "$busInfo->{location}->{dir}"; #Test for valid stop id. $length_stopLocation = length ($stopLocation); if ($length_stopLocation == 0 ){ die "Invalid Stop ID 2 "; } print "The next arrivals due at $stopLocation are \n"; foreach my $bus (@{$busInfo->{arrival}}){ print $bus->{fullSign} . " (heading $stopDirection) \n"; if (defined $bus->{estimated}) { my $atime = localtime ($bus->{estimated} / 1000); print "estimated arrival time: ". localtime ($bus->{estimated} / 1000); } else { print "scheduled arrival time: ". localtime ($bus->{scheduled} / 1000); } print " \n"; }