summaryrefslogtreecommitdiff
path: root/lib/Travelynx/Helper/IRIS.pm
blob: deed79a88ef900f7169bf0573015a71b5bf25bfb (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
package Travelynx::Helper::IRIS;

# Copyright (C) 2020-2023 Birte Kristina Friesel
#
# SPDX-License-Identifier: AGPL-3.0-or-later

use strict;
use warnings;
use 5.020;

use utf8;

use Mojo::Promise;
use Mojo::UserAgent;
use Travel::Status::DE::IRIS;
use Travel::Status::DE::IRIS::Stations;

sub new {
	my ( $class, %opt ) = @_;

	return bless( \%opt, $class );
}

sub get_departures {
	my ( $self, %opt ) = @_;
	my $station      = $opt{station};
	my $lookbehind   = $opt{lookbehind}   // 180;
	my $lookahead    = $opt{lookahead}    // 30;
	my $with_related = $opt{with_related} // 0;

	# Berlin Hbf exists twice:
	# - BLS / 8011160
	# - BL / 8098160 (formerly "Berlin Hbf (tief)")
	# Right now, travelynx assumes that station name -> EVA / DS100 is a unique
	# map.  This is not the case. Work around it here until travelynx has been
	# adjusted properly.
	if ( $station eq 'Berlin Hbf' or $station eq '8011160' ) {
		$with_related = 1;
	}

	my @station_matches
	  = Travel::Status::DE::IRIS::Stations::get_station($station);

	if ( @station_matches == 1 ) {
		$station = $station_matches[0][0];
		my $status = Travel::Status::DE::IRIS->new(
			station        => $station,
			main_cache     => $self->{main_cache},
			realtime_cache => $self->{realtime_cache},
			keep_transfers => 1,
			lookbehind     => 20,
			datetime       => DateTime->now( time_zone => 'Europe/Berlin' )
			  ->subtract( minutes => $lookbehind ),
			lookahead   => $lookbehind + $lookahead,
			lwp_options => {
				timeout => 10,
				agent   => 'travelynx/'
				  . $self->{version}
				  . ' +https://travelynx.de',
			},
			with_related => $with_related,
		);
		return {
			results       => [ $status->results ],
			errstr        => $status->errstr,
			station_ds100 =>
			  ( $status->station ? $status->station->{ds100} : undef ),
			station_eva =>
			  ( $status->station ? $status->station->{uic} : undef ),
			station_name =>
			  ( $status->station ? $status->station->{name} : undef ),
			related_stations => [ $status->related_stations ],
		};
	}
	elsif ( @station_matches > 1 ) {
		return {
			results => [],
			errstr  =>
			  "Mehrdeutiger Stationsname: '$station'. Mögliche Eingaben: "
			  . join( q{, }, map { $_->[1] } @station_matches ),
		};
	}
	else {
		return {
			results => [],
			errstr  => 'Unbekannte Station',
		};
	}
}

sub get_departures_p {
	my ( $self, %opt ) = @_;
	my $station      = $opt{station};
	my $lookbehind   = $opt{lookbehind}   // 180;
	my $lookahead    = $opt{lookahead}    // 30;
	my $with_related = $opt{with_related} // 0;

	# Berlin Hbf exists twice:
	# - BLS / 8011160
	# - BL / 8098160 (formerly "Berlin Hbf (tief)")
	# Right now, travelynx assumes that station name -> EVA / DS100 is a unique
	# map.  This is not the case. Work around it here until travelynx has been
	# adjusted properly.
	if ( $station eq 'Berlin Hbf' or $station eq '8011160' ) {
		$with_related = 1;
	}

	my @station_matches
	  = Travel::Status::DE::IRIS::Stations::get_station($station);

	if ( @station_matches == 1 ) {
		$station = $station_matches[0][0];
		my $promise = Mojo::Promise->new;
		Travel::Status::DE::IRIS->new_p(
			station        => $station,
			main_cache     => $self->{main_cache},
			realtime_cache => $self->{realtime_cache},
			keep_transfers => 1,
			lookbehind     => 20,
			datetime       => DateTime->now( time_zone => 'Europe/Berlin' )
			  ->subtract( minutes => $lookbehind ),
			lookahead   => $lookbehind + $lookahead,
			lwp_options => {
				timeout => 10,
				agent   => 'travelynx/'
				  . $self->{version}
				  . ' +https://travelynx.de',
			},
			with_related => $with_related,
			promise      => 'Mojo::Promise',
			user_agent   => Mojo::UserAgent->new,
			get_station  => \&Travel::Status::DE::IRIS::Stations::get_station,
			meta         => Travel::Status::DE::IRIS::Stations::get_meta(),
		)->then(
			sub {
				my ($status) = @_;
				$promise->resolve(
					{
						results       => [ $status->results ],
						errstr        => $status->errstr,
						station_ds100 => (
							  $status->station
							? $status->station->{ds100}
							: undef
						),
						station_eva => (
							$status->station ? $status->station->{uic} : undef
						),
						station_name => (
							$status->station ? $status->station->{name} : undef
						),
						related_stations => [ $status->related_stations ],
					}
				);
				return;
			}
		)->catch(
			sub {
				my ($err) = @_;
				$promise->reject(
					$err,
					{
						results => [],
						errstr  => "Error in promise: $err",
					}
				);
				return;
			}
		)->wait;
		return $promise;
	}
	elsif ( @station_matches > 1 ) {
		return Mojo::Promise->reject(
			'ambiguous station name',
			{
				results     => [],
				errstr      => "Mehrdeutiger Stationsname: '$station'",
				suggestions => [
					map { { name => $_->[1], eva => $_->[2] } }
					  @station_matches
				],
			}
		);
	}
	else {
		return Mojo::Promise->reject(
			'unknown station',
			{
				results => [],
				errstr  => 'Unbekannte Station',
			}
		);
	}
}

sub route_diff {
	my ( $self, $train ) = @_;
	my @json_route;
	my @route       = $train->route;
	my @sched_route = $train->sched_route;

	my $route_idx = 0;
	my $sched_idx = 0;

	while ( $route_idx <= $#route and $sched_idx <= $#sched_route ) {
		if ( $route[$route_idx] eq $sched_route[$sched_idx] ) {
			push( @json_route, [ $route[$route_idx], undef, {} ] );
			$route_idx++;
			$sched_idx++;
		}

		# this branch is inefficient, but won't be taken frequently
		elsif ( not( grep { $_ eq $route[$route_idx] } @sched_route ) ) {
			push( @json_route,
				[ $route[$route_idx], undef, { isAdditional => 1 } ], );
			$route_idx++;
		}
		else {
			push( @json_route,
				[ $sched_route[$sched_idx], undef, { isCancelled => 1 } ], );
			$sched_idx++;
		}
	}
	while ( $route_idx <= $#route ) {
		push( @json_route,
			[ $route[$route_idx], undef, { isAdditional => 1 } ], );
		$route_idx++;
	}
	while ( $sched_idx <= $#sched_route ) {
		push( @json_route,
			[ $sched_route[$sched_idx], undef, { isCancelled => 1 } ], );
		$sched_idx++;
	}
	return @json_route;
}

1;