#!/usr/bin/perl -w ###################################################################### ## ## ## ######################################################################## use strict; require LWP::UserAgent; use DBI; # and no... that's not the real password... don't even try my $dbh=DBI->connect("DBI:Pg:database=trains;host=10.1.1.1","peter",'$3cr3t!') or die $DBI::errstr; # # This URL clipped from the source of a lookup I did manually # my $url=qq( http://atisweb.njtransit.com/cgi-bin/atis81.pl?railline= &dd=9 &mina=0 &resptype=U &action=entry &lineext=NJCL%3ANorth+Jersey+Coast+Line &oloc=New+York+Penn+Station &mm=6 &dtime=12%3A00+PM &minb=0 &back=sf_tr_schedules.shtml &fare=Y &date=6%2F9%2F2005 &ori=105+++++%3ANew+York+Penn+Station &rtime=1%3A00+PM &dsid=73++++++ &dloc=Little+Silver &dow=W &line=NJCL &linelookup= &osid=105+++++ &yyyy=2005 &lineext2=NJCL%3ANorth+Jersey+Coast+Line &des=73++++++%3ALittle+Silver &linedesc=North+Jersey+Coast+Line ); $url =~ s/\n//g; $url =~ s/\s+//g; my $ua=LWP::UserAgent->new; my $response=$ua->get($url); my @lines=split("\n",$response->content); printf "Returned: %d lines\n",$#lines+1; # # Clear the table my $drop=$dbh->prepare(qq(delete from train_info where 1=1)) or die $dbh->errstr; $drop->execute or die $drop->errstr; # # Set up our insert. my $sth=$dbh->prepare('select add_train(?,?,?)') or die $dbh->errstr; foreach my $line(@lines){ chomp $line; # Remove unsightly cruft at end of line. # # Split the content line on spaces my @f=split(/[\s\t]+/,$line); my ($marker,$rail_line,$timeLeave,$timeHalfLeave, $timeArrive,$timeHalfArrive,$train)= ($f[0],$f[2],$f[4],$f[5],$f[7],$f[8],$f[9]); next if $marker ne 'TMTI'; # Not a schedule line. next if ($rail_line ne 'NJCL') and ( $rail_line ne 'NJCLL' ); # Not the train line I want # # Normalize our times to 24 hour clock format my $leave_time=fix_time($timeLeave,$timeHalfLeave); my $arrive_time=fix_time($timeArrive,$timeHalfArrive); # Insert the record $sth->execute($train,$leave_time,$arrive_time) or die $sth->errstr; } $sth->finish; $dbh->disconnect; exit(0); # # Convert 12 hour time to 24 hour time correctly. sub fix_time { my ($time,$marker)=@_; my @f=split(":",$time); if ( $f[0] == 12 ) { $f[0] = "00" if $marker eq 'AM'; } else { $f[0] += 12 if $marker eq 'PM'; } return join(":",@f); }