summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBirte Kristina Friesel <derf@finalrewind.org>2024-02-09 19:09:16 +0100
committerBirte Kristina Friesel <derf@finalrewind.org>2024-02-09 19:09:16 +0100
commit39ebc84e5f29c971d6a91594e548299c918b56b5 (patch)
treea95aaa7b9cdb9a606da9622dbbe41cde6848d8eb
parentfe0a8738999e12f53c149cfdb227172959e19cfb (diff)
add dumpstops command2.5.16
-rw-r--r--lib/Travelynx/Command/dumpstops.pm50
-rw-r--r--lib/Travelynx/Model/Stations.pm6
2 files changed, 56 insertions, 0 deletions
diff --git a/lib/Travelynx/Command/dumpstops.pm b/lib/Travelynx/Command/dumpstops.pm
new file mode 100644
index 0000000..79fb6ac
--- /dev/null
+++ b/lib/Travelynx/Command/dumpstops.pm
@@ -0,0 +1,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.
diff --git a/lib/Travelynx/Model/Stations.pm b/lib/Travelynx/Model/Stations.pm
index 5bbe781..ac4019c 100644
--- a/lib/Travelynx/Model/Stations.pm
+++ b/lib/Travelynx/Model/Stations.pm
@@ -71,6 +71,12 @@ sub add_meta {
}
}
+sub get_db_iterator {
+ my ($self) = @_;
+
+ return $self->{pg}->db->select( 'stations', '*' );
+}
+
sub get_meta {
my ( $self, %opt ) = @_;
my $db = $opt{db} // $self->{pg}->db;