#!/bin/perl -w
use strict;
use LWP::Simple;
use Locale::Country;
use DBI;
use DBD::Pg;
my $dbh = DBI->connect("DBI:Pg:dbname=sampledb","postgres","");
sub demo::insertrow{
shift;
my ($cityairport,$countrycode,$code,$dbh) = @_;
my $sth;
my $query="select count(*) from countrycodes where code='$code'";
$sth=$dbh->prepare($query);
$sth->execute;
if(($sth->fetchrow)[0]==0){
$query="insert into countrycodes(cityairport,countrycode,code) values (?,?,?)";
$sth=$dbh->prepare($query);
$sth->execute($cityairport,$countrycode,$code);
} else {
print "already inserted ($code), skipping\n";
}
}
my $dba;
my ($content,$line);
my $countrycode;
my $query;
my ($cityairport,$country,$code,$dummy);
my $pos;
my @content_array;
my @countrycodes;
$dba = "demo";
my @alphabet= ('a'..'w','xyz');
my $letter;
for(@alphabet){
$letter=$_;
$content = get("http://www.nationsonline.org/oneworld/airport_code_$letter.htm");
print "processing http://www.nationsonline.org/oneworld/airport_code_$letter.htm\n";
$pos=index($content,"<b>City, Airport</b>");
$content=substr($content,$pos);
$pos=index($content,"</tr>")+5;
$content=substr($content,$pos);
$pos=index($content,"</table>");
$content=substr($content,0,$pos);
$content =~ s/<[a-z\/]{2,3}>//g;
foreach $line ( split(/<tr bgcolor="#EEEEEE">/,$content)){
chomp($line);
$line=~s/<.*>//g;
if(length($line)>5){
($dummy,$cityairport,$country,$code) = split(/\n/,$line);
$countrycode=country2code($country);
if(!$countrycode){
$countrycode="??";
}
if(!$code){
print "$line\n";
}
$code=~s/ //g;
$dba->insertrow($cityairport,$countrycode,$code,$dbh);
}
}
} |