#!/usr/bin/perl -w
use v5.10;
use autodie;
use utf8::all;
use strict;
use warnings;
use diagnostics;
use Time::Local;
use Class::Date qw(date now);
use POSIX qw(strftime);
my $fecha = print strftime "%d-%m-%Y\n", localtime;
use LWP::Simple;
my $html = get("http://atudem.org/?option=com_partenieve&task=view&fecha='.$fecha.'#parte");
print "http://atudem.org/?option=com_partenieve&task=view&fecha=".$fecha."#parte";
#my $html = get(" http://atudem.org/?option=com_parteniev ... 2013#parte");
my $output_file = '/home/enric/csv/atu.csv';
## Interpretació del contingut
use HTML::TableExtract;
my $te = HTML::TableExtract->new();
$te->parse($html);
my @tables = $te->tables;
my @rows = $tables[0]->rows;
#shift @rows;
#my @values = map { $_->[1],$_->[2],$_->[3],$_->[4], $_->[5]} @rows;
my @values = map { $_->[0],$_->[1],$_->[3],$_->[5],$_->[7],$_->[8],$_->[9],$_->[10]} @rows;
#my @values = map { $_->[3]} @rows;
#print "$values[96]"; #nom astun
#print "$values[97]"; #remonts astun
#print "$values[98]"; #pistes astun
#print "$values[99]"; #kilometres astun
#print "$values[100]"; #gruix min astun
#print "$values[101]"; #gruix max astun
#print "$values[102]"; #tipus neu astun
#print "$values[103]"; #observacions astun
#print "$values[104]"; #nom candanchu
#print "$values[105]"; #remonts candanchu
#print "$values[106]"; #pistes candanchu
#print "$values[107]"; #kilometres candanchu
#print "$values[108]"; #gruix min candanchu
#print "$values[109]"; #gruix max candanchu
#print "$values[110]"; #tipus neu candanchu
#print "$values[111]"; #observacions candanchu
#print "$values[112]"; #nom cerler
#print "$values[113]"; #remonts cerler
#print "$values[114]"; #pistes cerler
#print "$values[115]"; #kilometres cerler
#print "$values[116]"; #gruix min cerler
#print "$values[117]"; #gruix max cerler
#print "$values[118]"; #tipus neu cerler
#print "$values[119]"; #observacions cerler
#print "$values[120]"; #nom formigal
#print "$values[121]"; #remonts formigal
#print "$values[122]"; #pistes formigal
#print "$values[123]"; #kilometres formigal
#print "$values[124]"; #gruix min formigal
#print "$values[125]"; #gruix max formigal
#print "$values[126]"; #tipus neu formigal
#print "$values[127]"; #observacions formigal
#print "$values[136]"; #nom panticosa
#print "$values[137]"; #remonts panticosa
#print "$values[138]"; #pistes panticosa
#print "$values[139]"; #kilometres panticosa
#print "$values[140]"; #gruix min panticosa
#print "$values[141]"; #gruix max panticosa
#print "$values[142]"; #tipus neu panticosa
#print "$values[143]"; #observacions panticosa
#print "$values[168]"; #nom boi
#print "$values[169]"; #remonts boi
#print "$values[170]"; #pistes boi
#print "$values[171]"; #kilometres boi
#print "$values[172]"; #gruix min boi
#print "$values[173]"; #gruix max boi
#print "$values[174]"; #tipus neu boi
#print "$values[175]"; #observacions boi
my @values2 = ('409',$values[96], $values[103],'-', $values[102], $values[100], $values[101], $values[97], $values[98], arregla($values[99]));
my @values3 = ('1258',$values[104], $values[111],'-', $values[110], $values[108], $values[109], $values[105], $values[106], arregla($values[107]));
my @values4 = ('412',$values[112], $values[119],'-', $values[118], $values[116], $values[117], $values[113], $values[114], arregla($values[115]));
my @values5 = ('410',$values[120], $values[127], '-', $values[126], $values[124], $values[125],$values[121], $values[122], arregla($values[123]));
my @values6 = ('411',$values[136], $values[143], '-', $values[142], $values[140], $values[141],$values[137], $values[138], arregla($values[139]));
my @values7 = ('413',$values[168], $values[175],'-', $values[174], $values[172], $values[173], $values[169], $values[170], arregla($values[171]));
#my $neu = $values[3];
my @titles = qw(ID ESTACIO ESTAT ACCES NEU GRUIX.MIN GRUIX.MAX REMUNTADORS PISTES KM);
open my $fh, ">:encoding(utf8)", $output_file or die $!;
use Text::CSV;
my $csv = Text::CSV->new ({
binary => 1,
eol => $/,
});
#$csv->print( \*STDOUT, \@titles );
#$csv->print( \*STDOUT, \@values2 );
#$csv->print( \*STDOUT, \@values3 );
#$csv->print( \*STDOUT, \@values4 );
#$csv->print( \*STDOUT, \@values5 );
#$csv->print( \*STDOUT, \@values6 );
#$csv->print( \*STDOUT, \@values7 );
$csv->print( $fh, \@titles);
$csv->print( $fh, \@values2 );
$csv->print( $fh, \@values3 );
$csv->print( $fh, \@values4 );
$csv->print( $fh, \@values5 );
$csv->print( $fh, \@values6 );
$csv->print( $fh, \@values7 );
close $fh;
### Subrutines
sub arregla {
my $txt = shift;
if (defined $txt) {
$txt = lc($txt);
$txt =~ s/,/./g;
}
return $txt;
}