summaryrefslogtreecommitdiff
path: root/bin/aseag-m
blob: af3bd94a06e3ad8bf2f0f1ec7bc5568b4bfa2cc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;

no if $] >= 5.018, warnings => "experimental::smartmatch";

our $VERSION = '0.00';

binmode( STDOUT, ':encoding(utf-8)' );

use Getopt::Long qw(:config no_ignore_case);
use List::Util qw(max);
use Travel::Status::DE::ASEAG;

my (@grep_lines);
my $full_route = 0;
my $hide_past  = 1;

GetOptions(
	'a|route-after'  => sub { $full_route = 'after' },
	'b|route-before' => sub { $full_route = 'before' },
	'h|help'         => sub { show_help(0) },
	'f|full-route'   => \$full_route,
	'l|line=s@'      => \@grep_lines,
	'p|with-past'    => sub { $hide_past  = 0 },
	'V|version'      => \&show_version,

) or show_help(1);

if ( @ARGV != 1 ) {
	show_help(1);
}

# --line=foo,bar support
@grep_lines = split( qr{,}, join( q{,}, @grep_lines ) );

my ($stop_name) = @ARGV;

my $status = Travel::Status::DE::ASEAG->new;

sub show_help {
	my ($code) = @_;

	print "Usage: B<aseag-m> [ B<-l> I<lines> ] [B<-f>] I< <stop name>\n"
	  . "See also: man aseag-m\n";

	exit $code;
}

sub show_version {
	say "aseag-m version ${VERSION}";

	exit 0;
}

sub display_result {
	my (@lines) = @_;

	my @line_length;

	if ( not @lines ) {
		die("Nothing to show\n");
	}

	for my $i ( 0 .. 2 ) {
		$line_length[$i] = max map { length( $_->[$i] ) } @lines;
	}

	for my $line (@lines) {

		printf(
			join( q{  }, ( map { "%-${_}s" } @line_length ) ) . "\n",
			@{$line}[ 0 .. 2 ]
		);

		if ($full_route) {
			print "\n" . $line->[3] . "\n\n\n";
		}
	}

	return;
}

sub show_results {
	my @output;

	for my $d (
		$status->results(
			full_routes => $full_route,
			hide_past   => $hide_past,
			stop        => $stop_name,
		)
	  )
	{

		my $dtime = $d->time;

		if ( ( @grep_lines and not( $d->line ~~ \@grep_lines ) ) ) {
			next;
		}

		push(
			@output,
			[
				$dtime,
				$d->line,
				$d->destination,
				join( "\n",
					map { sprintf( '%-8s  %s', @{$_} ) }
					  @{ $d->route_timetable } )
			]
		);
	}

	display_result(@output);

	return;
}

if ( my $err = $status->errstr ) {
	say STDERR "Request error: ${err}";
	exit 2;
}

show_results();

__END__

=head1 NAME

aseag-m - Unofficial interface to the ASEAG departure monitor

=head1 SYNOPSIS

B<aseag-m> I<name>

=head1 VERSION

version 1.04

=head1 DESCRIPTION

B<aseag-m> lists upcoming bus departures at the ASEAG stop I<name>.

=head1 OPTIONS

=over

=item B<-f>, B<--full-route>

Display complete routes (including arrival times) of all buses.

=item B<-l>, B<--line> I<lines>

Only show departures of I<lines> (comma-separatad list, option may be
repeated)

=item B<-V>, B<--version>

Show version information.

=back

=head1 EXIT STATUS

Zero.

=head1 CONFIGURATION

None.

=head1 DEPENDENCIES

=over

=item * Class::Accessor(3pm)

=item * DateTime(3pm)

=item * LWP::UserAgent(3pm)

=back

=head1 BUGS AND LIMITATIONS

Unknown.

=head1 AUTHOR

Copyright (C) 2013 by Daniel Friesel E<lt>derf@finalrewind.orgE<gt>

=head1 LICENSE

This program is licensed under the same terms as Perl itself.