summaryrefslogtreecommitdiff
path: root/lib/Travelynx/Command/dumpstops.pm
blob: 79fb6ac064a93a73bd782ebf43f299cf484f50fd (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
package Travelynx::Command::dumpstops;

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

use Mojo::Base 'Mojolicious::Command';
use List::Util qw();
use Text::CSV;

has description => 'Export HAFAS/IRIS stops to CSV';

has usage => sub { shift->extract_usage };

sub run {
	my ( $self, $command, $filename ) = @_;
	my $db = $self->app->pg->db;

	if ( not $command or not $filename ) {
		$self->help;
	}
	elsif ( $command eq 'csv' ) {
		open( my $fh, '>', $filename ) or die("open($filename): $!\n");

		my $csv = Text::CSV->new( { eol => "\r\n" } );
		$csv->combine(qw(name eva lat lon source archived));
		print $fh $csv->string;

		my $iter = $self->app->stations->get_db_iterator;
		while ( my $row = $iter->hash ) {
			$csv->combine( @{$row}{qw{name eva lat lon source archived}} );
			print $fh $csv->string;
		}
		close($fh);
	}
	else {
		$self->help;
	}
}

1;

__END__

=head1 SYNOPSIS

  Usage: index.pl dumpstops <format> <filename>

  Exports known stops to <filename>.
  Right now, only the "csv" format is supported.