blib/script/mosmix-import.pl untested

filter: Statementstmt 0.0% 0 / 57 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 14 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 94 CC 2 SCAR 10.6
CC
2
Cov
0%
status
6.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.9
1 #!/usr/local/bin/perl
2 use strict;
3 use warnings;
4 use Weather::MOSMIX::Writer;
5 use Weather::MOSMIX::Reader;
6  
7 use HTTP::Tiny;
8 use File::Temp 'tempfile';
9  
10 use Getopt::Long;
11  
12 our $VERSION = '0.02';
13  
14 GetOptions(
15 'create' => \my $create,
16 'import' => \my $import,
17 'fetch' => \my $fetch,
18  
19 'dsn=s' => \my $dsn,
20  
21 'verbose' => \my $verbose,
22 );
23  
24 $dsn ||= 'dbi:SQLite:dbname=mosmix-forecast.sqlite';
25  
26 sub status {
27 if( $verbose ) {
28 print "@_\n";
29 };
30 };
31  
32 my %actions;
33  
34 if( @ARGV) {
35 $import = 1;
36 };
37  
38 if( ! ($create || $import || $fetch )) {
39 $fetch = 1;
40 $import = 1;
41 };
42 $actions{ create } = $create;
43 $actions{ import } = $import;
44 $actions{ fetch } = $fetch;
45 my @files = @ARGV;
46  
47 my $w;
48 if( $actions{ create }) {
49 $w ||= Weather::MOSMIX::Writer->new();
50 $w->create_db(
51 dsn => $dsn
52 );
53 }
54  
55 if( $actions{ fetch }) {
56 my $base = 'https://opendata.dwd.de/weather/local_forecasts/mos/MOSMIX_S/all_stations/kml/MOSMIX_S_LATEST_240.kmz';
57 status( "Fetching $base" );
58  
59 my $ua = HTTP::Tiny->new();
60 my( $fh, $name ) = tempfile();
61 close $fh;
62  
63 my $res = $ua->mirror($base => $name);
64  
65 if( ! $res->{success}) {
66 die $res->{message};
67 };
68 status( join " ", "Fetched", -s($name), "bytes to $name" );
69  
70 push @files, $name;
71 };
72  
73 if( $actions{ import }) {
74 $w ||= Weather::MOSMIX::Writer->new(
75 dbh => {
76 dsn => $dsn,
77 }
78 );
79 my $r = Weather::MOSMIX::Reader->new(
80 writer => $w,
81 );
82  
83 for my $file (@files) {
84 status("Importing $file\n");
85 $r->read_zip( $file );
86 };
87 };