blib/lib/Weather/MOSMIX/Reader.pm

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