script/mosmix-import.pl untested

filter: Statementstmt 0.0% 0 / 62 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 109 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.04';
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 my $delete;
26 my @delete;
27  
28 sub status {
29 if( $verbose ) {
30 print "@_\n";
31 };
32 };
33  
34 my %actions;
35  
36 if( @ARGV) {
37 $import = 1;
38 };
39  
40 if( ! ($create || $import || $fetch )) {
41 $fetch = 1;
42 $import = 1;
43 $delete = 1;
44 };
45 $actions{ create } = $create;
46 $actions{ import } = $import;
47 $actions{ fetch } = $fetch;
48 my @files = @ARGV;
49  
50 my $w;
51 if( $actions{ create }) {
52 $w ||= Weather::MOSMIX::Writer->new();
53 $w->create_db(
54 dsn => $dsn
55 );
56 }
57  
58 if( $actions{ fetch }) {
59 my $base = 'https://opendata.dwd.de/weather/local_forecasts/mos/MOSMIX_S/all_stations/kml/MOSMIX_S_LATEST_240.kmz';
60 status( "Fetching $base" );
61  
62 my $ua = HTTP::Tiny->new();
63 my( $fh, $name ) = tempfile();
64 close $fh;
65  
66 my $res = $ua->mirror($base => $name);
67  
68 if( ! $res->{success}) {
69 die $res->{message};
70 };
71 status( join " ", "Fetched", -s($name), "bytes to $name" );
72  
73 push @files, $name;
74 push @delete, $name if $delete
75 };
76  
77 if( $actions{ import }) {
78 $w ||= Weather::MOSMIX::Writer->new(
79 dbh => {
80 dsn => $dsn,
81 }
82 );
83 my $r = Weather::MOSMIX::Reader->new(
84 writer => $w,
85 );
86  
87 for my $file (@files) {
88 status("Importing $file\n");
89 $r->read_zip( $file );
90 };
91 }
92 unlink @delete;