diff options
author | Daniel Friesel <derf@finalrewind.org> | 2015-03-12 13:14:27 +0100 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2015-03-12 13:14:27 +0100 |
commit | f6c1b258b4be472933202f47aacd07c347b927af (patch) | |
tree | e0fdb24e5ca947b9c6e8695c885d19a60936a641 | |
parent | 1c672cd0940c5173532b4d02a76c971e5bb473ae (diff) |
Parse URA output with Text::CSV to cope with weird formats (fixes ASEAG backend)
-rw-r--r-- | Build.PL | 1 | ||||
-rw-r--r-- | README | 1 | ||||
-rw-r--r-- | lib/Travel/Status/DE/URA.pm | 10 |
3 files changed, 9 insertions, 3 deletions
@@ -25,6 +25,7 @@ Module::Build->new( 'Getopt::Long' => 0, 'List::MoreUtils' => 0, 'LWP::UserAgent' => 0, + 'Text::CSV' => 0, }, sign => 1, @@ -13,6 +13,7 @@ Dependencies * DateTime::Format::Duration * List::MoreUtils * LWP::UserAgent + * Text::CSV Installation diff --git a/lib/Travel/Status/DE/URA.pm b/lib/Travel/Status/DE/URA.pm index 5c0022f..e2f1c70 100644 --- a/lib/Travel/Status/DE/URA.pm +++ b/lib/Travel/Status/DE/URA.pm @@ -13,6 +13,7 @@ use DateTime; use Encode qw(encode decode); use List::MoreUtils qw(firstval none uniq); use LWP::UserAgent; +use Text::CSV; use Travel::Status::DE::URA::Result; sub new { @@ -60,11 +61,12 @@ sub new { return $self; } - $self->{raw_str} = $response->decoded_content; + $self->{raw_str} = encode( 'UTF-8', $response->decoded_content ); + + # Fix encoding in case we're running through test files if ( substr( $self->{ura_instant_url}, 0, 5 ) eq 'file:' ) { $self->{raw_str} = encode( 'UTF-8', $self->{raw_str} ); } - $self->parse_raw_data; return $self; @@ -72,6 +74,7 @@ sub new { sub parse_raw_data { my ($self) = @_; + my $csv = Text::CSV->new( { binary => 1 } ); for my $dep ( split( /\r\n/, $self->{raw_str} ) ) { $dep =~ s{^\[}{}; @@ -79,7 +82,8 @@ sub parse_raw_data { # first field == 4 => version information, no departure if ( substr( $dep, 0, 1 ) != 4 ) { - my @fields = split( /"?,"?/, $dep ); + $csv->parse($dep); + my @fields = $csv->fields; push( @{ $self->{raw_list} }, \@fields ); push( @{ $self->{stop_names} }, $fields[1] ); } |