summaryrefslogtreecommitdiff
path: root/bin/efa-m
blob: 115f36b7172990f5b1e39528bb771addaa89521d (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
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;

our $VERSION = '0.00';

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

my ( $date, $time, $input_type );
my @output;

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

GetOptions(
	'd|date=s'  => \$date,
	'h|help'    => sub { show_help(0) },
	't|time=s'  => \$time,
	'V|version' => \&show_version,

) or show_help(1);

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

my ( $place, $input ) = @ARGV;

if ( $input =~ s{ ^ (?<type> address|poi|stop) : }{}x ) {
	$input_type = $+{type};
}

my $status = Travel::Status::DE::VRR->new(
	date  => $date,
	place => $place,
	name  => $input,
	time  => $time,
	type  => $input_type,
);

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

	print "Usage: efa-m [-d <dd.mm.yyyy>] [-t <hh:mm>] <city> <station>\n"
	  . "See also: man efa-m\n";

	exit $code;
}

sub show_version {
	say "efa-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 .. 3 ) {
		$line_length[$i] = max map { length( $_->[$i] ) } @lines;
	}

	for my $line (@lines) {

		if ( length( $line->[4] ) ) {
			$line->[4] =~ tr{\n\x0d}{ }s;
			chomp $line->[4];
			print "\n";
			for my $info_line ( split( qr{\n}, $line->[4] ) ) {
				say "# ${info_line}";
			}
		}

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

	return;
}

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

for my $d ( $status->results ) {

	push( @output,
		[ $d->time, $d->platform, $d->line, $d->destination, $d->info ] );
}

display_result(@output);

__END__

=head1 NAME

efa-m - Inofficial interface to the efa.vrr.de departure monitor

=head1 SYNOPSIS

B<efa-m> [B<-d> I<date>] [B<-t> I<time>] I<city> I<stop>

=head1 VERSION

version 0.00

=head1 DESCRIPTION

B<efa-m> lists upcoming tram, bus and train departures at I<stop> in I<city>

=head1 OPTIONS

=over

=item B<-d>, B<--date> I<dd.mm.yyyy>

Show departures for I<date> instead of today

=item B<-t>, B<--time> I<hh:mm>

Show departures starting at I<time> instead of now

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

Show version information.

=back

=head1 EXIT STATUS

Zero.

=head1 CONFIGURATION

None.

=head1 DEPENDENCIES

=over

=item * Class::Accessor(3pm)

=item * XML::LibXML(3pm)

=item * WWW::Mechanize(3pm)

=back

=head1 BUGS AND LIMITATIONS

Unknown.

=head1 AUTHOR

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

=head1 LICENSE

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