lib/Weather/MOSMIX/Reader.pm

filter: Statementstmt 50.5% 43 / 85 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 63.1% 12 / 19 Podpod 66.6% 4 / 6 totaltotal 50.8% 59 / 116 CC 7 SCAR 13.2
CC
7
Cov
47%
handle_place
12.0
file_expiry
6.0
open_zip
6.0
CRAP 3.7
« lib/Weather/MOSMIX.pm ↑ Summary lib/Weather/MOSMIX/Writer.pm »
1 package Weather::MOSMIX::Reader;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Moo 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Archive::Zip;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use Archive::Zip;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use PerlIO::gzip;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Weather::MOSMIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Weather::MOSMIX::Writer;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 NAME
17  
18 Weather::MOSMIX::Read - Read MOSMIX weather forecast data
19  
20 =head1 SYNOPSIS
21  
22 This reads and parses the XML from the compressed C<.kmz> file and writes it
23 to an SQLite database:
24  
25 my $w = Weather::MOSMIX::Writer->new(
26 dsn => 'dbi:SQLite:dbname=db/forecast.sqlite',
27 );
28 my $r = Weather::MOSMIX::Reader->new(
29 writer => $w,
30 );
31  
32 for my $file (@files) {
33 status("Importing $file\n");
34 $r->read_zip( $file );
35 };
36  
37 =cut
38  
39 2 our $VERSION = '0.04';
40  
41 =head1 METHODS
42  
43 =head2 C<< Weather::MOSMIX::Reader->new() >>
44  
45 =cut
46  
47 =head1 ACCESSORS
48  
49 =head2 C<< twig >>
50  
51 =cut
52  
53 has 'twig' => (
54 is => 'lazy',
55 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=1.2
56 2 my( $self ) = @_;
57 2 weaken $self;
58 2 require XML::Twig;
59 XML::Twig->new(
60 no_xxe => 1,
61 keep_spaces => 1,
62 twig_handlers => {
63  
64 # Set the expiry from the issue time
65 0 ▶ 'dwd:IssueTime' => sub { $self->handle_issuetime( $_[0], $_[1] ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 0 ▶ 'kml:Placemark' => sub { $self->handle_place( $_[0], $_[1] ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 },
68 )
69 0 },
70 2 );
71  
72 =head2 C<< expiry >>
73  
74 =cut
75  
76 # Ugly global attributes to store state while parsing:-/
77 2 has 'expiry' => (
78 is => 'rw',
79 );
80  
81 =head2 C<< issuetime >>
82  
83 =cut
84  
85 2 has 'issuetime' => (
86 is => 'rw',
87 );
88  
89 =head2 C<< writer >>
90  
91 =cut
92  
93 2 has 'writer' => (
94 is => 'ro',
95 );
96  
97 =head1 METHODS
98  
99 =head2 C<< ->file_expiry >>
100  
101 =cut
102  
103 0 ▶sub file_expiry( $self, $filename ) {
Subroutine file_expiry: not called CC=2 SCAR=17.9
Pod documented
104 0 ▶ if( $filename =~ m/MOSMIX_S_(20\d\d)(\d\d)(\d\d)(\d\d)_/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ /MOSMIX_S_(20\d\d)(\d\d)(\d\d)(\d\d)_/) 0 0 0
105 0 my $d = $3 +1; # we'll hang onto the data for 24 hours
106 0 "$1-$2-${d}T$4:00:00Z"
107 };
108 }
109  
110 =head2 C<< ->open_zip >>
111  
112 my $fh = $reader->open_zip(
113 zip_name =>
114 contained_name =>
115  
116 Opens the first file from a C<.kmz> file and returns an unzipping filehandle
117 so the data can be streamed in. If no C<contained_name> is given, the first
118 file will be read.
119  
120 =cut
121  
122 0 ▶sub open_zip( $self, %options ) {
Subroutine open_zip: not called CC=2 SCAR=17.9
Pod documented
123 0 my $reader = Archive::Zip->new( $options{ zip_name } );
124  
125 0 my $stream;
126 0 ▶ if( my $fn = $options{ contained_name }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fn = $options{'contained_name'}) { } 0 0 0
127 0 $stream = $reader->memberNamed( $fn )->fh;
128 } else {
129 0 my @members = $reader->members;
130 0 $members[0]->rewindData();
131 0 $stream = $members[0]->fh;
132 };
133 0 binmode $stream => ':gzip(none)';
134 0 return $stream;
135 }
136  
137 =head2 C<< ->read_zip >>
138  
139 $reader->read_zip( $kmzfile );
140  
141 Opens the C<.kmz> file and parses the KML data in the first contained file.
142  
143 =cut
144  
145 # This could be in its own module?! IO::ReadZipContent ?
146 0 ▶sub read_zip( $self, $filename, $expiry=$self->file_expiry($filename) ) {
Subroutine read_zip: not called CC=2 SCAR=17.9
Pod documented
147 0 my $stream = $self->open_zip( zip_name => $filename );
148 0 $self->parse_fh($stream, $expiry);
149 }
150  
151 =head2 C<< ->parse_fh >>
152  
153 $reader->parse_fh( $xml_fh, expiry => '2020-02-15T14:00:00Z );
154  
155 Parses the KML data streaming from the filehandle. The optional C<expiry>
156 option can be used to pass an expiry date.
157  
158 =cut
159  
160 2 ▶sub parse_fh( $self, $fh, $expiry=undef ) {
Subroutine parse_fh: called CC=2 SCAR=7.1
Pod documented
161 2 $self->writer->start;
162 2 $self->expiry($expiry);
163 2 $self->twig->parse($fh);
164 0 $self->writer->commit;
165 }
166  
167 0 ▶sub handle_issuetime( $self, $twig, $issuetime ) {
Subroutine handle_issuetime: not called CC=1 SCAR=6.9
Pod undocumented
168 0 my $exp = $issuetime->text();
169 0 $exp =~ s!\.000Z!!;
170 0 my $issued = Time::Piece->strptime($exp,$Weather::MOSMIX::TIMESTAMP);
171 0 $self->issuetime($issued.'Z');
172  
173 0 my $e = $issued->new();
174 0 $issued = $issued->strftime($Weather::MOSMIX::TIMESTAMP);
175 0 $self->issuetime($issued.'Z');
176  
177 0 $e += 24*60*60; # expire after 24 hours
178 0 $e = $e->strftime($Weather::MOSMIX::TIMESTAMP).'Z';
179 0 $self->expiry($e);
180 };
181  
182 0 ▶sub handle_place( $self, $twig, $place ) {
Subroutine handle_place: not called CC=3 SCAR=24.8
Pod undocumented
183 0 my $description = $place->first_child_text('kml:description');
184  
185 0 my ($long,$lat,$el) = split /,/, $place->first_descendant('kml:coordinates')->text;
186  
187 # filter for
188 # "ww" - significant weather
189 # "TTT" - temperature 2m above ground
190 my @forecasts = (
191 0 grep { $_->{type} =~ /^(ww|TTT)$/ }
192 0 map {+{ type => $_->att('dwd:elementName'), values => $_->first_descendant('dwd:value')->text }}
193 0 map {; $_->descendants('dwd:Forecast') } $place->descendants('kml:ExtendedData') );
194 0 for (@forecasts) {
195 0 ▶ $_->{values} = [ map { $_ eq '-' ? undef : $_ } split /\s+/, $_->{values} ];
Branch: 1 branch
BranchTrueFalseTotal
$_ eq '-' ? : 0 0 0
196 };
197  
198 0 my %info = (
199 name => scalar $place->first_child_text('kml:name'),
200 description => scalar $place->first_child_text('kml:description'),
201 longitude => $long,
202 latitude => $lat,
203 elevation => $el,
204 forecasts => \@forecasts,
205 issuetime => $self->issuetime,
206 );
207 0 $self->writer->insert( $self->expiry, \%info );
208  
209 0 $place->purge;
210  
211 0 $twig->purge;
212 };
213  
214 2 1;
215  
216 =head1 REPOSITORY
217  
218 The public repository of this module is
219 L<https://github.com/Corion/weather-mosmix>.
220  
221 =head1 SUPPORT
222  
223 The public support forum of this module is L<https://perlmonks.org/>.
224  
225 =head1 BUG TRACKER
226  
227 Please report bugs in this module via the RT CPAN bug queue at
228 L<https://rt.cpan.org/Public/Dist/Display.html?Name=Weather-MOSMIX>
229 or via mail to L<www-Weather-MOSMIX@rt.cpan.org|mailto:Weather-MOSMIX@rt.cpan.org>.
230  
231 =head1 AUTHOR
232  
233 Max Maischein C<corion@cpan.org>
234  
235 =head1 COPYRIGHT (c)
236  
237 Copyright 2019-2020 by Max Maischein C<corion@cpan.org>.
238  
239 =head1 LICENSE
240  
241 This module is released under the same terms as Perl itself.
242  
243 =cut
« lib/Weather/MOSMIX.pm ↑ Summary lib/Weather/MOSMIX/Writer.pm »