line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Web::SIVA; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
25507
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
41
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
26
|
|
5
|
1
|
|
|
1
|
|
5
|
use Carp; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
99
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
615
|
use version; our $VERSION = qv('0.0.5'); |
|
1
|
|
|
|
|
2414
|
|
|
1
|
|
|
|
|
6
|
|
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
776
|
use Mojo::DOM; |
|
1
|
|
|
|
|
109831
|
|
|
1
|
|
|
|
|
32
|
|
10
|
1
|
|
|
1
|
|
433
|
use LWP::Simple; |
|
1
|
|
|
|
|
42769
|
|
|
1
|
|
|
|
|
10
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our @provincias = qw(al ca ma gr se co hu ja); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our %provincias = map { $_ => 1 } @provincias; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our @meses = qw(ene feb mar abr may jun jul ago sep oct nov dic); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our $base_url = "http://www.juntadeandalucia.es/medioambiente/atmosfera/informes_siva/"; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Module implementation here |
21
|
|
|
|
|
|
|
sub new { |
22
|
1
|
|
|
1
|
1
|
8
|
my $class = shift; |
23
|
1
|
|
33
|
|
|
4
|
my $province = shift || croak "Necesito una provincia"; |
24
|
|
|
|
|
|
|
|
25
|
1
|
|
|
|
|
5
|
return bless { _province => $province}, $class; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub day { |
30
|
4
|
|
|
4
|
1
|
4756
|
my $self = shift; |
31
|
4
|
|
|
|
|
12
|
my ($dia, $mes, $year ) = @_; |
32
|
4
|
|
|
|
|
10
|
my $year_digits = substr($year,2,2); |
33
|
4
|
|
|
|
|
13
|
my $provincia = $self->{'_province'}; |
34
|
4
|
50
|
|
|
|
18
|
if ( ! $provincias{$provincia} ) { |
35
|
0
|
|
|
|
|
0
|
croak "$provincia is not one of the 8 provinces"; |
36
|
|
|
|
|
|
|
} |
37
|
4
|
|
|
|
|
24
|
my $date = sprintf("%02d%02d%02d",$year_digits,$mes,$dia); |
38
|
4
|
|
|
|
|
14
|
my $fecha = sprintf("%04d-%02d-%02d", $year, $mes, $dia ); |
39
|
4
|
|
|
|
|
5
|
my @datos; |
40
|
|
|
|
|
|
|
|
41
|
4
|
100
|
66
|
|
|
43
|
if ( ($year < 2004) || ( $year == 2004 && $mes == 1 && $dia < 11 ) ) { |
|
|
|
100
|
|
|
|
|
|
|
|
66
|
|
|
|
|
42
|
2
|
|
|
|
|
12
|
my $url = $base_url."$meses[$mes-1]$year_digits/n$provincia$date.txt"; |
43
|
2
|
|
|
|
|
12
|
my $content = get( $url ); |
44
|
2
|
50
|
|
|
|
712409
|
if ( $content ) { |
45
|
2
|
|
|
|
|
2720
|
my @tables = split(/\s+\n\s+\n\s+\n/, $content); |
46
|
2
|
|
|
|
|
5
|
shift @tables; # unneeded first row |
47
|
2
|
|
|
|
|
9
|
for my $t (@tables) { |
48
|
8
|
|
|
|
|
656
|
my @lines = split("\n", $t ); |
49
|
8
|
|
|
|
|
31
|
my $this_metadata = { date => $fecha."T00:00" }; |
50
|
8
|
|
|
|
|
11
|
my @metadatos; |
51
|
8
|
|
|
|
|
66
|
push @metadatos, ( $lines[0] =~ /Provincia:\s+(\w+)\s+Estacion:\s+(.+)/ ); |
52
|
8
|
|
|
|
|
43
|
push @metadatos, ( $lines[1] =~ /Municipio:\s+(\w+)\s+Direccion:\s+(.+)/ ); |
53
|
8
|
|
|
|
|
15
|
for my $k (qw(provincia estacion municipio direccion)) { |
54
|
32
|
|
|
|
|
49
|
$this_metadata->{$k} = shift @metadatos; |
55
|
|
|
|
|
|
|
} |
56
|
8
|
|
|
|
|
20
|
for (my $l = 5; $l <= $#lines-4; $l++ ) { |
57
|
1146
|
|
|
|
|
727
|
my %these_medidas = %{$this_metadata}; |
|
1146
|
|
|
|
|
3866
|
|
58
|
1146
|
|
|
|
|
6411
|
my @columnas = split(/\s+/, $lines[$l]); |
59
|
1146
|
|
|
|
|
1088
|
my $fecha_hora = shift @columnas; |
60
|
1146
|
|
|
|
|
3670
|
my ($hora) = ($fecha_hora =~ /(\d+:\d+)/); |
61
|
1146
|
50
|
|
|
|
3028
|
if ( !$hora ) { |
62
|
0
|
|
|
|
|
0
|
carp "Problemas con el formato en $l $lines[$l] $fecha"; |
63
|
0
|
|
|
|
|
0
|
next; |
64
|
|
|
|
|
|
|
} |
65
|
1146
|
|
|
|
|
2455
|
$these_medidas{'date'} =~ s/00:00/$hora/; |
66
|
1146
|
|
|
|
|
1257
|
for my $c (qw(SO2 PART NO2 CO)) { |
67
|
4584
|
|
|
|
|
6025
|
$these_medidas{$c} = shift @columnas; |
68
|
|
|
|
|
|
|
} |
69
|
1146
|
|
|
|
|
3110
|
push @datos, \%these_medidas; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
} else { |
74
|
2
|
|
|
|
|
12
|
my $url = $base_url."$meses[$mes-1]$year_digits/n$provincia$date.htm"; |
75
|
|
|
|
|
|
|
|
76
|
2
|
|
|
|
|
11
|
my $content = get( $url ); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
|
79
|
2
|
50
|
33
|
|
|
392378
|
if ( $content and $content =~ m{$year
|
80
|
2
|
|
|
|
|
29
|
my $dom = Mojo::DOM->new( $content ); |
81
|
|
|
|
|
|
|
|
82
|
2
|
|
|
|
|
605257
|
my @tables = $dom->find('table')->each; |
83
|
|
|
|
|
|
|
|
84
|
2
|
|
|
|
|
105407
|
shift @tables; #Primera tabla con leyenda |
85
|
|
|
|
|
|
|
|
86
|
2
|
|
|
|
|
19
|
while ( @tables ) { |
87
|
8
|
|
|
|
|
14
|
my $metadatos = shift @tables; |
88
|
8
|
50
|
|
|
|
55
|
next if !@tables; |
89
|
8
|
|
|
|
|
16
|
my $medidas = shift @tables; |
90
|
|
|
|
|
|
|
|
91
|
8
|
|
|
|
|
40
|
my @metadatos = ( $metadatos =~ /.([A-Z][^<]+)/g); |
92
|
8
|
|
|
|
|
5154
|
my $this_metadata = { date => $fecha }; |
93
|
8
|
|
|
|
|
16
|
for my $k (qw(provincia municipio estacion direccion)) { |
94
|
32
|
|
|
|
|
47
|
$this_metadata->{$k} = shift @metadatos; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
8
|
|
|
|
|
30
|
my @filas = $medidas->find('tr')->each; |
98
|
|
|
|
|
|
|
|
99
|
8
|
|
|
|
|
128348
|
shift @filas; #Cabecera |
100
|
8
|
|
|
|
|
21
|
pop @filas; |
101
|
8
|
|
|
|
|
23
|
for my $f (@filas) { |
102
|
1152
|
|
|
|
|
2697
|
my @columnas = $f->find('td')->map('text')->each; |
103
|
1152
|
|
|
|
|
450333
|
my %these_medidas = %{$this_metadata}; |
|
1152
|
|
|
|
|
4374
|
|
104
|
1152
|
|
|
|
|
1576
|
my $fecha_hora = shift @columnas; |
105
|
1152
|
|
|
|
|
5671
|
my ($hora) = ($fecha_hora =~ /(\d+:\d+)/); |
106
|
1152
|
50
|
|
|
|
2032
|
if ( !$hora ) { |
107
|
0
|
|
|
|
|
0
|
carp "Problemas con el formato en $f $fecha"; |
108
|
|
|
|
|
|
|
} |
109
|
1152
|
|
|
|
|
1804
|
$these_medidas{'date'} =~ s/00:00/$hora/; |
110
|
1152
|
|
|
|
|
1338
|
for my $c (qw(SO2 PART NO2 CO O3)) { |
111
|
5760
|
|
|
|
|
7256
|
$these_medidas{$c} = shift @columnas; |
112
|
|
|
|
|
|
|
} |
113
|
1152
|
|
|
|
|
9852
|
push @datos, \%these_medidas; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
} |
118
|
4
|
|
|
|
|
32
|
return \@datos; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
"We want air"; # Magic true value required at end of module |
122
|
|
|
|
|
|
|
__END__ |