summaryrefslogtreecommitdiff
path: root/bin/aseag-m
blob: 7edb7d61c982d4de92458724966e4894fa47c978 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;

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

our $VERSION = '0.04';

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

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

my (@grep_lines);
my $hide_past       = 1;
my $strftime_format = '%H:%M:%S';
my $strfrel_format  = '%M min';
my ( %edata, @edata_pre );
my $calculate_routes = 0;
my $via;

GetOptions(
	'h|help'      => sub { show_help(0) },
	'l|line=s@'   => \@grep_lines,
	'o|output=s@' => \@edata_pre,
	'p|with-past' => sub { $hide_past = 0 },
	's|strftime=s' => \$strftime_format,
	'S|strfrel=s'  => \$strfrel_format,
	'v|via=s'      => \$via,
	'V|version'    => \&show_version,

) or show_help(1);

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

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

for my $efield (@edata_pre) {
	given ($efield) {
		when ('a') { $edata{route_after}       = 1; $calculate_routes = 1 }
		when ('b') { $edata{route_before}      = 1; $calculate_routes = 1 }
		when ('f') { $edata{route_full}        = 1; $calculate_routes = 1 }
		when ('r') { $edata{route_interesting} = 1; $calculate_routes = 1 }
		when ('T') { $edata{relative_times}    = 1 }
		default    { $edata{$efield}           = 1 }
	}
}

my ($stop_name) = @ARGV;

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

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

	print 'Usage: aseag-m [-pV] [-o <output>] [-l <lines>] [-v <stopname>] '
	  . "<stopname>\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 @format = qw(%- %- %- %-);

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

	if ( $edata{relative_times} ) {
		$format[0] = q{%};
	}

	for my $i ( 0 .. 3 ) {
		$format[$i] .= max map { length( $_->[$i] ) } @lines;
		$format[$i] .= 's';
	}

	for my $line (@lines) {

		printf( join( q{  }, @format ) . "\n", @{$line}[ 0 .. 3 ] );

		if ( @{ $line->[4] } ) {
			for my $route ( @{ $line->[4] } ) {
				printf( join( q{  }, @format ) . "\n", @{$route} );
			}
			print "\n";
		}
	}

	return;
}

sub get_exact_stop_name {
	my ($fuzzy_name) = @_;

	my @stops = $status->get_stop_by_name($fuzzy_name);

	if ( @stops == 0 ) {
		say STDERR "Got no departures for '$fuzzy_name'";
		say STDERR 'The stop may not exist or not be in service right now';
		exit(3);
	}
	elsif ( @stops == 1 ) {
		return $stops[0];
	}
	else {
		say STDERR "The stop name '$fuzzy_name' is ambiguous. Please choose "
		  . 'one of the following:';
		say STDERR join( "\n", @stops );
		exit(3);
	}
}

sub show_route {
	my ( $dt_now, $dt_format, @routes ) = @_;
	my @res;

	if ( $edata{relative_times} ) {
		@res = map {
			[
				$dt_format->format_duration(
					$_->[0]->subtract_datetime($dt_now)
				),
				q{},
				$_->[1]
			]
		} @routes;
	}
	else {
		@res = map { [ $_->[0]->strftime($strftime_format), q{}, $_->[1] ] }
		  @routes;
	}

	return @res;
}

sub show_results {
	my @output;

	my $dt_now = DateTime->now( time_zone => 'Europe/Berlin' );
	my $dt_format
	  = DateTime::Format::Duration->new( pattern => $strfrel_format );

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

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

		if ( $edata{route_full} ) {
			@route = ( $d->route_pre, $d->route_post );
		}
		elsif ( $edata{route_after} ) {
			@route = $d->route_post;
		}
		elsif ( $edata{route_before} ) {
			@route = $d->route_pre;
		}

		if ( $edata{relative_times} ) {
			@line = (
				$dt_format->format_duration(
					$d->datetime->subtract_datetime($dt_now)
				),
				$d->line,
				q{},
				$d->destination,
				[ show_route( $dt_now, $dt_format, @route ) ],
			);
		}
		else {
			@line = (
				$d->datetime->strftime($strftime_format),
				$d->line, q{}, $d->destination,
				[ show_route( $dt_now, $dt_format, @route ) ],
			);
		}

		if ( $edata{route_before} ) {
			@{ $line[4] } = reverse @{ $line[4] };
		}

		if ( $edata{route_interesting} ) {
			$line[2] = join( q{  }, $d->route_interesting );
		}

		push( @output, \@line );
	}

	display_result(@output);

	return;
}

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

$stop_name = get_exact_stop_name($stop_name);
if ($via) {
	$via = get_exact_stop_name($via);
}
show_results();

__END__

=head1 NAME

aseag-m - Unofficial interface to the ASEAG departure monitor

=head1 SYNOPSIS

B<aseag-m> [B<-pV>] [B<-l> I<lines>] [B<-o> I<outputtypes>]
[B<-s> I<timefmt> | B<-S> I<timefmt>]
[B<-v> I<stopname>] I<stopname>

=head1 VERSION

version 0.04

=head1 DESCRIPTION

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

=head1 OPTIONS

=over

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

Limit output to departures of I<lines> (comma-separated list of line
names, may be used multiple times).

=item B<-o>, B<--output> I<outputtypes>

Format output according to I<outputtypes>. I<outputtypes> is a
comma-separated list and the B<--output> option may be repeated. Each
output type has both a short and a long form, so for instance both
C<< -or,T >> and C<< --output=route_interesting,relative_times >> are valid.

Valid output types are:

=over

=item a / route_after

For each departure, include the route after I<name>. Both stop names and
departure times are shown.

=item b / route_before

For each departure, include the route leading to I<name>. Both stop names and
departure times are shown.

=item f / route_full

For each departure, include the entire route (stop names and departure times).

=item T / relative_times

Show relative times. Applies to departure and route output.

=back

=item B<-p>, B<--with-past>

Include past departures. Applies both to the departure output and to the
route output of B<-a>, B<-b>, B<-f>.

=item B<-s>, B<--strftime> I<format>

Format absolute times in I<format>, applies both to departure and route
output.  See DateTime(3pm) for allowed patterns.

=item B<-S>, B<--strfrel> I<format>

Format relative times in I<format>, only applies when used with B<-r>.
See DateTime::Format::Duration(3pm) for allowed patterns.

=item B<-v>, B<--via> I<stop>

Only show lines which also serve I<stop>. When used with B<-b>,
I<stop> must be in the schedule before I<name>, with B<-f> it may be anywhere,
with B<-a> (and by default) it must be after I<name>.

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

Show version information.

=back

=head1 EXIT STATUS

Normally zero. B<1> means B<aseag-m> was called with invalid options,
B<2> indicates a request error from Travel::Status::DE::URA(3pm),
B<3> a bad (unknown or ambiguous) I<stop> name.

=head1 CONFIGURATION

None.

=head1 DEPENDENCIES

=over

=item * Class::Accessor(3pm)

=item * DateTime(3pm)

=item * DateTime::Format::Duration(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.