Skip to content

Instantly share code, notes, and snippets.

@mdekstrand
Created February 9, 2011 19:23
Show Gist options
  • Save mdekstrand/819071 to your computer and use it in GitHub Desktop.
Save mdekstrand/819071 to your computer and use it in GitHub Desktop.
Query tool for Metro Transit NexTrip
#!/usr/bin/env perl
=head1 NAME
nextrip.pl - NexTrip query tool
=head1 SYNOPSIS
B<nextrip.pl> [I<OPTIONS>]
=head1 DESCRIPTION
B<nextrip.pl> periodically polls Metro Transit's NexTrip service and
prints the departure times for the next 3 buses to standard output.
It's useful combined with programs like XMobar or dzen, or applets
that show script output in a panel.
In order to use this program, you will need your route number (e.g. 3)
and the stop number (find this at the Interactive Map at
L<http://metrotransit.org/map>).
=head1 OPTIONS
=over 1
=item C<-n> I<n> Print the next I<n> buses (default 3).
=item B<-d> I<delay> Delay for I<delay> seconds after each printout (default 60).
=item B<-r> I<route> Print for route number I<route>.
=item B<-s> I<stop> Display for stop I<stop>.
=back
=head1 NOTES
Errors are printed surrounded in XMobar color tags set to red.
=head1 COPYRIGHT
Copyright (c) 2011 Michael Ekstrand
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
=cut
use strict;
use warnings;
use LWP::UserAgent;
use JSON::XS;
use Getopt::Std;
sub get_trip;
our ($opt_n, $opt_d, $opt_r, $opt_s);
getopts('n:d:r:s:');
my $route = $opt_r;
my $stop = $opt_s;
my $n = 3;
$n = $opt_n if defined $opt_n;
my $delay = 60;
$delay = $opt_d if defined $opt_d;
my $ua = LWP::UserAgent->new;
$ua->agent("nextrip.pl");
$| = 1;
while (1) {
my @deps = eval {get_trip($route, $stop)};
if (@deps) {
my $msg = "Next buses ";
for (my $i = 0; $i < $n && $i <= $#deps; $i++) {
$msg .= ", " if $i > 0;
$msg .= $deps[$i];
}
print "$msg\n";
} elsif ($@) {
print "<fc=red>ERR: $@</fc>\n";
} else {
print "No buses coming.\n";
}
sleep $delay;
}
sub get_trip
{
my ($route, $stop) = @_;
my $url = "http://metrotransit.org/map/NextBusDepartures.ashx?stopid=$stop";
my $r = $ua->get($url);
if ($r ->is_success) {
my $dep = decode_json $r->content;
my @departures = @{$dep->{Departures}};
my @good = grep { $_->{Route} == $route } @departures;
return map { lc $_->{DepartureText} } @good;
} else {
die $r->status_line;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment