#!/usr/bin/perl

use LWP::Simple;
use Time::Local;
use CGI;

my $query = new CGI;
my $partenza    = $query->param('da');
my $arrivo	= $query->param('a');

# calcola l'ora attuale
my $time = time();
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time);
$year+=1900; $mon=sprintf("%02d",$mon+1); $mday=sprintf("%02d",$mday);
$hour=sprintf("%02d",$hour); $min=sprintf("%02d",$min);

my $URL = "http://orario.trenitalia.com/b2c/nppPriceTravelSolutions.do?stazin=$partenza&stazout=$arrivo&datag=$mday&datam=$mon&dataa=$year&timsh=$hour&timsm=$min&lang=it&nreq=10";

print "Content-type: text/plain\n\n";
my $content = get ($URL);

while ($content =~ /<td>(\d{2}:\d{2})<br>([^<]+)(<br>\n<span.*?\/span>)*?<\/td><td>(\d{2}:\d{2})<br>([^<]+)(<br>\n<span.*?\/span>)*?<\/td><td>(\d{2}:\d{2})<\/td>/gsi){
	print "Da: $2  A: $5\n";
	print "Partenza: $1  Arrivo: $4\n";
	print "Durata: $7\n\n";
}
