blib/lib/Web/SIVA.pm untested

filter: Statementstmt 0.0% 0 / 98 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 14 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 136 SCAR 42.8
CC
8
Cov
0%
day
72.0
new
2.0
BEGIN
2.0
CRAP 72.0
1 package Web::SIVA;
2  
3 use warnings;
4 use strict;
5 use Carp;
6  
7 use version; our $VERSION = qv('0.0.5');
8  
9 use Mojo::DOM;
10 use LWP::Simple;
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 my $class = shift;
23 my $province = shift || croak "Necesito una provincia";
24
25 return bless { _province => $province}, $class;
26  
27 }
28  
29 sub day {
30 my $self = shift;
31 my ($dia, $mes, $year ) = @_;
32 my $year_digits = substr($year,2,2);
33 my $provincia = $self->{'_province'};
34 if ( ! $provincias{$provincia} ) {
35 croak "$provincia is not one of the 8 provinces";
36 }
37 my $date = sprintf("%02d%02d%02d",$year_digits,$mes,$dia);
38 my $fecha = sprintf("%04d-%02d-%02d", $year, $mes, $dia );
39 my @datos;
40
41 if ( ($year < 2004) || ( $year == 2004 && $mes == 1 && $dia < 11 ) ) {
42 my $url = $base_url."$meses[$mes-1]$year_digits/n$provincia$date.txt";
43 my $content = get( $url );
44 if ( $content ) {
45 my @tables = split(/\s+\n\s+\n\s+\n/, $content);
46 shift @tables; # unneeded first row
47 for my $t (@tables) {
48 my @lines = split("\n", $t );
49 my $this_metadata = { date => $fecha."T00:00" };
50 my @metadatos;
51 push @metadatos, ( $lines[0] =~ /Provincia:\s+(\w+)\s+Estacion:\s+(.+)/ );
52 push @metadatos, ( $lines[1] =~ /Municipio:\s+(\w+)\s+Direccion:\s+(.+)/ );
53 for my $k (qw(provincia estacion municipio direccion)) {
54 $this_metadata->{$k} = shift @metadatos;
55 }
56 for (my $l = 5; $l <= $#lines-4; $l++ ) {
57 my %these_medidas = %{$this_metadata};
58 my @columnas = split(/\s+/, $lines[$l]);
59 my $fecha_hora = shift @columnas;
60 my ($hora) = ($fecha_hora =~ /(\d+:\d+)/);
61 if ( !$hora ) {
62 carp "Problemas con el formato en $l $lines[$l] $fecha";
63 next;
64 }
65 $these_medidas{'date'} =~ s/00:00/$hora/;
66 for my $c (qw(SO2 PART NO2 CO)) {
67 $these_medidas{$c} = shift @columnas;
68 }
69 push @datos, \%these_medidas;
70 }
71 }
72 }
73 } else {
74 my $url = $base_url."$meses[$mes-1]$year_digits/n$provincia$date.htm";
75
76 my $content = get( $url );
77
78
79 if ( $content and $content =~ m{$year</title} ) {
80 my $dom = Mojo::DOM->new( $content );
81
82 my @tables = $dom->find('table')->each;
83
84 shift @tables; #Primera tabla con leyenda
85
86 while ( @tables ) {
87 my $metadatos = shift @tables;
88 next if !@tables;
89 my $medidas = shift @tables;
90
91 my @metadatos = ( $metadatos =~ /<b>.([A-Z][^<]+)/g);
92 my $this_metadata = { date => $fecha };
93 for my $k (qw(provincia municipio estacion direccion)) {
94 $this_metadata->{$k} = shift @metadatos;
95 }
96
97 my @filas = $medidas->find('tr')->each;
98
99 shift @filas; #Cabecera
100 pop @filas;
101 for my $f (@filas) {
102 my @columnas = $f->find('td')->map('text')->each;
103 my %these_medidas = %{$this_metadata};
104 my $fecha_hora = shift @columnas;
105 my ($hora) = ($fecha_hora =~ /(\d+:\d+)/);
106 if ( !$hora ) {
107 carp "Problemas con el formato en $f $fecha";
108 }
109 $these_medidas{'date'} =~ s/00:00/$hora/;
110 for my $c (qw(SO2 PART NO2 CO O3)) {
111 $these_medidas{$c} = shift @columnas;
112 }
113 push @datos, \%these_medidas;
114 }
115 }
116 }
117 }
118 return \@datos;
119 }
120  
121 "We want air"; # Magic true value required at end of module
122 __END__